項目中有個datagrid需要編輯行時,用到Editor的屬性,那么如何添加一個事件
問題:同一個編輯行中的某個單元格值改變時,修改其他單元格的值
頁面用到的datagrid
<table id="list" data-bind="datagrid:grid"> <thead> <tr> <th field="ck" checkbox="false"></th> <th field="Uid" hidden="true"></th> <th field="OptimisticLockField" hidden="true"></th> <th field="ProjectId" hidden="true"></th> <th field="ProjectCode" sortable="true" align="center" width="120">檢查項編號</th> <th field="ProjectName" sortable="true" align="center" width="150">檢查項名稱</th> <th field="ProjectResult" sortable="true" align="center" width="150" editor="{type:'numberbox',options:{required:true,min:0,precision:2}}" >檢查項結果</th> <th field="CheckState" sortable="true" align="center" width="100" editor="{type:'combobox',options:{data:data.dataSource.CheckStates,required:true}}" styler="showStateColor">檢查狀態</th> <th field="ReferenceValue" sortable="true" align="center" width="150">參考值</th> <th field="ProjectUnitName" sortable="true" align="center" width="100">項目單位</th> </tr> </thead> </table>
如上所示,我編輯行時,只要修改檢查項和檢查結果兩個列,是檢查項結果值改變,帶出檢查狀態
js中如何實現
//grid的單擊事件 this.grid.onClickRow = function(rowIndex,rowData) { //單擊時觸發編輯行事件 $('#list').datagrid('beginEdit',rowIndex); //獲取當前修改行編輯器的數據 var editors = $('#list').datagrid('getEditors', rowIndex); //獲取行編輯器第一個對象 var editor1 = editors[0]; var editor2 = editors[1]; //console.log(rowData); //為對象的數字框添加onChange事件 editor1.target.numberbox({ onChange:function(newValue,oldValue){ if(newValue > 0) { //獲取檢查項的最小值和最大值 var minValue = parseFloat(rowData.MinValue); var maxValue = parseFloat(rowData.MaxValue); if(newValue > maxValue){ //設置combobox的值為偏高(2) editor2.target.combobox('setValue',2); } else if(newValue < minValue){ //設置combobox的值為偏低(1) editor2.target.combobox('setValue',1); } else{ //設置combobox的值正常(0) editor2.target.combobox('setValue',0); }
////傳遞檢查項結果data數據
//var dts={"ItemId": rowData.Uid,"ProjectId":rowData.ProjectId,"ProjectResult":newValue};
////請求計算結果(正常(0)、偏低(1)、偏高(2))
//com.ajax({
// type: 'POST',
// url: '/api/bpum/PumPrenatalInspect/GetProjectState/' + rowData.Uid,
// data: JSON.stringify(dts),
// success: function (str)
// {
// //設置combobox的值
// editor2.target.combobox('setValue',str);
// },
// error: function (e) {
// com.message('error', e.responseText);
// }
//}); } } }); }
圖片實例:
實現原理:
通過行單擊事件中rowIndex獲取行的編輯器對象editors,由於我們只有兩個列編輯的,所以editors[0]就獲取第一列檢查項結果編輯的對象,
editors[1]獲取檢查狀態的對象,由於第一個單元格是numberBox,第二個combobox,我們給numberBox綁定一個onChage()事件,判斷當第一個單元格
值改變時,修改第二個單元格的值。其他類型也可以修改,改成對應類型就可以了(修改數值也可以用ajax請求后修改,但是會出現刷新問題)
參考網址:
https://www.cnblogs.com/linvan/p/6607465.html