需求:
2016年12月2号,今天碰到一个问题就是如何选中jsp页面的多条数据,进行批量处理。
效果:
实现思路:
jsp页面
<%@ page contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@ taglib uri="/WEB-INF/cvicse-common.tld" prefix="common"%> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/cvicse-param.tld" prefix="param"%> <%@ page import="com.cvicse.jr.lbms.la.bo.RentManagementQueryObject" %> <%@ page import="com.cvicse.jr.lbms.la.bo.Project" %> <%@page import="com.cvicse.jr.lbms.cs.util.RentPlanUtil"%> <% response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); String path=request.getContextPath(); %> <script language="JavaScript" src="<%=path%>/js/ajaxbase.js"></script> <html:html> <head> <title>供应商租金批量核销</title> <common:theme /> <Script language="javaScript"> function toQuery(){ document.getElementById("operAtt").value = "querySupplierList"; document.forms[0].submit(); } //求和计算 function sum(){ var result1=0.0;//利息和 var result2=0.0;//租金和 var result3=0.0;//本金和 var judge = 0; var check_array=document.getElementsByName("checkBoxId"); for(var i=0;i<check_array.length;i++) { if(check_array[i].checked==true) { var num = check_array[i].value.split("#"); var num1 = num[4].replace(/,/g,"");//利息 var num2 = num[6].replace(/,/g,"");//租金 var num3 = num[7].replace(/,/g,"");//本金 result1=parseFloat(result1)+parseFloat(num1); result2=parseFloat(result2)+parseFloat(num2); result3=parseFloat(result3)+parseFloat(num3); judge = judge + 1; } } re = /(\d{1,3})(?=(\d{3})+(?:$|\.))/g; if(judge==0){ alert("请选择需要核销的期次!"); return false; } if(!confirm("总计核销租金:"+result2.toFixed(2).replace(re, "$1,")+",本金"+result3.toFixed(2).replace(re, "$1,")+",利息"+result1.toFixed(2).replace(re, "$1,")+"。是否确认?")){return;}; batchPay(); } function toBack(){ //window.location.href="supplierRentManagementAction.do?operAtt=query"; //window.location.href="javascript:history.go(-2)"; with (document.forms[0]) { operAtt.value="query"; isFirst.value="no"; //action = "supplierRentManagementAction.do"; submit(); } } function selectAll(sign) { var multiUserBox = document.forms[0].elements("checkBoxId"); if (multiUserBox != null) { if (typeof (multiUserBox.length) != "undefined") { for ( var i = 0; i < multiUserBox.length; i++) { var judge = multiUserBox[i].disabled; if(judge==false){ multiUserBox[i].checked = sign; } } } else { multiUserBox.checked = sign; } } } </Script> </head> <body> <common:form action="/supplierRentManagementAction.do"> <table class="border" align="center"> <TR> <TD><html:errors /></TD> </TR> <tr> <td class="pagehead">批量核销列表</td> </tr> <tr> <td> <table width="100%" class="grid" align="center"> <tr> <th></th> <th>序号</th> <th>承租人名称</th> <th>项目编号</th> <th>项目名称</th> <logic:notEmpty name="supplierRentManagementForm" property="footer.dataArray"> <tbody id="playList1jc"> <logic:iterate id="rentManagement" property="footer.dataArray" name="supplierRentManagementForm" indexId="indexId"> <% int i = indexId.intValue()+1; String realRetdayStr="realRetdayStr_" + i + "_"; String contractId="contract_" + i + "_"; String collectBank="collectBank_" + i + "_"; String collectAccount="collectAccount_" + i + "_"; String targetsName = collectAccount + ",#,#," +collectBank ; %> <tr> <logic:equal name="rentManagement" property="permbal.retflg" value="1"> <td style="text-align: center;"> <input type="checkbox" name="checkBoxId" id="checkBoxId" disabled="disabled" value="<bean:write name="rentManagement" property="id" />"/> </td> </logic:equal> </tbody> </logic:notEmpty> </tr> <tr> <td>全选<input type="checkbox" name="checkProjectId" id="checkProjectId" οnclick="selectAll(checked)" /></td> </tr> </table> </td> </tr> <tr align="center"> <td colspan=14 style="text-align:center"> <INPUT type="button" οnclick="sum()" value="核销确认"> <INPUT type="button" οnclick="toBack()" value="返回"> </td> </tr> </table> </common:form> <script> function batchPay(){ //window.location.href="supplierRentManagementAction.do?operAtt=query"; //if(!confirm("是否确定结清?结清后将不能恢复 !")){return;}; document.getElementById("operAtt").value = "batchPay"; document.getElementById("isFirst").value = "no"; document.forms[0].submit(); } function advanceRet(prjid,objectNo,ywId,grantId){ var url = "/cs/supplierRentManagementAction.do?operAtt=supAdvanceRet&projectId="+prjid+"&objectNo="+objectNo+"&ywId="+ywId+"&grantId="+grantId; openModalDialog(url,'800px','600px'); //document.getElementById("operAtt").value = "query"; document.forms[0].submit(); } function supRet(prjid,objectNo){ var url = "/tools/rentPlanCompanyQueryAction.do?operAtt=queryPlanSup&projectId="+prjid+"&objectNo="+objectNo; openModalDialog(url,'800px','600px'); //document.forms[0].submit(); } </script> </body> </html:html> java类中实现方法: public ActionForward batchPay(ActionMapping mapping, ActionForm aform, HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, Exception { //获得checkbox带来的value String[] checkProjectId=form.getCheckBoxId(); //获得页面上还款账户名称 String[] collectAccount = form.getCollectAccount1(); //获得页面上还款银行名称 String[] collectBank = form.getCollectBank1(); //或的页面实际还款时间 Date[] realDate = form.getRealDate(); //业务编号数组 String[] ywbh = new String[checkProjectId.length]; //realId编号数组 String[] realid = new String[checkProjectId.length]; //项目编号数组 String[] prjno = new String[checkProjectId.length]; //期次数组 Map map = new HashMap(); int[] retnum = new int[checkProjectId.length]; //次数 int i=0; //改变获得时间的的格式 SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd"); String a = ds.format(realDate[0]); for(int k=0;k<checkProjectId.length;k++){ String[] part = checkProjectId[k].split("#"); }