Easyui-editing datagrid: Batch save data 2. easyuidatagrid save
Next we will continue with the previous blog and save it in batches.
When we use updaterow to update the data in the datagrid, how can we get the data? Here we cannot use the convenient getchanges, but we can only get around one path. We chose the 10 thousand foolproof method, get all data rows in the datagrid through getRows of easyui.
IIAnd the second --- getRows
The solution is to get all the data in the datagrid, which is passed in json format.
How to get the row number of the datagrid: getRows, convert to json: JSON. stringify ("***");
<span style="font-size:18px;">//save all the dada from easyuiDatagridfunction updateDatagrid(num){var rows = $('#dg').datagrid('getRows');var entities = "";</span>
<Span style = "font-size: 18px;"> // loop the existing data in the datagrid and copy it to Entities row by row and convert it to json format </span>
<Span style = "font-size: 18px;"> // returns a set of objects in reverse sequence in the background. For (I = 0; I <rows. length; I ++) {entities = entities + JSON. stringify (rows [I]);} $. ajax ({url: getRootPath () + '/labor/update. do ', type: "post", async: true, dataType: 'json', data: {'entities': entities} success: function (data) {if (data. message = "operation successful! ") {Alert (data. message);} else {alert (data. message); return ;}$ ('# dg '). datagrid ('reload') ;}}) ;}</span>
Background reception: The json parsing tool used here is net. sf. json. Jsonarray and jsonobject are mainly used. It can be seen from the literal that one is responsible for data/sets and the other is responsible for a single object. Here we want to parse the array, so we use the tocollection method in jonsarray. There are many other methods in jsonarray. If you are interested, please take a look.
The specific code is as follows:
<span style="font-size:18px;">import net.sf.json.JSONArray;import net.sf.json.JSONObject;<span></span></span><pre name="code" class="java"><span>@Override</span>
@ RequestMapping ("update. do ")
@ ResponseBody
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
<span style="font-size:18px;"><span></span>public String update(HttpServletRequest request) {//1、 get the detail dataString ens = request.getParameter("entities");// 2、format the stringens = ens.replace("}{", "},{");ens = "[" + ens.toString() + "]";</span>
<Span style = "font-size: 18px;"> <span> </span> // 3. <span style = "font-family: Arial, Helvetica, sans-serif;"> transform to the object list </span> JSONArray array = JSONArray. fromObject (ens. toString (); @ SuppressWarnings ("unchecked") List <JobContentDetail> listDetail = (List <JobContentDetail>) JSONArray. toCollection (array, JobContentDetail. class); try {labourservice. saveData (listDetail);} catch (Exception e) {e . PrintStackTrace (); return createErrorMessage ("operation failed! "). ToString ();} return createSuccessMessage (" operation successful! "). ToString () ;}</span>
Iii. usage experience
Both edit and save; Edit and batch save; their solutions are the same, because the received data type of the easyui datagrid (and combobox) is json, therefore, as long as we can get the json data displayed on the front-end, everything will be solved and transmitted from the front-end to the backend. As long as the URL is correct, everything will go smoothly.