单独使用combobox,设置默认值看官方文档即可。
情景描述:
JS中初始化datagrid,列中包含combobox,并为combobox设置默认值。
代码:只贴部分代码。
$("#id").datagrid..............
{ field: "XXX", title: "XXX", width: "10%",align: "center",editor:{
type:'combobox',
options:{
data:[{id:"1",text:"X1"},{id:"0",text:"X2"}],
valueField:"id",
panelHeight:"auto",
height:30,
textField:"text",
editable: false,
required:true,
disabled: disabled
}
}},
网上给出的办法1(失败):
修改data
data:[{id:"1",text:"X1"},{id:"0",text:"X2",selected:true}]
网上给出的方法2(失败):
{
field: 'NMK06', title: '性别', width: 100, align: 'center',
editor: {
type: 'combobox', options: {
valueField: "value", textField: "text",
data: [{ value: '请选择', text: '请选择'},{ value: '男', text: '男' },{ value: '女', text: '女' }],
onLoadSuccess: function () {
// $(this).combobox('setValue', '0');
// $(this).combobox('setText', '--请选择--');
},
panelHeight: '60'
}
}, sortable: true
},
这里没有贴我的代码,意思类似,就是在options中增加了onLoadSuccess方法,然后使用$(this).combobox('setValue','0');
有人说用了可以,但我用了不太行。
我自己的解决方案:
不在options中处理,而在datagrid加载之后,为其赋值,也就是在datagrid的onLoadSuccess方法中。代码:
onLoadSuccess:function (data){
var rows = $(this).datagrid("getRows");
for(var index=0;index<;index++){
$(this).datagrid("beginEdit", index);
if($(this).datagrid("getEditor",{index:index,field:"subcontractingZb"}).('getValue') == ''){
$(this).datagrid("getEditor",{index:index,field:"subcontractingZb"}).('setValue', "0");
}
},
},
代码中的if条件是因为,有时候后台传过来值,就不需要再设置默认值了。