Vince
Vince

Reputation: 219

Refresh a dijit.form.Select

First, you have to know that I am developing my project with Struts (J2EE Here is my problem :

I have 2 dijit.form.Select widgets in my page, and those Select are filled with the same list (returned by a Java class).

When I select an option in my 1st "Select widget", I would like to update my 2nd Select widget, and disable the selected options from my 1st widget (to prevent users to select the same item twice).

I succeed doing this (I'll show you my code later), but my problem is that when I open my 2nd list, even once, it will never be refreshed again. So I can play a long time with my 1st Select, and choose many other options, the only option disabled in my 2nd list is the first I've selected.

Here is my JS Code :

function removeSelectedOption(){
       var list1 = dijit.byId("codeModif1");
       var list2 = dijit.byId("codeModif2");
       var list1SelectedOptionValue = list1.get("value");

       if(list1SelectedOptionValue!= null){
           list2.reset();
           for(var i = 0; i < myListSize; i++){
               // If the value of the current option = my selected option from list1
               if(liste2.getOptions(i).value == list1SelectedOptionValue){
                   list2.getOptions(i).disabled = true;
               } else {
                   list2.getOptions(i).disabled = false;
           }
       }
   }

Thanks for your help

Regards

Upvotes: 2

Views: 5737

Answers (1)

Frode
Frode

Reputation: 5710

I think you have to reset() the Select after you've updated its options' properties. Something like:

function removeSelectedOption(value)
{
    var list2 = dijit.byId("codeModif2"),
        prev  = list2.get('value');

    for(var i = 0; i < myListSize; i++)
    {
        var opt = myList[i];
        opt.disabled = opt.value === value;
        list2.updateOption(opt);
    }
    list2.reset();
    // Set selection again, unless it was the newly disabled one.
    if(prev !== value)  list2.set('value', prev);
};

(I'm assuming you have a myList containing the possible options here, and the accompanying myListSize.)

Upvotes: 2

Related Questions