批量刪除功能的實現
其實實現這個功能還是挺簡單的 因為我這是直接拼接的,所以用了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類型
需要先把第一個位置和最后一個位置上的雙引號去掉;