easyui-editing datagrid 批量保存数据 一

来源:互联网 发布:尔雅网络课程手机登录 编辑:程序博客网 时间:2024/04/29 18:56

 这篇博客是分享给大家使用easyUI 框架从界面向后台传值的两种方法。使用easyui 已经有一段时间,没有很深入的学习过,只是停留在可以简单使用的层面上,这次由于项目需要,前台界面的需求比较多,所以对easyui的使用,在这段时间,从量上,使用的深度上有了一个大的跨越,特别是datagrid 。然后回顾以前使用的一些经验,整理了这篇博客。   

     先看效果图:我需要将表格中数据如红色框中的数据,统一编辑修改之后,一起保存。

      

这里有两种方法,咱们先来看第一种:

       一、——使用getChanges

       可以使用easyui 提供的便捷的方法getchanges();

     getChanges:从上一次的提交获取改变的所有行。类型参数指明用哪些类型改变的行,可以使用的值有:inserted,deleted,updated等。当类型参数未配置的时候返回所有改变的行。我们来看看具体是如何使用的。

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">function save(){  
  2.             if ($('#dg').datagrid('getChanges').length) {  
  3.                 var inserted = $('#dg').datagrid('getChanges', "inserted");  
  4.                 var updated = $('#dg').datagrid('getChanges', "updated");</span>  
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="white-space:pre">       </span>var deleted = $('#dg').datagrid('getChanges', "deleted");<span style="background-color: rgb(255, 255, 255);"> </span><span style="background-color: rgb(255, 255, 255);">    </span></span>  
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">        </span>// 统一放到一个json中传递   
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">        </span>var effectRow = new Object();   
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">        </span>if (inserted.length)   
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">        </span>{   
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">            </span>effectRow["inserted"] = JSON.stringify(inserted);  
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">        </span> }   
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">        </span>if (updated.length) {   
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">            </span>effectRow["updated"] = JSON.stringify(updated);  
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">        </span> }  

if (deleted.length) {

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">                   effectRow["deleted"] = JSON.stringify(deleted);  
  2.                 }</span>  
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="white-space:pre">   </span>// $.post jquery中简单的异步提交,如果需要错误处理,需使用$.ajax.</span>  

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. $.post(getRootPath()+"/labour/update.do", effectRow, function(data)   
  2.     {   
  3.         if(data.success){   
  4.             $('#dg').datagrid('acceptChanges');  
  5.         $('#dg').datagrid('reload');  
  6.     } }, "JSON").error(function() { alert('error'); }); }; }  

      后台的接收: 从后台接收到对应的json ,可以做对应的增,删,改操作。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">import net.sf.json.JSONArray;  
  2. import net.sf.json.JSONObject;</span>  

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">@RequestMapping("/update.do")  
  2. @ResponseBody  
  3. public Object save(HttpServletRequest request) {  
  4.     //获取编辑数据 这里获取到的是json字符串  
  5.     String inserted = request.getParameter("inserted");  
  6.     String updated = request.getParameter("updated");  
  7.     List<JobContentDetail> listUpdated = new ArrayList<JobContentDetail>();  
  8.     List<JobContentDetail> listInserted = new ArrayList<JobContentDetail>();  
  9.     if(inserted != null){  
  10.     //把json字符串转换成对象  
  11.     JSONArray jsonArr = JSONArray.fromObject(inserted);   
  12.     for (int i = 0; i < jsonArr.size(); i++) {       listInserted.add((JobContentDetail)JSONObject.toBean(jsonArr.getJSONObject(i), JobContentDetail.class));   
  13.     }   
  14.     try {  
  15.     labourservice.saveEntities(listInserted);  
  16.     } catch (Exception e) {  
  17.         e.printStackTrace();  
  18.     return createErrorMessage(e.getMessage()).toString();  
  19.     }}  
  20.     if(updated != null){   
  21.     //把json字符串转换成对象  
  22.     JSONArray jsonArr = JSONArray.fromObject(updated);   
  23.     for (int i = 0; i < jsonArr.size(); i++) {       listUpdated.add((JobContentDetail)JSONObject.toBean(jsonArr.getJSONObject(i), JobContentDetail.class));  
  24.     }   
  25.     try {  
  26.         labourservice.saveEntities(listUpdated);  
  27.     } catch (Exception e) {  
  28.         e.printStackTrace();  
  29.         return createErrorMessage(e.getMessage()).toString();  
  30.     }}  
  31.     return createSuccessMessage("操作成功!").toString();  
  32.     }</span>  


     第一种方法固然好,但是发现有一个问题就是,假如,我在datagrid中使用了“updaterow”方法,那么,easyui的getchanges方法,是拿不到这些updateRow 改变的数据,如下所示:        

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">function rowsave() {  
  2.     var drow = snl.datagrid('getSelected');  
  3.     var index = snl.datagrid('getRowIndex', drow);  
  4.     snl.datagrid('updateRow', {  
  5.         index: index,  
  6.         row: {  
  7.             SeriesNumber: "电话号码",  
  8.             SeriesName: "姓名"  
  9.         }  
  10.     });  
  11.     });</span>  
   保存数据的时候能正常保存到表单里面去,但是用: var rows =snl.datagrid('getChanges'); 读取数据,读取到的rows为0

      那么怎么办,如何拿到表格中既有删除,又有添加,还有被“updateRow”更新的数据呢?  

     下篇博客继续。

   

版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0