简体   繁体   English

在Dojo中动态填充ComboBox

[英]Populating ComboBox dynamically in dojo

I am trying to populate a ComboBox dynamically in dojo. 我试图在dojo中动态填充ComboBox。 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. 我已经在html中声明了它,然后尝试在js中创建Memory存储,然后使用我在js中创建的store值设置ComboBox的store属性。 Here are my html and javascript files. 这是我的html和javascript文件。 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. 我在js中调用一个函数,该函数以json响应( item )作为参数,并且响应值即将到来( ResultData1,ResultData2,ResultData3 ),我通过保留警报框进行了测试。 But when running this page i am getting TypeError: Memory is not a constructor error. 但是,当运行此页面时,我收到TypeError:内存不是构造函数错误。 Can someone please explain me what i am doing wrong. 有人可以解释一下我在做什么错。

FYI: I have added all the required dependency list in my js file. 仅供参考:我已经在我的js文件中添加了所有必需的依赖项列表。

HTML: HTML:

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

JavaScript: 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);

Using a DOM API to update the store on a widget is not going to work. 使用DOM API更新小部件上的商店将不起作用。 Instead of using domAttr.set to set the store, you should be referencing the widget itself and calling set('store', ...) on the widget. 而不是使用domAttr.set来设置商店,您应该引用窗口​​小部件本身并在窗口小部件上调用set('store', ...)

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. 此外,模板中的小部件上不需要静态id ,因为您已经为其分配了附加点。 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. 给它分配一个静态id使其不可能一次创建一个以上的窗口小部件实例,因为静态ID在实例之间会发生冲突。

You should be able to solve your problem with the following changes: 您应该可以通过以下更改来解决您的问题:

  1. Remove the static id="..." from the element in the template 从模板中的元素中删除静态id="..."
  2. Replace var combo = dijit.byId('importDocumentTo') with var combo = this.importDocumentTo (to reference the attach point rather than the ID) var combo = dijit.byId('importDocumentTo')替换为var combo = this.importDocumentTo (引用附加点而不是ID)
  3. Replace domAttr.set("importDocumentTo", ...) with combo.set('store', stateStore) combo.set('store', stateStore) domAttr.set("importDocumentTo", ...)替换domAttr.set("importDocumentTo", ...) combo.set('store', stateStore)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM