在EasyUI的DataGrid中嵌入Combobox,easyuidatagrid
在做項目時,需要在EasyUI的DataGrid中嵌入Combobox,花了好幾天功夫,在大家的協助下,終於看到了它的廬山真面:
核心代碼如下:
<html><head> @*添加Jquery EasyUI的樣式*@ <link href="@Url.Content("../../Content/JqueryEasyUI/themes/default/easyui.css")" rel="stylesheet" /> <link href="@Url.Content("../../Content/JqueryEasyUI/themes/icon.css")" rel="stylesheet" /> @*添加Jquery,EasyUI和easyUI的語言套件的JS檔案*@ <script src="@Url.Content("../../Content/JqueryEasyUI/jquery-1.8.0.min.js")"></script> <script src="@Url.Content("../../Content/JqueryEasyUI/jquery.easyui.min.js")"></script> <script src="@Url.Content("../../Content/JqueryEasyUI/locale/easyui-lang-zh_CN.js")"></script> @*實現對EasyUI的DataGird控制項操作的JS代碼*@ <script type="text/javascript"> $(function () { //當頁面首次重新整理的時候執行的事件 initTable(); }); //實現新聞DataGird控制項的綁定操作 function initTable(queryData) { $('#test').datagrid({ //定位到Table標籤,Table標籤的ID是test fitColumns: true, url: '/News/QueryAllNews', //指向背景Action來擷取目前使用者的資訊的Json格式的資料 title: '新聞公告', //表格標題 striped: true, //斑馬線效果 pagination: true, //分頁工具列 rownumbers: true, //顯示行號 onClickCell: onClickCell, //點擊儲存格觸發的事件(重要) onAfterEdit:onAfterEdit, //編輯儲存格之後觸發的事件(重要) idField: 'NewsID', //識別欄位 queryParams: queryData, //非同步查詢的參數 columns: [[ { field: 'ck', checkbox: true }, { title: '主鍵', field: 'NewsID', sortable: true, hidden: true, }, <span style="white-space:pre"></span> { title: '內容標題', field: 'NewsTitle', width: 50, sortable: true }, <span style="white-space:pre"></span> { title: '具體內容', field: 'NewsContent', sortable: true, hidden: true, }, <span style="white-space:pre"></span> { title: '建立時間', field: 'TimeStamp', sortable: true, }, <span style="white-space:pre"></span> { title: '所屬類別', field: 'CategoryName', sortable: true, }, <span style="white-space:pre"></span> { title: '建立人', field: 'UserName', sortable: true }, <span style="white-space:pre"></span> { title: '是否在首頁顯示', field: 'IsShow', sortable: true, editor: { type: 'combobox', options: { valueField: 'text', textField: 'text', method: 'get', url: '/News/ReturnIsShowData', required: true } } } ]], toolbar: [{ id: 'btnadd', text: '添加', iconCls: 'icon-add', handler: function () { //實現彈出註冊資訊的頁面 AddNews(); } }, '-', { id: 'btncut', text: '修改', iconCls: 'icon-cut', handler: function () { //實現修改的方法 UpdateLzjs(); } }, '-', { id: 'btnCancle', text: '刪除', iconCls: 'icon-remove', handler: function () { //實現直接刪除所有資料的方法 DeleteLzjs(); } }] }); } $.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() { if (editIndex == undefined) { return true } if ($('#test').datagrid('validateRow', editIndex)) { $('#test').datagrid('endEdit', editIndex); editIndex = undefined; return true; } else { return false; } }//點擊儲存格觸發的事件 function onClickCell(index, field) { if (endEditing()) { $('#test').datagrid('selectRow', index).datagrid('editCell', { index: index, field: field }); editIndex = index; } }//編輯完儲存格之後觸發的事件 function onAfterEdit(index, row, changes) { //…執行編輯儲存格後需要執行的操作… //…執行編輯儲存格後需要執行的操作… } } </script></head><body> <table id="test" style="width: 955px; margin: 20px 0 10px 8px" title="新聞中心" iconcls="icon-edit"> </table></body></html>
在Combobox類型的editor的資料來源url: '/News/ReturnIsEnabledData'在相應Controller中對應的方法為(其實就是在後台拼了個特別簡單的json串):
public string ReturnIsShowData() { string strJson = "[{\"id\":\"yes\",\"text\":\"是\"},{\"id\":\"no\",\"text\":\"否\"}]"; return strJson; }
當選擇了Combobox中的值時,只需要把將要執行的操作寫在onAfterEdit(index, row, changes)函數中即可,index為編輯的行號,預設從0開始;row為被編輯單元格所在的整個行,row.列名可以獲得此行此列的資料,如row.IsShow可以獲得被編輯單元格所在行的IsShow列的資料。
(此文內容基於Asp.net MVC)
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。