java批量删除,前后台操作

来源:互联网 发布:阿里云cnd 编辑:程序博客网 时间:2024/06/08 21:49

 

方法一:

第一步:

<button id="del_model" type='button' class='button' onMouseOver="this.className='button_over';" onMouseOut="this.className='button';">
        
         <img src="${pageContext.request.contextPath}/ui/images/button/shanchu.png" border='0' align='absmiddle'>&nbsp;删除</button>       

 <input type="checkbox" id="allChk">全选</td>
       <input type="checkbox" id="subChk" name="subChk" value="${sysUserGroup.id}"/> 
 

第二步:js

<script type="text/javascript">
$(document).ready(function(){
 //全选全不选
 $("#allChk").click(function(){
  alert("全选");
  $("input[name='subChk']").attr("checked",this.checked);
  });

 //
 // 单选
 var subChk = $("input[name='subChk']")
 subChk.click(function() {
 $("#allChk").attr("checked", subChk.length == subChk.filter(":checked").length ? true:false);
 });

 /* 批量删除 */
 $("#del_model").click(function() {
 // 判断是否至少选择一项
 var checkedNum = $("input[name='subChk']:checked").length;
 if(checkedNum == 0) {
 alert("请选择至少一项!");
 return;
 }
 // 批量选择
 if(confirm("确定要删除所选项目?")) {
 var checkedList = new Array();
 $("input[name='subChk']:checked").each(function() {
 checkedList.push($(this).val());
 alert("checkedList: "+checkedList);//已经用,号分隔了
 });
 $.ajax({
 type: "POST",
 url: "sys/sysUserGroupAction_delete.do",
 data: {'ids':checkedList.toString()},
  success: function(result) {
   // alert("回调成功"+result);
 $("[name ='subChk']:checkbox").attr("checked", false);
 window.location.reload();
 }
 });
 }
 });
 //});

  
  
 
 
});
  
 </script>

第三部:

/**
  * 删除部门信息
  *
  * @return
  */

 public String delete() {
  // 获取部门的ids
  String idInfo = request.getParameter("ids");
  String[] idsArr = idInfo.split(",");
     
  
  Integer[] ids=new Integer[idsArr.length];//这一步是将string类型的数组转换为int类型的数组
  for(int i=0;i<ids.length;i++){   
   ids[i]=Integer.parseInt(idsArr[i]);}
  
  
  List<SysUserGroup> sysUserGroup=null;
  sysUserGroup=sysUserGroupService.delete(ids);
//  for (String id : idsArr) {
//    sysUserGroup=sysUserGroupService.delete(ids);
//   
//  }
  Gson gson=new Gson();
  String json = gson.toJson(sysUserGroup);//转换为json数据
  PrintWriter out=null;
  try {
   out = response.getWriter();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  out.println(json);
  out.flush();
  out.close();

  // 删除

  return null;
 }

 

 

 

 

 

 

 

第二中方法

第一步

<button  type='button' class='button' onMouseOver="this.className='button_over';" onMouseOut="this.className='button';" 
         onClick="document.forms[1].submit();">

 

<s:form name="form2" method="post" action="sys/sysUserGroupAction_delete.do" namespace="/sys">

        <input type="checkbox" id="checkall" name="checkall" value="" class="checkbox" onClick="checkAll()"></td>

        <s:checkbox  name="ids" cssClass="checkbox" fieldValue="%{#sysUserGroup.id}" onclick="changeCheckCount();"/>

第二步

<script type="text/javascript">
   function changeCheckCount(){
       var count=0;
    $("input[type='checkbox'][name='ids']").each(function(index,data){
            if(this.checked){
             count++; 
            }
    });
       if(count== $("input[type='checkbox'][name='ids']").length){
        $("#checkall").attr("checked","checked");
       }else{
        $("#checkall").attr("checked",null);
       }
   }
  
   function  checkAll(){
      if($("#checkall")[0].checked){
       $("input[type='checkbox'][name='ids']").attr("checked","checked");
      }else{
       $("input[type='checkbox'][name='ids']").attr("checked",null);
      }
   }
 </script>

第三步:后台操作,。。。。。。。。

0 0