通常情况下我们会获取所选取行对象的ID,通过循环及简单封装拼凑成一个长String传送过去,并在Service层解释再通过findByID获取实例并update

但今次我们需要将整个对象群完">

Home  >  Article  >  Web Front-end  >  Method to pass Array object array in JS to the background in JSON format_javascript skills

Method to pass Array object array in JS to the background in JSON format_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:05:181023browse

The business needs to transfer the data of several selected rows in the front-end jQuery easyUI DataGrid list to the background for update operation
Method to pass Array object array in JS to the background in JSON format_javascript skills

Normally we will get the ID of the selected row object through a loop And simply encapsulate it into a long String and send it to the service layer, and then use findByID to get the instance and update

But this time we need to completely transmit the entire object group to the background

The structure is as follows
Method to pass Array object array in JS to the background in JSON format_javascript skills

Use Google's GSON plug-in and json2.js together

The front-end code is as follows, some simple verification codes have been removed for the sake of simplicity:

Copy code The code is as follows:

var rows = $('#dg1').datagrid('getSelections');
$.ajax({
cache : false,
type : "POST",
url : _basePath '/sectionGroup/pair',
data : {rows : JSON.stringify(rows ), group_id : group_id, group_name : group_name},
success : function(data) {
if(data.success == true){
$.messager.confirm('Configuration successful','whether Refresh the list? ', function(r){
if (r){
$('#dg').datagrid('reload');
$('#dg1').datagrid(' reload');
$('#dg2').datagrid('reload');
}
});
}else{
$.messager.show({
title:'Prompt',msg:'Configuration failed',
showType:'fade',style:{right:'',bottom:''}
});
}
}
});

This page needs to introduce json2.js

The background Controller receives the following:
Copy the code The code is as follows:

@RequestMapping(value = "/pair")
@ResponseBody
public ResponseData pair(String rows, String group_name , String group_id, HttpServletRequest request) {
User user = (User) SecurityContextUtil.getCurrentUser();
if (user == null) {
user = (User) request.getSession().getAttribute( Constants.USER_OS);
}
Gson gson = new Gson();
List list = gson.fromJson(rows, new TypeToken>() {}.getType ());
for (SectionGroup sectionGroup : list) {
sectionGroup.setRegion(user.getRegion_id());
sectionGroup.setCompany_id(user.getOrg_id());
sectionGroup.setGroup_id( group_id);
sectionGroup.setGroup_name(group_name);
service.insertEntity(sectionGroup);
}
return ResponseData.SUCCESS_NO_DATA;
}

where GSON And TypeToken is a class introduced in the GSON package

The input parameter rows should be of String type

After Gson conversion, the List is still a normal interface, and the complete Object object at the front end is loaded internally.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn