Easy UI實現行內編輯,easyui實現編輯
最近跟著勇霞師姐做我們高校雲平台的UI系統,各個系統介面的管理。UI的系統雖然很小,但是技術內容很新鮮,這篇博文就像大家介紹一下我新接觸的EasyUI的知識:開啟行內編輯。
如下:
一、View頁面
首先,引入easyui的js檔案。
<link rel="stylesheet" type="text/css" href="../../Content/jquery-easyui-1.3.2/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="../../Content/jquery-easyui-1.3.2/themes/icon.css"> <link rel="stylesheet" type="text/css" href="../../Content/jquery-easyui-1.3.2/demo/demo.css"> <script type="text/javascript" src="../../Content/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="../../Content/jquery-easyui-1.3.2/jquery.easyui.min.js"></script> <script type="text/javascript" src="../../Content/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script> <link href="../../CSS/index.css" rel="stylesheet" />
其次,是對工具列和表單的設定。
<div id="ContentAreas"> <div class="easyui-panel" title="查詢頁面屬性" > <div id="buttonAreas" style="margin-left: 10px; margin-top: 10px;"> <a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="append()">添加</a> <a href="javascript:void(0)" id="edit" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="accept() " >儲存</a> <a href="#" id="lostFocus" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="destory()">刪除</a> </div> <div id="tt" > <table id="dg" class="easyui-datagrid" data-options="rownumbers:true, fitColumns:true, url:'/Properties/QueryData', pagination:true, singleSelect:false, onClickCell: onClickCell,onAfterEdit: onAfterEdit"> <thead> <tr> <th data-options="field:'ck',checkbox:true,align:'center'"></th> <th data-options="field:'QueryId',align:'center',hidden:'true'"></th> <th data-options="field:'EntityName',align:'center',sortable:true, formatter:function(value,row){return row.EntityName;}, editor:{ type:'combobox', options:{ valueField:'EntityName', textField:'EntityName', method:'get', url:'/NonQueryProperties/QueryEntity', required:true, onSelect: function(rec){ showEntityDesc(rec.EntityDesc); } } },sortable:true">實體名稱</th> <th id='aaa' data-options="field:'EntityDesc',align:'center',editor:'text'">實體描述</th> <th data-options="field:'PropertyName',align:'center',editor:'text',sortable:true">屬性名稱</th> <th data-options="field:'PropertyDesc',align:'center',editor:{type:'text',options:{required:true}}">屬性描述</th> <th data-options="field:'IsShow',align:'center',editor:{type:'checkbox',options:{on:'Y',off:'N'}},sortable:true">是否顯</th> <th data-options="field:'IsCondition',align:'center',editor:{type:'checkbox',options:{on:'Y',off:'N'}}">查詢條件</th> <th data-options="field:'PropertyOrder',align:'center',editor:'text',sortable:true">顯示順序</th> <th data-options="field:'ControlId',align:'center', formatter:function(value,row){ return row.ControlDesc; }, editor:{ type:'combobox', options:{ valueField:'ControlId', textField:'ControlDesc', method:'get', url:'/Controls/QueryAllControls', required:true } }">控制項類型</th> <th data-options="field:'ControlHtmlName',align:'center',editor:'text'">控制項HtmlName</th> <th data-options="field:'ControlHtmlId',align:'center',editor:'text'">控制項HtmlId</th> </tr> </thead> </table> </div> </div> </div>
然後,就是編寫js事件了。
var editIndex = undefined;//定義編輯列的索引function endEditing() {//判斷是否處於編輯狀態 if (editIndex == undefined) { return true } if ($('#dg').datagrid('validateRow', editIndex)) { $('#dg').datagrid('endEdit', editIndex); editIndex = undefined; return true; } else { return false; }}//1.0單擊儲存格的時候觸發function onClickCell(index, field) { if (endEditing()) { $('#dg').datagrid('selectRow', index).datagrid('editCell', { index: index, field: field }); editIndex = index; }}//2.0根據類型選擇條件進行查詢function doSearch() { //查詢參數直接添加在queryParams中 var searchName = $('#txtSearch').val(); $('#dg').datagrid('reload', {//重新載入表資訊datagrid strCondition: searchName //參數名稱 });}// 根據類型選擇條件進行查詢function entitySearch(entityId) { $('#dg').datagrid('reload', {//重新載入表資訊datagrid strCondition: entityId //參數名稱 });}//3.0向後台提交資料,包括添加和修改function onAfterEdit(index, row,value) { var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields')); var controlId = row.ControlId; //獲得隱藏的GUID列 var queryId = row.QueryId; var entityName = row.EntityName; //alert(entityName); var enQueryProperties = JSON.stringify(row); if (row.IsShow == 'N' && row.isCondition == 'Y') { alert("查詢條件必須顯示到前台頁面"); //重新載入表格式資料 $('#dg').datagrid('reload'); } else { $.ajax( { url: '/Properties/updateQueryProperty',//向後台提交資料 type: "post", async: true, dataType: 'json', data: { 'enQueryProperties': enQueryProperties'}, success: function (data) { if (data == 'true') { alert("更新成功!"); } } } ); }}//4.0添加按鈕,同時怎加隱藏的GUID列function append() { if (endEditing()) { $('#dg').datagrid('appendRow', { QueryId: '' }); editIndex = $('#dg').datagrid('getRows').length - 1; $('#dg').datagrid('selectRow', editIndex) .datagrid('beginEdit', editIndex); }}//5.0刪除事件function remove() { if (editIndex == undefined) { return } $('#dg').datagrid('cancelEdit', editIndex) .datagrid('deleteRow', editIndex); editIndex = undefined;}function accept() { $('#dg').datagrid('acceptChanges'); }
在這裡,我們需要注意的是添加和修改後的儲存事件,他們響應的是同一個js方法,那就是onAfterEdit,那麼在向Controller提交的時候就會出現問題,如何根據讓兩個不同的後台方法響應同一個事件呢?請看Controller的代碼。
二、Controller頁面
首先,先看修改後更新的方法。
public Boolean updateQueryProperty(string enQueryProperties) { #region 定義變數 //擷取查詢屬性實體的json string json = Request.Params["enQueryProperties"].ToString(); //擷取屬性對應的控制項實體 string strControlId = Request.Params["ControlId"].ToString(); //擷取記錄對應的ID string strQueryId = Request.Params["QueryId"].ToString(); #endregion #region 還原序列化 查詢屬性實體的json 為屬性實體 //還原序列化 查詢屬性實體的json 為屬性實體 QueryPropertiesViewModel enQueryPropertiesViewModel = (QueryPropertiesViewModel)JsonConvert.DeserializeObject(json, typeof(QueryPropertiesViewModel)); #endregion #region 取控制項的值 //根據控制項ID查詢控制項實體 ControlsViewModel enControlViewModel = controlWCF.QueryControlByID(strControlId); //給屬性實體的控制項屬性賦值 //enQueryPropertiesViewModel.Controls = enControlViewModel; enQueryPropertiesViewModel.ControlsControlId = enControlViewModel.ControlId; #endregion 取控制項的 //判斷strQueryId如果有值,則為修改實體 if (strQueryId != "") { //定義更新標籤 Boolean queryIsUpdate = false; //調用wcf 介面,更新查詢屬性實體 try { //更新查詢屬性實體 queryIsUpdate = queryPropertiesServiceWCF.UpdateQueryProperties(enQueryPropertiesViewModel); if (!queryIsUpdate) { Exception e = new Exception("更新失敗!"); } return true; } catch (Exception e) { throw new Exception("更新失敗!"); } }else<span style="font-family: KaiTi_GB2312;">{</span>//如果strQueryId為空白值,則說明是添加實體 //為GUID主鍵賦值 enQueryPropertiesViewModel.QueryId = Guid.NewGuid().ToString(); AddEntity(enQueryPropertiesViewModel);//執行添加操作 return true; } }
從上面更新的方法可以看出,updateQueryProperty接收的是編輯行整行的資料,我們區分是編輯單元格還是編輯新添加的一行可以通過判斷該編輯行的主鍵列是否為空白值來得到結論,如果主鍵是空值,那個編輯行一定是新添加的一列,如果主鍵有值,那就就是在原來的單元格上的修改操作。
下面是添加的操作:
public bool AddEntity(QueryPropertiesViewModel queryViewModel) { bool addIdSuccess = false; try { queryViewModel.QueryId = Guid.NewGuid().ToString(); queryViewModel.ControlHtmlId = queryViewModel.PropertyName; queryViewModel.ControlHtmlName = queryViewModel.PropertyName; if (queryPropertiesServiceWCF.AddQueryPropertiesy(queryViewModel)!=null) { addIdSuccess = true; } } catch (Exception e) { throw e; } return addIdSuccess; }
至此,便完美收工了。
總結:對行內編輯的EasyUI一直高度興趣,這次終於是接觸到了,很開心;但是中間的添加和修改兩個事件著實困惑了我好幾天的時間,剛開始是理不清思路,因為我們的代碼是從師姐那裡拷過來的,就順著她的思路往下走,把自己給繞了進去。後來還是打算自己從頭把這裡塊知識理清楚,找來了行內編輯的EasyUI的Demo,用FireBug一步一步的調試,然後再某個即將入眠的晚上,終於茅塞頓開了。這還是說明那個問題,編程思路真的很重要,有了清晰的邏輯思路,你的工作就完成90%了。