Dojo and dynamically add Dijit.Form.select options

I use dojo 1.8.2, here is my problem (I have seen this and this questions, but they did not help):

My The JS code receives some data in JSON format from the server. With it, I dynamically created some options for dijit.form.select:

var select = registry.byId( 'zgloszenieDoFirmyEdycja');
for (var uzytkownik in dane.uzytkownicy){
var idUzytkownika = dane.uzytkownicy[uzytkownik]['_id']['$oid'];
var imie = dane.uzytkownicy[uzytkownik].imie;
var nazwisko = dane.uzytkownicy[uzytkownik].nazwisko;
var wybrany = (idUzytkownika == id);
var opcja = {};

opcja.label = imie + '' + nazwisko;
opcja.value = idUzytkownika;
opcja.selected = wybrany;

console.log (wybrany);
console.log(idUzytkownika + '|' + imie + '' + nazwisko);
console.log(opcja);

select.addOption(opcja );
/*select.addOption({
label: imie + '' + nazwisko,
value: idUzytkownika,
selected: wybrany
});*/
}

This is my console output:

false
5077d2a1e4b0f5734a9850a1 | zero zero
Object {label="zero zero", value=" 5077d2a1e4b0f5734a9850a1", selected=false}
true
50c0776f096aa0e726d221a3 | raz raz
Object {label="raz raz", value="50c0776f096aa0e726d221a3", selected=true}
false
50d019c3096aa862c6898cdb | dwa dwa
Object {label="dwa dwa", value="50d019c3096aa862c6898cdb", selected=false}

But after updating dijit.form.select, the selected parameters will Somehow confused and set to true, not the option I set for it, but for the first one:

console.log(select);
...
Object[Object {label="zero zero", value="5077d2a1e4b0f5734a9850a1", selected=true},
Object {label="raz raz", value="50c0776f096aa0e726d221a3", selected= false},
Object {label="dwa dwa", value="50d019c3096aa862c6898cdb", selected=false}
]
...

I don’t understand why Is this clue happening?

View the source code of dijit / form / _FormSelectWidget, the problem is that if no option is selected, then The first option is selected by default. When you add the first option, this condition will be met and the first option will be selected.

Solution: Add all options as an array at once:

var select = registry.byId("select1");

option1 = {value: "o1", label: "option 1", selected: false };
option2 = {value: "o2", label: "option 2", selected: true };

select.addOption([option1, option2]); / / add all options at once as an array

See it in action: http://jsfiddle.net/phusick/BfTXC/

Me Using dojo 1.8.2, here is my problem (I have seen this and this questions, but they did not help):

My JS code receives some data in JSON format from the server. Yes With it, I dynamically created some options for dijit.form.select:

var select = registry.byId('zgloszenieDoFirmyEdycja');
for (var uzytkownik in dane.uzytkownicy){
var idUzytkownika = dane.uzytkownicy[uzytkownik]['_id']['$oid'];
var imie = dane.uzytkownicy[uzytkownik].imie;
var nazwisko = dane.uzytkownicy[uzytkownik].nazwisko;
var wybran y = (idUzytkownika == id);
var opcja = {};

opcja.label = imie + '' + nazwisko;
opcja.value = idUzytkownika;
opcja.selected = wybrany;

console.log(wybrany);
console.log(idUzytkownika + '|' + imie + '' + nazwisko);
console .log(opcja);

select.addOption(opcja);
/*select.addOption({
label: imie + '' + nazwisko,
value : idUzytkownika,
selected: wybrany
});*/
}

This is my console output:

false
5077d2a1e4b0f5734a9850a1 | zero zero
Object {label="zero zero", value="5077d2a1e4b0f5734a9850a1", selected=false}
true
50c0776f096aa0e726d221a3 | raz raz
/>Object {label="raz raz", value="50c0776f096aa0e726d221a3", selected=true}
false
50d019c3096aa862c6898cdb | dwa dwa
Object {label="dwa dwa", value=" 50d019c3096aa862c6898cdb", selected=false}

But after updating dijit.form.select, the selected parameters will be confused in some way and set to true, instead of Is the option I set for it, but for the first one:

console.log(select);
...
Object[Object { label="zero zero", value="5077d2a1e4b0f5734a9850a1", selected=true},
Object {label="raz raz", value="50c0776f096aa0e726d221a3", selected=false},
Object {label= "dwa dwa", value="50d019c3096aa862c6898cdb", selected=false}
]
...

I don’t understand why this clue happens?

Check the source code of dijit/form/_FormSelectWidget, the problem is because if no option is selected, the first option will be selected by default. When you When adding the first option, this condition will be met and the first option will be selected.

Solution: Add all options as an array at once:

< pre>var select = registry.byId(“select1”);

option1 = {value: “o1”, label: “option 1”, selected: false };
option2 = { value: “o2”, label: “option 2”, selected: true };

select.addOption([option1, option2]); // add all options at once as an array

See it in action: http://jsfiddle.net/phusick/BfTXC/

Leave a Comment

Your email address will not be published.