views:

612

answers:

2

Hi all,

I have two DropDownSelect widgets added to my from what i need is to dynamically load the data in the second DropDownSelect widget as the first DropDownSelect widget changes how can i load the data in DropDownSelect widget programitacally.

Abdul khaliq

+1  A: 

DropDownSelect has an "onChange" method which you can pass an anonymous function that builds the option list for the second select using something like addOption:

var s1 = new dojox.form.DropDownSelect();
var s2 = new dojox.form.DropDownSelect();
s1.onChange(function() {
  s2.addOption(new Option("text","value"));
});
TML
Thanks for the help! One more thing, when I add the new option the control does not renter properly i.e. the widget does drop down with a new option but with empty text. I see a empty but expanding dropdown.
Abdul Khaliq
i a using markup and getting the reference of the widget by dijit.byId(...) and using the add Option method to add the option
Abdul Khaliq
also i see the value of the selected option in the second dropdownselect in the alter dialog box when i select it.
Abdul Khaliq
+2  A: 

I think you need something like this:

      dojo.connect(s1, 'onChange', function(value) {

      console.log(value); // selected in s1 value

      s2.addOption([{ 
        label: "new option1", value: 1
      },
      { 
        label: "new option2", value: 2
      },
      { 
        label: "new option3", value: 3
      }]);
    });

In this example above, when selected value of s1 changes, we load 3 new options into s2. You can pass only one option to addOption method instead of array:

    s2.addOption({ label: "new option1", value: 1 }

Probably, you also wish to clear s2 first:

    s2.options = [];
ivalkeen
yes this is exactly what i need one more thing I am using Struts2 load the data via SMD method. The above code works fine if i hardcode the values in javascript but when i load the Json string from the server side the function does not work. here is my java script code.dojo.connect(s1, 'onChange', function(value) { console.log(value); // selected in s1 value var valuefromserver = "{label: \"new option1\", value: 1},{label: \"new option2\", value: 2 },{label: \"new option3\", value: 3}"; s2.addOption([valueFromServer]); });any suggestions
Abdul Khaliq
Your code doesn't work because valuefromserver is string, but should be array (javascript object). I don't know about Struts, but in dojo.xhrGet, you can specify "handleAs: json" option to automatically convert server string to object. Just to make you code work, you can manually convert you string to JS object: valuefromserver = dojo.fromJson("[" + valuefromserver + "]"); and then call:s2.addOption(valuefromserver);to add options
ivalkeen
excellent!! thankyou very much for helping
Abdul Khaliq