需求在 datagrid 编辑框中开启一个combobox ,但是里面的数据需要开启的时候才会知道,数据会根据其他因数变更
参考原文 :http://blog.csdn.net/donggua3694857/article/details/51433882
//点击单元格触发的事件
function onClickCell(index, field) {
if (endEditing() && fag) {
$('#subDg').datagrid('selectRow', index).datagrid('editCell', { index: index, field: field });//开启field单元格编辑
editIndex = index;
}
var taskLevelsData =$("#taskLevels").val(); //数据源 如: 11,22,33,44,55,
if(field=="taskLevel" && taskLevelsData){//下拉编辑列时触发 taskLevel 为列名
var editors = $('#subDg').datagrid('getEditors', index);//取当前的编辑器
var row = $('#subDg').datagrid('getRows')[index];
var smEditor = editors[0];
$(smEditor.target).combobox({
onLoadSuccess: function () { //远程加载时触发
},
onShowPanel: function(){ //下拉展开时动态修改options
var data = [];
$.each(taskLevelsData.split(","),function(i,e){
var obj={};
obj.id=e;
obj.text=e;
data.push(obj);
})
$(smEditor.target).combobox("loadData", data);
//设置值
$(smEditor.target).combobox('setValue', row.taskLevel); //row的属性赋值
}
});
}
}
var editIndex = undefined;
//判断是否编辑结束
function endEditing() {
if (editIndex == undefined) { return true }
if ($('#subDg').datagrid('validateRow', editIndex)) {
$('#subDg').datagrid('endEdit', editIndex);
editIndex = undefined;
return true;
} else {
return false;
}
}
$.extend($.fn.datagrid.methods, {
editCell: function (jq, param) {
return jq.each(function () {
var opts = $(this).datagrid('options');
var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields'));
for (var i = 0; i < fields.length; i++) {
var col = $(this).datagrid('getColumnOption', fields[i]);
col.editor1 = col.editor;
if (fields[i] != param.field) {
col.editor = null;
}
}
$(this).datagrid('beginEdit', param.index);
for (var i = 0; i < fields.length; i++) {
var col = $(this).datagrid('getColumnOption', fields[i]);
col.editor = col.editor1;
}
});
}
});