有人可以向我解释为什么这个简单的直接代码不起作用, var serviceStore = new dojo.data.ItemFileWriteStore({data: {identifier: "serviceCode",items:[]}}); //jsonObj is a json object that I obtain from the server via AJAX f
var serviceStore = new dojo.data.ItemFileWriteStore({ data: {identifier: "serviceCode",items:[]} }); //jsonObj is a json object that I obtain from the server via AJAX for(var i = 0; i<jsonObj.length;i++){ serviceStore.newItem({serviceCode: jsonObj[i]}); } var serviceFilterSelect = dojo.byId('serviceSelect'); serviceFilterSelect.store = serviceStore;
没有显示任何错误,但我的id为“serviceSelect”的组合框没有显示任何选项,组合在我的代码的html部分声明,
<input dojoType = "dijit.form.ComboBox" id="serviceSelect"></input>
任何指向正确方向的指针都将非常受欢迎.
首先,您应该使用dijit.byId来获取dojo小部件而不是dojo.byId.此外,jsonObj中的每个项目都应包含字段“name”.该字段将显示在组合框中.例如:
dojo.require("dojo.data.ItemFileWriteStore"); dojo.require("dijit.form.ComboBox"); var storeData = { identifier: 'serviceCode', items: [] } var jsonObj = [{ serviceCode: 'sc1', name: 'serviceCode1' }, { serviceCode: 'sc2', name: 'serviceCode2' }] dojo.addOnLoad(function () { var serviceStore = new dojo.data.ItemFileWriteStore({ data: storeData }); for (var i = 0; i < jsonObj.length; i++) { serviceStore.newItem(jsonObj[i]); } var serviceFilterSelect = dijit.byId('serviceSelect'); serviceFilterSelect.attr('store', serviceStore); });
和HTML:
<select dojotype="dijit.form.ComboBox" id="serviceSelect" ></select>
好像是it works.