前端
效果图
点击多选框,将需要删除的数据选中,点击批量删除
ajax提交数组
数组提交,需要设置traditional : true,数组序列化
// 批量选择
$(".batchDel-th").click(function(){
var checks=$(".batchDel-check");
$.each(checks,function(i,input){
//状态反选
input.checked=input.checked==true?false:true;
});
});
// 批量删除
$('.batchDel-btn').click(function () {
var ids = [];
// 拿到当前被选中的数据
var checks = $('.batchDel-check:checked');
if (checks.length != null && checks.length > 0){
// 拿到被选中的数据的id
$.each(checks, function (i, check) {
var id = $(check).closest("tr").attr("data-id");
ids.push(id);
});
// $.each(selectedCategoryId, function (i, id) {
// alert(id);
// });
$.ajax({
url : "/category/batchDel.json",
type : "POST",
traditional : true,
data : {
ids : ids
},
success : function (result) {
loadCategoryList();
}
});
// ids = [];
}
});
后台
controller
// 批量删除分类
@RequestMapping("/batchDel.json")
@ResponseBody
public CommonReturnType batchDel(@RequestParam("ids") int[] ids){
categoryService.batchDelCategory(ids);
return CommonReturnType.success("success");
}
mapper
package com.lzy.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lzy.pojo.Category;
public interface CategoryMapper extends BaseMapper<Category> {
}
service
// 批量删除分类
@Override
public void batchDelCategory(int[] ids) {
if (null !=ids && ids.length > 0){
for (int id : ids){
categoryMapper.deleteById(id);
}
}
}