Vhasure Prashant
Vhasure Prashant

Reputation: 188

Dojo: dijit.form.filteringselect dynamically add options from Json

I am getting data from json file, now i want to add it to filteringselect. i tried below code but its not adding, please help me

 HTML code==
        <select data-dojo-type="dijit/form/FilteringSelect"
                id="education"></select>

        Javascrip code==
            request.get("/json/education.json", {
                            handleAs: "json"
                        }).then(function(data) {

                            var node = dijit.byId('education');
                            dojo.forEach(data.items, function(desc, index){     
                                node.addOption({
                                     label: desc.name,
                                     value: desc.value 
                                     });
                            });

                        },
                        function(error){});


    Json code

    {
        "title":"Education",
        "items":[
    {"name":"Select Education","value":"0"},
    {"name":"B.A", "value":"1"},
    {"name":"B.Sc" ,"value":"2"},
    ...........................

Upvotes: 2

Views: 2358

Answers (2)

user4535106
user4535106

Reputation: 1

I hope this helps:

dijit.byId("select_id").store.root.add(dojo.create("option",{ value: "some", innerHTML: "label of option"}));

To remove the existing elements did just that:

var size = dijit.byId("select_id").store.root.removeChild.length;    
for(var i=size; i>=0; i--){   dijit.byId("select_id").store.root.removeChild(dijit.byId("select_id").store.root.children[size-1]);
}

Upvotes: 0

vivek_nk
vivek_nk

Reputation: 1600

The store of FilteringSelect can be set dynamically, based on the ajax call.

var str = new dojo.store.Memory(optionData);
dijit.byId('widgetId').set('store',str);

But, your json data must be like this:-

var optionData={
                data:[
                   {id:'aaaaaa',name:'aaaaaa'},
                   {id:'bbbbbb',name:'bbbbbb'}
                  ]
               };

The above example will actually replace the store. If, the new data from ajax call need to be appended to the existing options, then

 //str is the existing store of FilteringSelect field above
 dojo.forEach(result.data, function(optionSet, index){     
                str.add(optionSet);
 });

Note to remember: 'addOption' is available only for Select. Not for FilteringSelect.

Upvotes: 2

Related Questions