效果圖以下:javascript
1、View頁面css
首先,引入easyui的js文件。 html
- <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" />
其次,是對工具欄和表單的設置。java
- <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事件了。jquery
- 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;
- }
- }
- function onClickCell(index, field) {
- if (endEditing()) {
- $('#dg').datagrid('selectRow', index)
- .datagrid('editCell', { index: index, field: field });
- editIndex = index;
- }
- }
- function doSearch() {
-
- var searchName = $('#txtSearch').val();
- $('#dg').datagrid('reload', {
- strCondition: searchName
-
- });
- }
-
- function entitySearch(entityId) {
- $('#dg').datagrid('reload', {
- strCondition: entityId
-
- });
- }
-
- function onAfterEdit(index, row,value) {
- var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields'));
-
- var controlId = row.ControlId;
-
- var queryId = row.QueryId;
- var entityName = row.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("更新成功!");
- }
-
- }
- }
- );
- }
-
- }
- function append() {
- if (endEditing()) {
- $('#dg').datagrid('appendRow', { QueryId: '' });
- editIndex = $('#dg').datagrid('getRows').length - 1;
- $('#dg').datagrid('selectRow', editIndex)
- .datagrid('beginEdit', editIndex);
-
- }
- }
- function remove() {
- if (editIndex == undefined) { return }
- $('#dg').datagrid('cancelEdit', editIndex)
- .datagrid('deleteRow', editIndex);
- editIndex = undefined;
- }
- function accept() {
- $('#dg').datagrid('acceptChanges');
- }
在這裏,咱們須要注意的是添加和修改後的保存事件,他們響應的是同一個js方法,那就是onAfterEdit,那麼在向Controller提交的時候就會出現問題,如何根據讓兩個不一樣的後臺方法響應同一個事件呢?請看Controller的代碼。ajax
2、Controller頁面編程
首先,先看修改後更新的方法。
json
- public Boolean updateQueryProperty(string enQueryProperties)
- {
- #region 定義變量
-
- string json = Request.Params["enQueryProperties"].ToString();
-
- string strControlId = Request.Params["ControlId"].ToString();
-
- string strQueryId = Request.Params["QueryId"].ToString();
- #endregion
-
- #region 反序列化 查詢屬性實體的json 爲屬性實體
-
- QueryPropertiesViewModel enQueryPropertiesViewModel = (QueryPropertiesViewModel)JsonConvert.DeserializeObject(json, typeof(QueryPropertiesViewModel));
- #endregion
-
- #region 取控件的值
-
- ControlsViewModel enControlViewModel = controlWCF.QueryControlByID(strControlId);
-
-
-
- enQueryPropertiesViewModel.ControlsControlId = enControlViewModel.ControlId;
- #endregion 取控件的
-
-
- if (strQueryId != "")
- {
-
- Boolean queryIsUpdate = false;
-
-
- 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>
-
- enQueryPropertiesViewModel.QueryId = Guid.NewGuid().ToString();
- AddEntity(enQueryPropertiesViewModel);
- return true;
- }
- }
從上面更新的方法能夠看出,updateQueryProperty接收的是編輯行整行的數據,咱們區分是編輯單元格仍是編輯新添加的一行能夠經過判斷該編輯行的主鍵列是否爲空值來獲得結論,若是主鍵是空值,那個編輯行必定是新添加的一列,若是主鍵有值,那就就是在原來的單元格上的修改操做。app
下面是添加的操做:async
- 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%了。