Populating ComboBox dynamically in dojo

2019-08-31 01:26发布

问题:

I am trying to populate a ComboBox dynamically in dojo. I have declared it in html and I am trying to created the Memory store in js and then setting the store attribute for the ComboBox with that value of store which I am creating in js. Here are my html and javascript files. I am calling a function in js which get a json response(item) as its argument and in that response values are coming(ResultData1,ResultData2,ResultData3) I have tested that by keeping alert boxes. But when running this page i am getting TypeError: Memory is not a constructor error. Can someone please explain me what i am doing wrong.

FYI: I have added all the required dependency list in my js file.

HTML:

<select data-dojo-type="dijit/form/ComboBox" data-dojo-attach-point="importDocumentTo" id="importDocumentTo" name="importDocumentTo">

JavaScript:

_onPopulate : function(item) {
                 alert('_onPopulate:');            
                 var combo = dijit.byId('importDocumentTo');
                 alert('combo' + combo)

                 var stateStore=new Memory({
                       data: [
                              {name:item["ResultData1"], id:"data1"},
                              {name:item["ResultData2"], id:"data2"},
                              {name:item["ResultData3"], id:"data3"}
                              ]      
                 });

                 alert('stateStore:' + stateStore);

          var result=domAttr.set("importDocumentTo","store",stateStore);

回答1:

Using a DOM API to update the store on a widget is not going to work. Instead of using domAttr.set to set the store, you should be referencing the widget itself and calling set('store', ...) on the widget.

Moreover, there should be no need for a static id on the widget in your template, since you are already assigning it an attach point. Assigning it a static id makes it impossible to create more than one instance of the widget at a time, because the static ID would conflict between instances.

You should be able to solve your problem with the following changes:

  1. Remove the static id="..." from the element in the template
  2. Replace var combo = dijit.byId('importDocumentTo') with var combo = this.importDocumentTo (to reference the attach point rather than the ID)
  3. Replace domAttr.set("importDocumentTo", ...) with combo.set('store', stateStore)