CRUD(32):删除-批量删除
一、给全部删除按钮添加一个id;
<button class="btn btn-danger" id="emp_delete_all_btn">删除</button>
二、把EmployeeController中的删除方法,单个删除批量删除二合一
// 删除-单个删除/批量删除 二合一
@RequestMapping(value = "/emp/{empId}", method = RequestMethod.DELETE)
@ResponseBody
public Msg deleteEmp(@PathVariable("empId") String ids) {
if (ids.contains("-")) {
List<Integer> del_ids = new ArrayList<>();
String[] str_ids = ids.split("-");
for(String string:str_ids) {
del_ids.add(Integer.parseInt(string));
}
//组装id的集合
employeeService.deleteBatch(del_ids);
} else {
Integer id = Integer.parseInt(ids);
employeeService.deleteEmp(id);
}
return Msg.success();
}
三、在EmployeeService中添加批量删除的方法;
//删除-批量删除员工
public void deleteBatch(List<Integer> ids) {
EmployeeExample example = new EmployeeExample();
Criteria criteria = example.createCriteria();
criteria.andEmpIdIn(ids);
employeeMapper.deleteByExample(example);
}
ps.
在这里我把所有用户名称中的"-"去掉了,看着心烦。
UPDATE `tbl_emp`
SET emp_name = REPLACE(emp_name,'-','')
WHERE emp_name LIKE '%-%';
四、组装员工id的字符串
var del_idstr = "";
del_idstr += $(this).parents("tr").find("td:eq(1)").text()+"-";
//删除del_idstr中最后一个多余的-号
del_idstr = del_idstr.substring(0,del_idstr.length-1);
二、给批量删除按钮绑定单击事件
//删除-批量删除
$("#emp_delete_all_btn").click(function(){
var empNames = "";
var del_idstr = "";
//找到被选中的每一个check_item
$.each($(".check_item:checked"),function(){
empNames +=
$(this).parents("tr").find("td:eq(2)").text()+",";
del_idstr +=
$(this).parents("tr").find("td:eq(1)").text()+"-";
});
//删除empNames中最后一个多余的,号
empNames = empNames.substring(0,empNames.length-1);
//删除del_idstr中最后一个多余的-号
del_idstr = del_idstr.substring(0,del_idstr.length-1);
if (confirm("确认删除【" + empNames + "】吗?")) {
//确认,发送ajax请求
$.ajax({
url : "${APP_PATH}/emp/" + del_idstr,
type : "DELETE",
success : function(result) {
//回到本页
to_page(currentPage);
alert(result.msg);
}
});
}
});