Copy link to clipboard
Copied
how do remove an option from an array dynamically? If I had something like the following I would want it so that selecting an option in the first dropdown would remove it from dropdowns 2 and 3 and then anything selected in dropdown 2 would be removed from dropdown 3
var win = new Window("dialog","options",undefined,{closeButton: true});
win.orientation='column';
var group1 = win.add("group");
group1.orientation='column';
var dropdownlist1_array = ["Select","option 1","option 2","option 3"];
var dropdownlist2_array = ["Select","option 1","option 2","option 3"];
var dropdownlist3_array = ["Select","option 1","option 2","option 3"];
var dropdownlist1 = group1.add("dropdownlist", undefined, dropdownlist1_array);
dropdownlist1.selection = 0;
dropdownlist1.preferredSize.height = 20;
dropdownlist1.onChange = function(){onChangeDropdownlist1();};
var dropdownlist2 = group1.add("dropdownlist", undefined, dropdownlist2_array);
dropdownlist2.selection = 0;
dropdownlist2.preferredSize.height = 20;
dropdownlist2.onChange = function(){onChangeDropdownlist2();};
var dropdownlist3 = group1.add("dropdownlist", undefined, dropdownlist3_array);
dropdownlist3.selection = 0;
dropdownlist3.preferredSize.height = 20;
win.show();
function onChangeDropdownlist1()
{};
function onChangeDropdownlist2()
{};
This works
var win = new Window("dialog","options",undefined,{closeButton: true});
win.orientation='column';
var group1 = win.add("group");
group1.orientation='column';
var dropdownlist1_array = ["Select","option1","option2","option3"];
var dropdownlist2_array = ["Select","option1","option2","option3"];
var dropdownlist3_array = ["Select","option1","option2","option3"];
var dropdownlist1 = group1.add("dropdownlist", undefined, dropdownlist1_array);
dropdownlist1.selecti
...
Copy link to clipboard
Copied
In this case splice method would serve you. See the documentation below
So the idea is that in the onChange method you will find the index of the element that is selected and get its index. You can get the selected item using the selection property. Once you get the index of the selected item, you can call the splice method on the array from which you want to remove the element
-Manan
Copy link to clipboard
Copied
When I run the following code the alert is "Selection 1" which is what I would have expected but from that I need to get "Select, Selection 2, Selection 3"
var remove = dropdownlist2_array.splice (1,1);
alert(remove);
Copy link to clipboard
Copied
The return value is the value that you deleted, check the element in dropdownlist2_array it should have the value removed
-Manan
Copy link to clipboard
Copied
Maybe the splice method will work, but I've found that I've had to remove all the items from a dropdownlist and repopulate it with the edited array.
Copy link to clipboard
Copied
Worth a try, good point
-Manan
Copy link to clipboard
Copied
Chuck I am sure that I saw an answer by you along those lines but struggling to find it again
Copy link to clipboard
Copied
because you can not use indexOf i thought about something like this but although to the eye the data looks the same I am not getting a match alert
function onChangeDropdownlist1()
{
var i = 0;
while (i < dropdownlist2_array.length) {
if (dropdownlist1_array[i] === dropdownlist1.selection) {
alert ("match");
var remove = dropdownlist2_array.splice(i, 1);
} else {
++i;
}
}
alert(dropdownlist2_array);
};
Copy link to clipboard
Copied
This works
var win = new Window("dialog","options",undefined,{closeButton: true});
win.orientation='column';
var group1 = win.add("group");
group1.orientation='column';
var dropdownlist1_array = ["Select","option1","option2","option3"];
var dropdownlist2_array = ["Select","option1","option2","option3"];
var dropdownlist3_array = ["Select","option1","option2","option3"];
var dropdownlist1 = group1.add("dropdownlist", undefined, dropdownlist1_array);
dropdownlist1.selection = 0;
dropdownlist1.preferredSize.height = 20;
dropdownlist1.onChange = function(){onChangeDropdownlist1();};
var dropdownlist2 = group1.add("dropdownlist", undefined, dropdownlist2_array);
dropdownlist2.selection = 0;
dropdownlist2.preferredSize.height = 20;
dropdownlist2.onChange = function(){onChangeDropdownlist2();};
var dropdownlist3 = group1.add("dropdownlist", undefined, dropdownlist3_array);
dropdownlist3.selection = 0;
dropdownlist3.preferredSize.height = 20;
win.show();
function onChangeDropdownlist1()
{
var i = 0;
while (i < dropdownlist2_array.length) {
if (dropdownlist1_array[i] === dropdownlist1.selection.text) {
var remove = dropdownlist2_array.splice(i, 1);
var remove = dropdownlist3_array.splice(i, 1);
}
++i;
}
dropdownlist2.removeAll();
for(var i=0;i<dropdownlist2_array.length;i++){
dropdownlist2.add('item',dropdownlist2_array[i])
}
dropdownlist2.selection = 0;
};
function onChangeDropdownlist2()
{
var i = 0;
while (i < dropdownlist3_array.length) {
if (dropdownlist3_array[i] === dropdownlist2.selection.text && dropdownlist2.selection.text !== "Select") {
var remove = dropdownlist3_array.splice(i, 1);
}
++i;
}
dropdownlist3.removeAll();
for(var i=0;i<dropdownlist3_array.length;i++){
dropdownlist3.add('item',dropdownlist3_array[i])
}
dropdownlist3.selection = 0;
};
thanks to the pointer from Chuck