关键代码
<input type="hidden" name="userId" value="${user.id}">
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px">
<input id="selall"
type="checkbox" class="icheckbox_square-blue"></th>
<th class="sorting_asc">ID</th>
<th class="sorting">角色名称</th>
<th class="sorting">角色描述</th>
</tr>
</thead>
<tbody>
<c:forEach items="${roles}" var="role">
<tr>
<td>
<input name="roleId" type="checkbox" value="${role.id}">
</td>
<td>${role.id}</td>
<td>${role.rolename}</td>
<td>${role.roledesc}</td>
</tr>
</c:forEach>
</tbody>
</table>
@RequestMapping("toAddRole.do")
public ModelAndView toAddRole(int id){
List<Role> roleList = roleService.findRoleByUserId(id);
ModelAndView mv = new ModelAndView();
mv.addObject("roles",roleList);
mv.setViewName("user-role-add");
return mv;
}
package com.zhongruan.bean;
public class Role {
private int id;
private String rolename;
private String roledesc;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public String getRoledesc() {
return roledesc;
}
public void setRoledesc(String roledesc) {
this.roledesc = roledesc;
}
@Override
public String toString() {
return "Role{" +
"id=" + id +
", rolename='" + rolename + '\'' +
", roledesc='" + roledesc + '\'' +
'}';
}
}
<select id="findRoleByUserId" parameterType="int" resultType="role">
select * from tb_role where id not in (select roleId from user_role where userId = #{id})
</select>
就能将数据库里没有的那部分人拿出来了。
作业部分(未实现)
function addRoles() {
var checkedNum=$("input[name='roleId']:checked").length;
alert(checkedNum);
if(checkedNum==0){
alert("请至少选择一个进行添加!!!");
return;
}
if(confirm("确认要添加这些用户吗?")){
var roleList=new Array();
var userList=new Array();
$("input[name='roleId']:checked").each(
function () {
roleList.push($(this).val())
}
);
$("input[name='userId']:checked").each(
function () {
userList.push($(this).val())
}
);
alert(roleList);
alert(userList);
$.ajax({
type:"post",
url: "${pageContext.request.contextPath}/user/addRoleAll.do",
data:{roleList:roleList.toString(),userList:userList.toString()},
success:function () {
alert("添加成功");
location.reload();
},
error:function () {
alert("删除失败");
}
});
}
}
@RequestMapping("addRoleAll.do")
public String addRoleAll(String roleList,String userList){
String[] strs1 = roleList.split(",");
String[] strs2 = userList.split(",");
List<Integer> ids=new ArrayList<>();
for(int i = 0;i<strs1.length;i++){
userService.addroleuser(strs1[i],strs2[i]);
}
return "redirect:findAll.do";
}
<insert id="add" parameterType="list">
insert into user_role (userId,roleId) values (#{userId},#{roleId})
</insert>
思路是靠ajax把数据拿过去装进数组里,然后逐个添加,然而根本没法运行今天晚上也有点事没法仔细研究,等明天老师解答了。