java实现checkbox翻页选择(我照这个在ofbiz上也作了一个)

http://www.cnblogs.com/John-zhaohui/archive/2007/06/11/778685.html
java实现checkbox翻页选择(我照这个在ofbiz上也作了一个)

java实现checkbox翻页选择
  以前在网上看到jcheckbox翻页选择的话题很热,讨论也很多,这次由于项目需要,我用java实现了这个话题,在这里与大家共享.
  JSP页面:
   1,定义三个Hidden变量:
    <html:hidden name="form" property="all_selected"/>
    <html:hidden name="form" property="now_selected"/>
    <html:hidden name="form" property="no_selected"/>
   看到名字大家应该知道是做什么用的,在这里不多解释.
   2,javascript
  // 获取checkbox信息,选中,未选中,当前选中
            function getCheckBoxInformation() {
              var checkboxes = document.getElementsByName("checkbox");
              var checkedStr = "";
              var uncheckedStr = "";
              for(i = 0; i < checkboxes.length; i++) {
                  var checkbox = checkboxes[i];
                  if(checkbox.checked) {
                     checkedStr = checkedStr + "," + checkbox.value;
                 }else {
                     uncheckedStr = uncheckedStr + "," + checkbox.value;
                 }
             }
             document.forms[0].now_selected.value = checkedStr;
             document.forms[0].no_selected.value = uncheckedStr;
          }
         
           // 页面onload的时候计算当前页被选中项,并在页面表示
          function initPage() {
             var all_selected = document.forms[0].all_select.value;
             if(all_selected != "" && all_select ed!= null) {
                 var arrall_select = all_selected.split(",");
                 if(arrall_select.length > 0) {
                     for(k = 0; k < arrall_select.length; k++) {
                            for(i = 0; i < document.all.che_seq.length; i++) {
                                if(document.forms[0].checkbox[i].value == arrall_select[k]) {
                                 document.forms[0].checkbox[i].checked = true;  
                             }  
                         }
                          }  
                 }  
             } 
          }   
    每次翻页的时候调用getCheckBoxInformation()方法,页面加载的时候调用initPage()方法.
     3,后台Action操作,在每次执行时候调用下面的方法
  private void operationCheckInfo(Map map, Map form, HttpServletRequest request) {
          HttpSession session = request.getSession(false);
          //flag判断是否是翻页操作
          if (request.getParameter("flag") != null && request.getParameter("flag").equals("CHECKED")) {
              String all_select = (String) session.getAttribute("SESSION_CHECKBOX");
              if (all_select == null || all_select.equals("")) {
                  all_select = "";
              }
             //获取当前选中的项目
             String now_select = (String) form.get("now_select");
             String[] all_now_select = now_select.split(",");
             //获取当前未选中项目
             String no_select = (String)form.get("no_select");
             String[] all_now_no_select = no_select.split(",");
             //将当前选中项目加入列表
             for (int i = 1; i < all_now_select.length; i++) {
                 String strBoxSelected = all_now_select[i];
                 String strSearchWith = strBoxSelected  + ",";
                 if (all_select.indexOf(strSearchWith) == -1) {
                     all_select = all_select + strSearchWith;
                 }       
             }
             //将当前未选中项目从列表中删除
             for (int i = 1; i < all_now_no_select.length; i++) {
                 String strBoxNoselected = all_now_no_select[i];
                 String strSearchWith = strBoxNoselected + ",";
                 int iSearchIndex = all_select.indexOf(strSearchWith);                 if (iSearchIndex != -1) {
                     all_select = all_select.replaceAll(strSearchWith, "");
                 }                 }
             //将所有被选择项加入session
             session.setAttribute("SESSION_CHECKBOX", all_select);
             map.put("all_select", all_select);
         } else {
             //不是翻页的场合,将session清空
             session.removeAttribute("SESSION_CHECKBOX");
         }
     }
     到现在为止,checkbox的翻页选中已经完成,最后说一句的是,这次仅仅完成了任务,性能方面还有很大的缺陷,大家如果有好的想法或者建议,请与我留言.谢谢!

你可能感兴趣的:(JavaScript,java,jsp)