我以为这会超级简单,但找不到解决方案。我有一个使用Jquery构建的Kendo下拉菜单。我不知道如何根据Json中的属性动态设置默认值。
数据源是这样的:
var dataStores2 = {"stores":[{"id":100, "name":"Shop 1" ,"defaultStore":false},{"id":150, "name":"Shop 2" ,"defaultStore":true},{"id":900, "name":"Shop 3" ,"defaultStore":false}]};
使用以下命令构建下拉列表:
var storeList = $('#StoreNameSelect').data('kendoDropDownList');
if (!storeList) {
console.log('store list is not initialised');
// create DropDownList from input HTML element
$('#StoreNameSelect').kendoDropDownList({
dataTextField: 'name',
dataValueField: 'id',
dataSource: dataStores2.stores,
dataBound: function(e) {
// handle the event
console.log(this);
this.select(1);
}
});
}
您可以看到我正在使用以下代码对dataBound事件中的默认值进行硬编码:this.select(1);
如何动态地基于Json中的defaultStore布尔值?
我正在使用它作为解决方案,但是如果有更好的方法可以使用它,可能会感兴趣。
if (!storeList) {
console.log('store list is not initialised');
// create DropDownList from input HTML element
$('#StoreNameSelect').kendoDropDownList({
dataTextField: 'name',
dataValueField: 'id',
dataSource: dataStores2.stores,
dataBound: function(e) {
// handle the event
for (var i=0,len=dataStores2.stores.length; i<len; i++){
//console.log(dataStores2.stores[i].defaultStore);
if(dataStores2.stores[i].defaultStore){
this.select(i);
}
}
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句