批量删除功能的实现
其实实现这个功能还是挺简单的 因为我这是直接拼接的,所以用了DOM方法来获取id
话不多说直接上代码
首先是复选框全选和反选
这里的话
获取最上面一个复选框的状态同步到拼接的复选框
$("#check11").click(function () { var hz = $("#check11").prop("checked"); $(".qx").prop('checked',hz); });
直接看checkbox就好
1 $.ajax({ 2 url: "/reader", 3 type: "get", 4 dataType: "json", 5 success:function (result) { 6 var dataTR = ""; 7 $.each(result, function (index, value) { 8 dataTR += 9 "<tr>" + 10 "<td>" + value.readerId + "</td>" + 11 "<td>" + value.name + "</td>" + 12 "<td>" + value.sex + "</td>" + 13 "<td>" + value.birth + "</td>" + 14 "<td>" + value.address + "</td>" + 15 "<td>" + value.telcode + "</td>" + 16 "<td>" + 17 "<input type='checkbox' class='qx'> " + 18 "</td>" + 19 "</tr>"; 20 }); 21 $("#tbody").html(dataTR); 22 } 23 });
下面是ajax请求
1 $("#delete").click(function () { 2 var zhi = $('input:checkbox:checked'); 3 var id = ''; 4 var str = ''; 5 $.each(zhi,function(){ 6 id = this.parentNode.parentNode.firstChild.innerHTML; 7 if(id!=null){ 8 str += id+","; 9 } 10 swal({ 11 title:"确定要删除这"+zhi.length+'个数据吗', 12 showCancelButton: true, 13 confirmButtonColor: "#DD6B55", 14 confirmButtonText: "确定!", 15 cancelButtonText: "取消!", 16 closeOnConfirm: false, 17 closeOnCancel: false 18 },function (isConfirm) { 19 var s = JSON.stringify(str); 20 console.log(s); 21 if(isConfirm){ 22 $.ajax({ 23 url:"/reader1", 24 type:"post", 25 dataType: "json", 26 data:{"str":s}, 27 success:function (result) { 28 if(result=="success"){ 29 swal('删除成功','','success'); 30 getAll(); 31 }else { 32 swal("取消!", "停留在此页面", "info"); 33 } 34 } 35 }); 36 }else { 37 swal("取消!", ", "info"); 38 } 39 }); 40 }); 41 });
这里注意var定义一定要放在each方法的外部并且要给赋值’ ';不然的话JSON转换成字符串的时候会
为空
dao层
int deleteByPrimaryKey(Integer readerId);
Mapper
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from reader_info where reader_id = #{readerId,jdbcType=INTEGER} </delete>
service层
int deleteByPrimaryKey(Integer readerId);
impl
1 package gentleman.service; 2 3 import gentleman.bean.User; 4 import gentleman.dao.Userdao; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import java.util.List; 10 11 @Service 12 @Transactional 13 public class serviceimpl implements UserService { 14 15 @Autowired 16 private Userdao userdao; 17 18 @Override 19 public void deleteUserById(int id) throws Exception { 20 userdao.deleteUserById(id); 21 } 22 }
Controller层
1 package gentleman.Controller; 2 3 import com.alibaba.fastjson.JSON; 4 import gentleman.service.reader_infoservice; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 10 @Controller 11 public class CountDown { 12 @Autowired 13 private reader_infoservice reader; 14 15 @RequestMapping("/reader1") 16 @ResponseBody 17 public String deleteByPrimaryKey(String str){ 18 String substring = str.substring(1,str.length()-1); 19 String[] split = substring.split(","); 20 try { 21 for (int i = 0; i < split.length; i++) { 22 reader.deleteByPrimaryKey(Integer.parseInt(split[i])); 23 } 24 return JSON.toJSONString("success"); 25 } catch (NumberFormatException e) { 26 e.printStackTrace(); 27 return JSON.toJSONString("fail"); 28 } 29 } 30 }
这里需要注意的就是JSON传过来的字符串是"id,id,id"的形式,如果想转成int类型
需要先把第一个位置和最后一个位置上的双引号去掉;