easyui-editing datagrid 批量儲存資料 一,easyuidatagrid儲存
這篇部落格是分享給大家使用easy使用者介面架構從介面向後台傳值的兩種方法。使用easyui 已經有一段時間,沒有很深入的學習過,只是停留在可以簡單使用的層面上,這次由於項目需要,前台介面的需求比較多,所以對easyui的使用,在這段時間,從量上,使用的深度上有了一個大的跨越,特別是datagrid 。然後回顧以前使用的一些經驗,整理了這篇部落格。
先看:我需要將表格中資料如紅色框中的資料,統一編輯修改之後,一起儲存。
這裡有兩種方法,咱們先來看第一種:
一、——使用getChanges
可以使用easyui 提供的便捷的方法getchanges();
getChanges:從上一次的提交擷取改變的所有行。型別參數指明用哪些類型改變的行,可以使用的值有:inserted,deleted,updated等。當型別參數未配置的時候返回所有改變的行。我們來看看具體是如何使用的。
<span style="white-space:pre"></span>function save(){ if ($('#dg').datagrid('getChanges').length) { var inserted = $('#dg').datagrid('getChanges', "inserted"); var updated = $('#dg').datagrid('getChanges', "updated");
<pre name="code" class="html"><span style="white-space:pre"></span>var deleted = $('#dg').datagrid('getChanges', "deleted"); // 統一放到一個json中傳遞 var effectRow = new Object(); if (inserted.length) { effectRow["inserted"] = JSON.stringify(inserted); } if (updated.length) { effectRow["updated"] = JSON.stringify(updated); }
<pre name="code" class="html"><span style="white-space:pre"></span>if (deleted.length) { effectRow["deleted"] = JSON.stringify(deleted); }
<span style="white-space:pre"></span>// $.post jquery中簡單的非同步提交,如果需要錯誤處理,需使用$.ajax.
$.post(getRootPath()+"/labour/update.do", effectRow, function(data) { if(data.success){ $('#dg').datagrid('acceptChanges');
<span style="white-space:pre"></span>$('#dg').datagrid('reload'); } }, "JSON").error(function() { alert('error'); }); }; }
背景接收: 從後台接收到對應的json ,可以做對應的增,刪,改操作。
<span style="white-space:pre"></span><pre name="code" class="java"><span>import net.sf.json.JSONArray;import net.sf.json.JSONObject;<span></span></span>
@RequestMapping("/update.do")@ResponseBodypublic Object save(HttpServletRequest request) {//擷取編輯資料 這裡擷取到的是json字串String inserted = request.getParameter("inserted");String updated = request.getParameter("updated");List<JobContentDetail> listUpdated = new ArrayList<JobContentDetail>();List<JobContentDetail> listInserted = new ArrayList<JobContentDetail>();if(inserted != null){//把json字串轉換成對象JSONArray jsonArr = JSONArray.fromObject(inserted); for (int i = 0; i < jsonArr.size(); i++) { listInserted.add((JobContentDetail)JSONObject.toBean(jsonArr.getJSONObject(i), JobContentDetail.class)); } try {labourservice.saveEntities(listInserted);} catch (Exception e) {e.printStackTrace();return createErrorMessage(e.getMessage()).toString();}}if(updated != null){ //把json字串轉換成對象JSONArray jsonArr = JSONArray.fromObject(updated); for (int i = 0; i < jsonArr.size(); i++) { listUpdated.add((JobContentDetail)JSONObject.toBean(jsonArr.getJSONObject(i), JobContentDetail.class)); } try {labourservice.saveEntities(listUpdated);} catch (Exception e) {e.printStackTrace();return createErrorMessage(e.getMessage()).toString();}}return createSuccessMessage("操作成功!").toString();}
第一種方法固然好,但是發現有一個問題就是,假如,我在datagrid中使用了“updaterow”方法,那麼,easyui的getchanges方法,是拿不到這些updateRow 改變的資料,如下所示:
function rowsave() { var drow = snl.datagrid('getSelected'); var index = snl.datagrid('getRowIndex', drow); snl.datagrid('updateRow', { index: index, row: { SeriesNumber: "電話號碼", SeriesName: "姓名" } }); }); 儲存資料的時候能正常儲存到表單裡面去,但是用:
var rows =snl.datagrid('getChanges'); 讀取資料,讀取到的rows為0
那麼怎麼辦,如何拿到表格中既有刪除,又有添加,還有被“updateRow”更新的資料呢?
下篇部落格繼續。