虽然没有注释,但是勉=仔细看看还是可以的

    效果图如下:

    

    一、View页面

    首先,引入easyui的js文件。   

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <link rel="stylesheet" type="text/css" href="../../Content/jquery-easyui-1.3.2/themes/default/easyui.css">  
  2. <link rel="stylesheet" type="text/css" href="../../Content/jquery-easyui-1.3.2/themes/icon.css">  
  3. <link rel="stylesheet" type="text/css" href="../../Content/jquery-easyui-1.3.2/demo/demo.css">  
  4. <script type="text/javascript" src="../../Content/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>  
  5. <script type="text/javascript" src="../../Content/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>  
  6. <script type="text/javascript" src="../../Content/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script>  
  7. <link href="../../CSS/index.css" rel="stylesheet" />  

    

    其次,是对工具栏和表单的设置。

    

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <div id="ContentAreas">  
  2.   
  3.     <div class="easyui-panel" title="查询页面属性" >  
  4.   
  5.          <div id="buttonAreas" style="margin-left: 10px; margin-top: 10px;">  
  6.             <href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="append()">添加</a>  
  7.             <href="javascript:void(0)" id="edit" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="accept() " >保存</a>  
  8.             <href="#" id="lostFocus" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="destory()">删除</a>  
  9.   
  10.         </div>  
  11.         
  12.   
  13.         <div id="tt" >  
  14.             <table id="dg" class="easyui-datagrid"   
  15.                 data-options="rownumbers:true,  
  16.                     fitColumns:true,  
  17.                     url:'/Properties/QueryData',  
  18.                     pagination:true,  
  19.                     singleSelect:false,  
  20.                     onClickCell: onClickCell,onAfterEdit: onAfterEdit">  
  21.                 <thead>  
  22.                     <tr>  
  23.                         <th data-options="field:'ck',checkbox:true,align:'center'"></th>  
  24.                         <th data-options="field:'QueryId',align:'center',hidden:'true'"></th>  
  25.                         <th data-options="field:'EntityName',align:'center',sortable:true,  
  26.                            formatter:function(value,row){  
  27.             return row.EntityName;  
  28.         },  
  29.   
  30.                                          editor:{  
  31.                                                 type:'combobox',  
  32.                                                 options:{  
  33.                                                 valueField:'EntityName',  
  34.                                                 textField:'EntityName',  
  35.                                                 method:'get',  
  36.                                                 url:'/NonQueryProperties/QueryEntity',  
  37.                                                 required:true,  
  38.                                                 onSelect: function(rec){  
  39.                                                           showEntityDesc(rec.EntityDesc);  
  40.   
  41.   
  42.                             }  
  43.   
  44.                             }  
  45.                             },sortable:true">实体名称</th>  
  46.                         <th id='aaa' data-options="field:'EntityDesc',align:'center',editor:'text'">实体描述</th>  
  47.   
  48.                         <th data-options="field:'PropertyName',align:'center',editor:'text',sortable:true">属性名称</th>  
  49.                         <th data-options="field:'PropertyDesc',align:'center',editor:{type:'text',options:{required:true}}">属性描述</th>  
  50.                         <th data-options="field:'IsShow',align:'center',editor:{type:'checkbox',options:{on:'Y',off:'N'}},sortable:true">是否显</th>  
  51.                         <th data-options="field:'IsCondition',align:'center',editor:{type:'checkbox',options:{on:'Y',off:'N'}}">查询条件</th>  
  52.                         <th data-options="field:'PropertyOrder',align:'center',editor:'text',sortable:true">显示顺序</th>  
  53.   
  54.                         <th data-options="field:'ControlId',align:'center',  
  55.                  formatter:function(value,row){  
  56.                         return row.ControlDesc;  
  57.                         },  
  58.                         editor:{  
  59.                         type:'combobox',  
  60.                         options:{  
  61.                         valueField:'ControlId',  
  62.                         textField:'ControlDesc',  
  63.                         method:'get',  
  64.                         url:'/Controls/QueryAllControls',  
  65.                         required:true  
  66.                         }  
  67.                         }">控件类型</th>  
  68.                         <th data-options="field:'ControlHtmlName',align:'center',editor:'text'">控件HtmlName</th>  
  69.                         <th data-options="field:'ControlHtmlId',align:'center',editor:'text'">控件HtmlId</th>  
  70.                     </tr>  
  71.                 </thead>  
  72.             </table>  
  73.         </div>        
  74.         </div>  
  75. </div>  


    然后,就是编写js事件了。

    

[javascript] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. var editIndex = undefined;//定义编辑列的索引  
  2. function endEditing() {//判断是否处于编辑状态  
  3.     if (editIndex == undefined) { return true }  
  4.     if ($('#dg').datagrid('validateRow', editIndex)) {  
  5.         $('#dg').datagrid('endEdit', editIndex);  
  6.         editIndex = undefined;  
  7.         return true;  
  8.     } else {  
  9.         return false;  
  10.     }  
  11. }  
  12. //1.0单击单元格的时候触发  
  13. function onClickCell(index, field) {  
  14.     if (endEditing()) {  
  15.         $('#dg').datagrid('selectRow', index)  
  16.         .datagrid('editCell', { index: index, field: field });  
  17.         editIndex = index;  
  18.     }  
  19. }  
  20. //2.0根据类型选择条件进行查询  
  21. function doSearch() {  
  22.     //查询参数直接添加在queryParams中  
  23.     var searchName = $('#txtSearch').val();  
  24.     $('#dg').datagrid('reload', {//重新加载表信息datagrid  
  25.         strCondition: searchName  //参数名称  
  26.   
  27.     });  
  28. }  
  29.   
  30. //  根据类型选择条件进行查询  
  31. function entitySearch(entityId) {  
  32.     $('#dg').datagrid('reload', {//重新加载表信息datagrid  
  33.         strCondition: entityId  //参数名称  
  34.   
  35.     });  
  36. }  
  37.   
  38. //3.0向后台提交数据,包括添加和修改  
  39. function onAfterEdit(index, row,value) {  
  40.     var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields'));  
  41.   
  42.     var controlId = row.ControlId;  
  43.     //获得隐藏的GUID列  
  44.     var queryId = row.QueryId;  
  45.     var entityName = row.EntityName;  
  46.     //alert(entityName);  
  47.     var enQueryProperties = JSON.stringify(row);  
  48.   
  49.     if (row.IsShow == 'N' && row.isCondition == 'Y') {  
  50.         alert("查询条件必须显示到前台页面");  
  51.         //重新加载表格数据  
  52.         $('#dg').datagrid('reload');  
  53.   
  54.     }  
  55.     else {  
  56.         $.ajax(  
  57.                 {  
  58.                     url: '/Properties/updateQueryProperty',//向后台提交数据  
  59.                     type: "post",  
  60.                     async: true,  
  61.                     dataType: 'json',  
  62.                     data: { 'enQueryProperties': enQueryProperties'},                      
  63.                     success: function (data) {  
  64.                         if (data == 'true') {  
  65.                             alert("更新成功!");  
  66.                         }  
  67.   
  68.                     }  
  69.                 }  
  70.             );  
  71.     }  
  72.   
  73. }  
  74. //4.0添加按钮,同时怎加隐藏的GUID列  
  75. function append() {  
  76.     if (endEditing()) {  
  77.         $('#dg').datagrid('appendRow', { QueryId: '' });  
  78.         editIndex = $('#dg').datagrid('getRows').length - 1;  
  79.         $('#dg').datagrid('selectRow', editIndex)  
  80.                           .datagrid('beginEdit', editIndex);  
  81.   
  82.     }  
  83. }  
  84. //5.0删除事件  
  85. function remove() {  
  86.     if (editIndex == undefined) { return }  
  87.     $('#dg').datagrid('cancelEdit', editIndex)  
  88.                       .datagrid('deleteRow', editIndex);  
  89.     editIndex = undefined;  
  90. }  
  91. function accept() {      
  92.     $('#dg').datagrid('acceptChanges');     
  93. }  


           在这里,我们需要注意的是添加和修改后的保存事件,他们响应的是同一个js方法,那就是onAfterEdit,那么在向Controller提交的时候就会出现问题,如何根据让两个不同的后台方法响应同一个事件呢?请看Controller的代码。

    二、Controller页面

    首先,先看修改后更新的方法。
    

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public Boolean updateQueryProperty(string enQueryProperties)  
  2.    {  
  3.        #region 定义变量  
  4.        //获取查询属性实体的json  
  5.        string json = Request.Params["enQueryProperties"].ToString();  
  6.        //获取属性对应的控件实体  
  7.        string strControlId = Request.Params["ControlId"].ToString();  
  8.        //获取记录对应的ID  
  9.        string strQueryId = Request.Params["QueryId"].ToString();   
  10.        #endregion  
  11.  
  12.        #region 反序列化 查询属性实体的json 为属性实体  
  13.        //反序列化 查询属性实体的json 为属性实体  
  14.        QueryPropertiesViewModel enQueryPropertiesViewModel = (QueryPropertiesViewModel)JsonConvert.DeserializeObject(json, typeof(QueryPropertiesViewModel));   
  15.        #endregion  
  16.  
  17.        #region 取控件的值  
  18.        //根据控件ID查询控件实体  
  19.        ControlsViewModel enControlViewModel = controlWCF.QueryControlByID(strControlId);  
  20.   
  21.        //给属性实体的控件属性赋值  
  22.        //enQueryPropertiesViewModel.Controls = enControlViewModel;  
  23.        enQueryPropertiesViewModel.ControlsControlId = enControlViewModel.ControlId;  
  24.        #endregion 取控件的  
  25.   
  26.        //判断strQueryId如果有值,则为修改实体  
  27.        if (strQueryId != "")  
  28.        {     
  29.            //定义更新标签  
  30.            Boolean queryIsUpdate = false;  
  31.              
  32.            //调用wcf 接口,更新查询属性实体  
  33.            try  
  34.            {  
  35.                //更新查询属性实体  
  36.                queryIsUpdate = queryPropertiesServiceWCF.UpdateQueryProperties(enQueryPropertiesViewModel);  
  37.                if (!queryIsUpdate)  
  38.                {  
  39.                    Exception e = new Exception("更新失败!");  
  40.                }  
  41.                return true;  
  42.            }  
  43.            catch (Exception e)  
  44.            {  
  45.   
  46.                throw new Exception("更新失败!");  
  47.            }  
  48.   
  49.   
  50.        }else<span style="font-family: KaiTi_GB2312;">{</span>//如果strQueryId为空值,则说明是添加实体  
  51.            //为GUID主键赋值  
  52.            enQueryPropertiesViewModel.QueryId = Guid.NewGuid().ToString();  
  53.            AddEntity(enQueryPropertiesViewModel);//执行添加操作  
  54.            return true;  
  55.        }          
  56.    }  


             从上面更新的方法可以看出,updateQueryProperty接收的是编辑行整行的数据,我们区分是编辑单元格还是编辑新添加的一行可以通过判断该编辑行的主键列是否为空值来得到结论,如果主键是空值,那个编辑行一定是新添加的一列,如果主键有值,那就就是在原来的单元格上的修改操作。

    下面是添加的操作:

    

[csharp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public bool AddEntity(QueryPropertiesViewModel queryViewModel)  
  2. {  
  3.     bool addIdSuccess = false;  
  4.     try  
  5.     {  
  6.          queryViewModel.QueryId = Guid.NewGuid().ToString();  
  7.          queryViewModel.ControlHtmlId = queryViewModel.PropertyName;  
  8.          queryViewModel.ControlHtmlName = queryViewModel.PropertyName;  
  9.          if (queryPropertiesServiceWCF.AddQueryPropertiesy(queryViewModel)!=null)  
  10.          {  
  11.              addIdSuccess = true;  
  12.          }  
  13.     }  
  14.     catch (Exception e)  
  15.     {  
  16.           
  17.         throw e;  
  18.     }  
  19.     return addIdSuccess;  
  20. }  

    至此,便完美收工了。

    总结:对行内编辑的EasyUI一直很感兴趣,这次终于是接触到了,很开心;但是中间的添加和修改两个事件着实困惑了我好几天的时间,刚开始是理不清思路,因为我们的代码是从师姐那里拷过来的,就顺着她的思路往下走,把自己给绕了进去。后来还是打算自己从头把这里块知识理清楚,找来了行内编辑的EasyUI的Demo,用FireBug一步一步的调试,然后再某个即将入眠的晚上,终于茅塞顿开了。这还是说明那个问题,编程思路真的很重要,有了清晰的逻辑思路,你的工作就完成90%了。

原文地址:https://www.cnblogs.com/perrybalabala/p/5774552.html