複選框跨頁多選實現js+struts2

轉載地址  http://blog.csdn.net/lxhjava/article/details/54633829


1.問題描述:查詢出的數據分頁顯示在jsp頁面table中,table的每一行都有複選框,用戶在當前頁勾選數據而且可以跳轉到任意頁勾選,且勾選的數據在翻頁時記住勾選。用戶勾選完數據,可以同時提交所選的數據。

2.需要解決的問題    1:記住之前頁所勾選的數據,返回之前頁可以查看到勾選的數據。2:對用戶在不同頁所勾選的數據需要點擊提交按鈕時同時獲取。

3.實現思想:根據上面描述的問題,我們可以在複選框上添加onclick事件(見updateCheckedIds),這個事件負責處理用戶對當前數據的操作(勾選/取消勾選),同時在jsp中還要有一個隱藏域來保存所有勾選的數據;onclick事件函數負責變更隱藏域中的值。這裏我獲取當前數據的id,再通過判斷是勾選,還是取消勾選,來決定是否添加到隱藏域中還是以空字符串替換之前已經勾選的id。在提交按鈕的onclick函數中來獲取隱藏域中的值。需要注意的是:前提是點擊上一頁,下一頁等翻頁時是提交表單的方式,這樣才能把當前頁所選的id(保存在隱藏域中),即隱藏域中的值提交到action。另外action返回到jsp頁面時需要兩個forEach循環(外層爲<c:forEach>用於顯示集合數據,裏層爲<c:forTokens>用於循環字符串,字符串中的id之間以“,”分隔;我在action做了切割,用的是兩個forEach)來判斷數據是否勾選過。我這裏的分頁功能做了封裝。

參考源代碼:

js函數實現:

[javascript] view plain copy
  1. <script type="text/javascript">  
  2.     function freeBound() {  
  3.      //1.獲得模板id,版本id,選擇的場景id  
  4.      var tempId=$('#templateId').val();  
  5.      var tempVerId=$('#tempVerId').val();  
  6.      var checkSceneIds =$('#checkedIds').val();   
  7.   //$('input[name="sceneIds"]:checked').each(function(){   
  8.   // checkSceneIds+=","+$(this).val();   
  9.   //});   
  10.   if(checkSceneIds.length==0){  
  11.    alert('請選擇需要解除的場景!');   
  12.    return;  
  13.   }else if(confirm('是否確認解除綁定?')){  
  14.    checkSceneIds=checkSceneIds.substr(1,checkSceneIds.length);  
  15.    var freeBoundUrl = "${pageContext.request.contextPath}/scene/freeBound.action";  
  16.    $.ajax({  
  17.     type:'post',  
  18.     url:freeBoundUrl,  
  19.     data:{"tempId":tempId,"tempVerId":tempVerId,"sceneIds":checkSceneIds},  
  20.     dataType:'json',  
  21.     success:function(data){  
  22.      alert('解除綁定成功!');  
  23.      location.href=location.href;  
  24.     },  
  25.     error:function(data){  
  26.      alert('操作失敗!');  
  27.     }  
  28.    })  
  29.   }  
  30.     }  
  31.       
  32.     //跨頁多選  
  33.     function updateCheckedIds(box){  
  34.      //獲得當前複選框的值  
  35.      var id=box.value;  
  36.      //歷史所選的id,多個id以","分隔  
  37.      var checkedIds=$('#checkedIds').val();  
  38.      if(box.checked){//勾選  
  39.       checkedIds+=","+id;//歷史用沒有則追加  
  40.      }else{//取消勾選  
  41.       checkedIds=checkedIds.replace(","+id, "");//id替換爲空字符串,<strong>注:此處寫法有誤,應該先split,再for循環查找並替換</strong>  
  42.      }  
  43.      $('#checkedIds').val(checkedIds);//最新的所選值保存到隱藏域中  
  44.     }  
  45. </script>  

[html] view plain copy
  1. //jsp部分源代碼如下,大部分都貼出來了比較多:  
  2.   
  3.          <input type="button" class="btn" style="width:80px;text-align:center" value="解除綁定" onclick="freeBound();" />  
  4. <form id="form1" action="querySceneByTempForFreeBound.action?templateId=${templateId}&tempVerId=${tempVerId}" style="padding:0; margin-top: 5px"  method="post" name="form1">  
  5.       
  6.     <input type="hidden" name="templateId" id="templateId" value="${templateId}"/>  
  7.     <input type="hidden" name="tempVerId" id="tempVerId" value="${tempVerId}"/>  
  8.     <input type="hidden" name="checkedIds" id="checkedIds" value="${checkedIds}"/>  
  9.       
  10.     <table border="0" cellpadding="0" cellspacing="0" style="margin-bottom:8px;table-layout:fixed;" id="table1" class="table datagrid">  
  11.         <thead>  
  12.         <tr>  
  13.          <th width="4%"></th>  
  14.             <th width="5%">序  號</th>  
  15.             <th width="5%" nowrap="nowrap">場景編號</th>  
  16.             <th width="10%" nowrap="nowrap">交易對象</th>  
  17.             <th width="10%" nowrap="nowrap">業務系統</th>  
  18.             <th width="30%" nowrap="nowrap">場景描述</th>  
  19.             <th width="10%" nowrap="nowrap">簽約環節</th>  
  20.             <th width="5%" nowrap="nowrap">場景狀態</th>  
  21.         </tr>  
  22.         </thead>  
  23.         <tbody>  
  24.         <c:forEach var="scene" items="${scenes}" varStatus="status">  
  25.             <tr style="text-align: left">  
  26.              <td align="center">  
  27.              <input type="checkbox" name="sceneIds" onclick="updateCheckedIds(this)" value="${scene.id}"  
  28.               <c:forEach var="sceneId" items="${ids}">  
  29.                 <c:if test="${sceneId==scene.id}">checked="checked"</c:if>   
  30.               </c:forEach>  
  31.              />  
  32.              </td>  
  33.                 <td align="center">${status.index+1 }</td>  
  34.                 <td>  
  35.                         ${scene.sceneCode}  
  36.                 </td>  
  37.                 <td style="text-align: left">  
  38.                         ${scene.transactionObjName}  
  39.                 </td>  
  40.                 <td style="text-align: left">  
  41.                         ${scene.busiSysNameStr}  
  42.                 </td>  
  43.                 <td title="${scene.sceneDesc}" style="overflow:hidden;text-overflow:ellipsis;" nowrap=false>  
  44.                         ${scene.sceneDesc}  
  45.                 </td>  
  46.                 <td style="text-align: left">  
  47.                         ${scene.signLinkName}  
  48.                 </td>  
  49.                 <td align="center">  
  50.                     <c:if test="${scene.sceneStatus==1 }">有效</c:if>  
  51.                     <c:if test="${scene.sceneStatus==2 }">無效</c:if>  
  52.                 </td>  
  53.             </tr>  
  54.         </c:forEach>  
  55.         </tbody>  
  56.     </table>  
  57.     <input type="hidden" id="busiSysName" value="${busiSysName}"/>  
  58.     <input type="hidden" id="serviceLife" value="${serviceLife}"/>  
  59.     <tw:page/>  
  60. </form>  
  61.   
  62.   
  63.   
  64.   
  65. //action中用於處理所選的id代碼如下:  
  66.   
  67.    String checkedIds=request.getParameter("checkedIds");//選中的場景id  
  68.   
  69.     if( checkedIds!=null){  
  70.          String[] ids=checkedIds.split(",");//切割  
  71.          request.setAttribute("ids",ids);  
  72.       }  
  73.    request.setAttribute("checkedIds",checkedIds);  

發佈了26 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章