非常強大的table根據表頭排序,點擊表頭名稱,對其內容排序


js代碼:

 1     /**
 2      * 通過表頭對表列進行排序
 3      *
 4      * @param sTableID
 5      *            要處理的表ID<table id=''>
 6      * @param iCol
 7      *            字段列id eg: 0 1 2 3 ...
 8      * @param sDataType
 9      *            該字段數據類型 int,float,date 缺省情況下當字符串處理
10      */
11     function  orderByName(sTableID, iCol, sDataType) {
12          var  oTable = document.getElementById(sTableID);
13          var  oTBody = oTable.tBodies[0];
14          var  colDataRows = oTBody.rows;
15          var  aTRs =  new  Array;
16          for  (  var  i = 0; i < colDataRows.length; i++) {
17             aTRs[i] = colDataRows[i];
18         }
19          if  (oTable.sortCol == iCol) {
20             aTRs.reverse();
21         }  else  {
22             aTRs.sort(generateCompareTRs(iCol, sDataType));
23         }
24          var  oFragment = document.createDocumentFragment();
25          for  (  var  j = 0; j < aTRs.length; j++) {
26             oFragment.appendChild(aTRs[j]);
27         }
28         oTBody.appendChild(oFragment);
29         oTable.sortCol = iCol;
30     }
31     
32     
33     /**
34      * 處理排序的字段類型
35      *
36      * @param sValue
37      *            字段值 默認為字符類型即比較ASCII碼
38      * @param sDataType
39      *            字段類型 對於date只支持格式為mm/dd/yyyy或mmmm dd,yyyy(January 12,2004)
40      * @return
41      */
42     function  convert(sValue, sDataType) {
43          switch  (sDataType) {
44          case   "int" :
45              return  parseInt(sValue);
46          case   "float" :
47              return  parseFloat(sValue);
48          case   "date" :
49              return   new  Date(Date.parse(sValue));
50          default :
51              return  sValue.toString();
52         }
53     }
54     
55     
56     /**
57      * 比較函數生成器
58      *
59      * @param iCol
60      *            數據行數
61      * @param sDataType
62      *            該行的數據類型
63      * @return
64      */
65     function  generateCompareTRs(iCol, sDataType) {
66          return   function  compareTRs(oTR1, oTR2) {
67             vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue, sDataType);
68             vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue, sDataType);
69              if  (vValue1 < vValue2) {
70                  return  -1;
71             }  else   if  (vValue1 > vValue2) {
72                  return  1;
73             }  else  {
74                  return  0;
75             }
76         };
77     }

在table表中需要排序的列頭添加事件,調用orderByName(sTableID, iCol, sDataType)即可.

三個參數第一個是表的id,第二個參數是第幾列,第三個參數是類型,不寫即為字符型.

注意列頭如果不參與排序,放在另一個table中即可.

jsp部分示例代碼:

              <table width="100%" border="0" cellpadding="0" cellspacing="0" class="import_tab" align="center">
                            <tbody>
                                <tr>
                                <td width="11%" class="borderTh" title="查詢名稱為空即其沒有查詢結果">名稱(查詢)</td>
                                <td width="6%" class="borderTh" title="經營狀態為空表示其沒有查詢結果">經營狀態</td>
                                <td width="11%" class="borderTh" title="點擊可排序;Ctrl+F查找" onclick="orderByName('resultTab',2);">名稱(所填企業名)</td>
                                <td width="10%" class="borderTh" >統一社會信用代碼</td>
                                <td width="7%" class="borderTh" title="點擊可排序" onclick="orderByName('resultTab',4,Date);">申請日期</td>
                                </tr>
                            </tbody>    
                        </table>        
                        <table id="resultTab" width="100%" border="0" cellpadding="0" cellspacing="0" class="import_tab" align="center">        
                            <tbody>
                            <c:forEach items="${listItems}" var="wgl"  varStatus="num">
                                <tr> 
                                <td width="11%" class="borderTh" >${wgl.entname}</td>
                                <td width="6%" class="borderTh">${wgl.entstatus}</td>
                                <td width="11%" class="borderTh">${wgl.userName}</td>
                                <td width="10%" class="borderTh">${wgl.appCertificateNum}</td>
                                <td width="7%" class="borderTh"><fmt:formatDate value="${wgl.appDate}" type="date" dateStyle="long" /></td>
                                </tr>
                            </c:forEach>
                            </tbody>
                        </table> 

如 日期排序的效果,再點擊即可轉換順序排序展示


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM