Skip to main content
rakeshk21205956
Inspiring
July 1, 2020
Answered

Copy all List Box option to other List box via button at once.

  • July 1, 2020
  • 1 reply
  • 835 views

I want to achieve something like this :

Document to have 2 list boxes  one filled with options and other empty and along with 1 button (add.)..

I should be able to select multiple option from 1st list box and then press Add button and it will populate all the options from list box1 to list box2 all at once.

 

Currently i have found a file on internet which does the same. but it add 1 option at a time.... when it select multiple options in the 1st listbox.. the add button doesnot work.

 

Plz guide ..

 

Thanks

This topic has been closed for replies.
Correct answer rakeshk21205956

Below Script worked for me :

 

 

var tableSource=new Array();
var selection = this.getField("List Box1").currentValueIndices;
var List1 = this.getField("List Box1");
var List2 = this.getField("List Box2")
if (typeof selection!="object") {
	tableSource.push([List1.getItemAt(selection,false),List1.getItemAt(selection,true)]);
} else {
	for (var i=0; i<selection.length; i++) tableSource.push([List1.getItemAt(selection[i],false),List1.getItemAt(selection[i],true)]);
}
var tableCible=new Array();
var listeCible=List2.value;
if (typeof listeCible!="object") {
	tableCible.push([List2.getItemAt(0,false),List2.getItemAt(1,true)]);
} 
tableCible.push([List2.getItemAt(i,false),List2.getItemAt(i,true)]);
}
for (var i=0; i<tableSource.length; i++) {
	var abc = 0;
	for (j=0; j<tableCible.length; j++) {
		if (tableSource[i][0]==tableCible[j][0] && tableSource[i][1]==tableCible[j][1]) {
			abc++;
			break;
		}
		if (abc==0) List2.insertItemAt(tableSource[i][0],tableSource[i][1],-1);
	}
}

 

1 reply

try67
Community Expert
Community Expert
July 1, 2020

You can use this code to do it:

var f1 = this.getField("List Box1");
var f2 = this.getField("List Box2");
var selectedItems = f1.value;
if (typeof selectedItems=="object") {
	for (var i in selectedItems) {
		f2.insertItemAt({cName: selectedItems[i], nIdx: -1});
	}
} else f2.insertItemAt({cName: selectedItems, nIdx: -1});

 

Adjust the field names in the first two lines, as needed.

rakeshk21205956
Inspiring
July 6, 2020

Thanks this script works but it copies the export value of the first list box.

rakeshk21205956
rakeshk21205956AuthorCorrect answer
Inspiring
September 18, 2020

Below Script worked for me :

 

 

var tableSource=new Array();
var selection = this.getField("List Box1").currentValueIndices;
var List1 = this.getField("List Box1");
var List2 = this.getField("List Box2")
if (typeof selection!="object") {
	tableSource.push([List1.getItemAt(selection,false),List1.getItemAt(selection,true)]);
} else {
	for (var i=0; i<selection.length; i++) tableSource.push([List1.getItemAt(selection[i],false),List1.getItemAt(selection[i],true)]);
}
var tableCible=new Array();
var listeCible=List2.value;
if (typeof listeCible!="object") {
	tableCible.push([List2.getItemAt(0,false),List2.getItemAt(1,true)]);
} 
tableCible.push([List2.getItemAt(i,false),List2.getItemAt(i,true)]);
}
for (var i=0; i<tableSource.length; i++) {
	var abc = 0;
	for (j=0; j<tableCible.length; j++) {
		if (tableSource[i][0]==tableCible[j][0] && tableSource[i][1]==tableCible[j][1]) {
			abc++;
			break;
		}
		if (abc==0) List2.insertItemAt(tableSource[i][0],tableSource[i][1],-1);
	}
}