复选框全选全不选反选加批量删除
发布日期:2021-10-12 20:07:57 浏览次数:16 分类:技术文章

本文共 2586 字,大约阅读时间需要 8 分钟。

 先看图

一,全选,全不选 

1.如何给复选框设置id

这个是第一个复选框即全选全不选反选按钮
这是foreach循环中的复选框 这个里面要确定每条数据的id即id="${product.id }",还有class选择器ids,这条数据id对应的value

 2.再看js

//全选,全不选	    $("#qx").click(function () {	        if(this.checked){	            $(".ids").each(function () {	                $(this).prop("checked", true);	            })	        }else{	            $(".ids").each(function () {	                $(this).removeAttr("checked");	            })	        } 	    });	    //反选	     $(".ids").change(function () {	        $(".ids:checked").length == $(".ids").length ? $("#qx").prop("checked", true) : $("#qx").prop("checked", false);	    })

二, 如何批量删除呢:

//批量删除 function deleteAll(){	   根据test name属性得到选中的id,以字符串的形式拼接	    obj = document.getElementsByName("test");	    ids = [];	    for(k in obj){	        if(obj[k].checked)	            ids.push(obj[k].value);	    }	    if(ids==""){	    	layer.alert("请选择要删除的商品!!!");	    }else{	    	layer.confirm('请确认是否删除选中的商品', {		    	btn: ['删除','取消'] //按钮		    	}, function()		    	{		    		 $.post(	    			       "/user/system/deleteAll?ids="+ids,	    			       function(obj){	    			    	   if(obj=="200"){	    			    		   layer.alert('删除成功!', function(index){	    							   window.location.href="/user/system/productMangerList?status="+status;	    							 });	    			    	   }else{	    			    		   layer.alert("删除失败");	    			    	   }	    			       },	    			       "text"	    			    );		    	 });	     }	  }

3.后台如何接收呢

@Controller@RequestMapping(value = "user/system/")public class ProductController {        @Autowired	private IUserService userService;      /*	 * 批量删除	 */	@RequestMapping(value = "/deleteAll", method = RequestMethod.POST)	@ResponseBody	public String deleteAll (String ids){		return productService.deleteAll(ids);	}}

4.接口类

public interface IProductService {    String deleteAll(String ids);}

5,业务层

@Servicepublic class ProductServiceImpl implements IProductService{        @Autowired	private IProductMapper productMapper;	@Override	public String deleteAll(String ids) {		try {              //截取“,”通过for循环把数据根据id一个一个删除			String[] productId = ids.split(",");			int p =0;			for (int i = 0; i < productId.length; i++) {				 p =productMapper.deleteProduct(productId[i]);			  }			  if (p>0) {					log.info("商品删除成功!!!!!");					return "200";				}				} catch (Exception e) {		e.printStackTrace();		}		return "500";	}}

6.映射mapper.xml 接口

public interface IProductMapper {	int deleteProduct(String id);}

7.mapper.xml 写sql  这个的删除都是假删除,只是改变一下状态。

update t_sys_product set status = 3 ,updateTime = NOW() where id = #{id}

Over!!!

转载地址:https://blog.csdn.net/qq_39772439/article/details/90638105 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:消除eclipse中项目js文件出现的红叉(红叉不影响项目运行)
下一篇:SSM框架crud(增删改查模糊查询加分页)之模糊查询(三)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月13日 18时16分11秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章