今天遇到一个问题,想要单击datagrid的上的某个单元格,就可以编辑,实现过程如下!
给datagrid 加上onclickCell方法,单击单元格触发,蓝色部分为给想要编辑的单元格加上
<table class="easyui-datagrid" id="discountChangeList" toolBar="#toolBar" url="${ctx}/discountChange/view/ajaxList.action" pagination="true" style="width:100%" data-options="fitColumns:true,pageSize:20,onClickCell:onClickCell">
<thead>
<tr>
<th field="ck" checkbox="true" ></th>
<th data-options="field:'customerID',width:10,align:'center' ">编号</th>
<th data-options="field:'customerName',width:10,align:'center' ">客户姓名</th>
<th data-options="field:'fuDaoDisCount',width:10,align:'center'">辅导书折扣</th>
<th data-options="field:'changedDiscount',width:10,align:'center',editor:{type:'numberbox',options:{precision:2,max:1}} ">变化后折扣</th>
<th data-options="field:'sumMoney',width:10,align:'center' ">累计余额</th>
</tr>
</thead>
</table>
js方法
<%-- 编辑单元格--%>
$.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;
}
});
}
});
var editIndex = undefined;
结束编辑
function endEditing(index){
if (editIndex == undefined){return true}
if(index!=editIndex){
$('#discountChangeList').datagrid('unselectRow', editIndex);
}
if ($('#discountChangeList').datagrid('validateRow', editIndex)){
$('#discountChangeList').datagrid('endEdit', editIndex);
editIndex = undefined;
return true;
} else {
return false;
}
}
单击后触发
function onClickCell(index, field){
if (endEditing(index)){
if(field=="changedDiscount"){
$('#discountChangeList').datagrid('unselectRow', index);
$('#discountChangeList').datagrid('editCell', {index:index,field:field});
editIndex = index;
}
}
}
这样就可以愉快的编辑了,本来想写很多注释,但是提笔忘字,算了不写了。