其实实现这个功能还是挺简单的 因为我这是直接拼接的,所以用了DOM方法来获取id
话不多说直接上代码
首先是复选框全选和反选
这里的话
获取最上面一个复选框的状态同步到拼接的复选框
$("#check11").click(function () {
var hz = $("#check11").prop("checked");
$(".qx").prop('checked',hz);
});
直接看checkbox就好
$.ajax({
url: "/reader",
type: "get",
dataType: "json",
success:function (result) {
var dataTR = "";
$.each(result, function (index, value) {
dataTR +=
"<tr>" +
"<td>" + value.readerId + "</td>" +
"<td>" + value.name + "</td>" +
"<td>" + value.sex + "</td>" +
"<td>" + value.birth + "</td>" +
"<td>" + value.address + "</td>" +
"<td>" + value.telcode + "</td>" +
"<td>" +
"<input type='checkbox' class='qx'> " +
"</td>" +
"</tr>";
});
$("#tbody").html(dataTR);
}
});
下面是ajax请求
$("#delete").click(function () {
var zhi = $('input:checkbox:checked');
var id = '';
var str = '';
$.each(zhi,function(){
id = this.parentNode.parentNode.firstChild.innerHTML;
if(id!=null){
str += id+",";
}
swal({
title:"确定要删除这"+zhi.length+'个数据吗',
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "确定!",
cancelButtonText: "取消!",
closeOnConfirm: false,
closeOnCancel: false
},function (isConfirm) {
var s = JSON.stringify(str);
console.log(s);
if(isConfirm){
$.ajax({
url:"/reader1",
type:"post",
dataType: "json",
data:{"str":s},
success:function (result) {
if(result=="success"){
swal('删除成功','','success');
getAll();
}else {
swal("取消!", "停留在此页面", "info");
}
}
});
}else {
swal("取消!", ", "info");
}
});
});
});
这里注意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
package gentleman.service;
import gentleman.bean.User;
import gentleman.dao.Userdao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class serviceimpl implements UserService {
@Autowired
private Userdao userdao;
@Override
public void deleteUserById(int id) throws Exception {
userdao.deleteUserById(id);
}
}
Controller层
package gentleman.Controller;
import com.alibaba.fastjson.JSON;
import gentleman.service.reader_infoservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CountDown {
@Autowired
private reader_infoservice reader;
@RequestMapping("/reader1")
@ResponseBody
public String deleteByPrimaryKey(String str){
String substring = str.substring(1,str.length()-1);
String[] split = substring.split(",");
try {
for (int i = 0; i < split.length; i++) {
reader.deleteByPrimaryKey(Integer.parseInt(split[i]));
}
return JSON.toJSONString("success");
} catch (NumberFormatException e) {
e.printStackTrace();
return JSON.toJSONString("fail");
}
}
}
这里需要注意的就是JSON传过来的字符串是"id,id,id"的形式,如果想转成int类型
需要先把第一个位置和最后一个位置上的双引号去掉;