jsp代码
var rows =$('#findAllRolestable').datagrid('getSelections');
var result = JSON.stringify(rows);
if(rows){
//去除两边的中括号
result=result.substring(1,result.length-1)
//alert(result)
$.ajax({
url: '<%=basePath%>user/addRoleAllRoles.do?user_no='+user_no,
data:{'datas':result}, //直接传_list节可以了,相当于 data: { "ids[0]":1,"ids[1]":2 }这种写法
dataType: "json",
type: "POST",
success: function (data) {
$.messager.show({
title:'提示',
msg: data.msg,
showType:'show'
});
$('#findAllRoles').dialog('close');
$('#roleAllocationtable').datagrid('reload');
}
});
}else{
$.messager.show({
title:'提示',
msg: '至少选择一条',
showType:'show'
});
}
后台代码====
/**
* 分配角色
* @param list
* @return
*/
@RequestMapping("/addRoleAllRoles.do")
@ResponseBody
public Map addRoleAllRoles(@RequestParam("datas")String datas,String user_no ) {
Map<String,Object>map=new HashMap<String,Object>();
System.out.println("添加角色user_no===="+user_no+"--datas==="+datas);
//List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr);
try {
//添加角色
Integer row=userService.addRoleAllRoles(datas,user_no);
map.put("msg", "添加成功");
} catch (Exception e) {
map.put("msg", "添加成失败");
e.printStackTrace();
}
return map;
}
实现类代码===
@Override
@Transactional
public Integer addRoleAllRoles(String datas, String user_no) {
System.out.println("serviceimpl--user_no=="+user_no+"--datas==="+datas);
Integer row=0;
try {
//添加角色之前先删除已有角色
int now=userDao.deleteRoleAllRoles(user_no);
if(null!=datas&&!"".equals(datas)) {
String data="["+datas+"]";
List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONObject.parse(data);
System.out.println("listObjectFir=="+listObjectFir);
for(Map<String,String> strmap:listObjectFir) {
String REMARK= strmap.get("REMARK");
System.out.println("REMARK=="+REMARK);
//添加角色
row=userDao.addRoleAllRoles(REMARK,user_no);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return row;
}