欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

layui数据表格:批量删除

程序员文章站 2022-07-02 20:34:39
...

前端页面

<form>
    <input type="button" value="批量删除" class="btn" style="width: 50px; height: 25px; cursor: pointer; margin-bottom: 1px;" onclick="delete()"/>
    <table id="demo" lay-filter="test"></table>
</form>
 
<script type="text/javascript">
function delete(){
	var checkStatus = table.checkStatus('demo');
	if(checkStatus.data.length==0){
		parent.layer.msg('请先选择要删除的数据行!', {icon: 2});
		return ;
	}
	var ids = "";
	for(var i=0;i<checkStatus.data.length;i++){
		ids += checkStatus.data[i].id+",";
	}
	parent.layer.msg('删除中...', {icon: 16,shade: 0.3,time:5000});
	$.post('url',
            {'ids':ids},
            function(data){
		        layer.closeAll('loading');
		        if(data.code==1){
			        parent.layer.msg('删除成功!', {icon: 1,time:2000,shade:0.2});
			        location.reload(true);
		        }else{
			        parent.layer.msg('删除失败!', {icon: 2,time:3000,shade:0.2});
		        }
	        }
    );
}
</script>

后端

@RequestMapping(value="/delete",produces="application/json;charset=utf-8")
@ResponseBody
public Object delete(User user,Map<String, Object> map){
	String ids = user.getIds();        //ids和id都存在于实体类User
	String a[] = ids.split(",");
	try{
		for (int i = 0; i < a.length; i++) {
			String id = a[i];
			userService.deleteById(id);
			map.put("code", 1);
		}
	}catch (Exception e) {
		e.printStackTrace();
		map.put("code", -1);
	}
	return JSON.toJSONString(map);
		
}

 转载自:https://blog.csdn.net/weixin_40106067/article/details/81387781?utm_source=blogxgwz2