好久沒上博客園寫文章了,最近看了些關於高性能JS開發的書籍,對於Grid排序這塊自我感覺理解的還算不錯,可以拿上來讓JS高手們指導下!也可以讓JS的新手們了解下!在代碼上有何不妥之處歡迎大家拍磚!感激不盡!
在這里我主要是封裝了Grid排序的實體功能,為了讓在各種項目版本中都可以直接使用!以下為JS詳細代碼以及注釋!
1 /* 2 * @數據表格排序 3 * @CodeBy:MrCo 4 * @Date:2013/3/15 5 * @Mail:co.mr.co@gmail.com 6 */ 7 (function (w) { 8 9 /* 10 * @驗證在window對象中是否已經存在gridSort對象 11 */ 12 if (w.gridSort) { 13 return new Error('系統中以存在gridSort類'); 14 } 15 16 /* 17 * @創建排序的構造函數 18 * @GID string 需要支持排序的table唯一ID,只能是ID 19 */ 20 var GridSort = function (GID) { 21 this.Grid = document.getElementById(GID); 22 this.Gbody = this.Grid.tBodies[0]; 23 this.Ghead = this.Grid.tHead; 24 } 25 26 /* 27 * @公共的靜態函數 28 * @主要是做Grid中字符串類型轉換並返回新類型值,以便排序使用 29 * @val string 需要轉換的字符串值 30 * @valType string 需要轉換的類型格式,目前接收int、float、date,默認返回string型 31 */ 32 GridSort._StringByConvert = function (val, valType) { 33 if (!valType) return val.toString(); 34 switch (valType.toLowerCase()) { 35 case 'int': 36 return parseInt(val); 37 case 'float': 38 return parseFloat(val); 39 case 'date': 40 return new Date(Date.parse(val)); 41 default: 42 return val.toString(); 43 } 44 } 45 46 /* 47 * @Grid支持排序的核心方法 48 * @colIdx int 數據表(table)中td的列索引 49 * @colType string 數據表(table)中td的列類型 50 */ 51 GridSort.prototype._Sequence = function (colIdx, colType) { 52 //這里相信JS的高手們一下就可以看出來這是一個匿名方法體 53 //這個方法體在這里不會執行,它會return到調用_Sequence()的函數上去執行 54 //在這里這個匿名方法體是屬於Array的Sort()函數的參數 55 //細心的童鞋可能主要到了arguments 56 //我們將這兩個參數看做為A跟B,接下來我們來看看這兩個參數是如何比較的 57 //譯: 58 //若 a 小於 b,在排序后的數組中 a 應該出現在 b 之前,則返回一個小於 0 的值。 59 //若 a 等於 b,則返回 0。 60 //若 a 大於 b,則返回一個大於 0 的值。 61 return (function(){ 62 var _rowPrevVal = GridSort._StringByConvert(arguments[0].cells[colIdx].firstChild.nodeValue, colType), //這個相當於A參數 63 _rowAfterVal = GridSort._StringByConvert(arguments[1].cells[colIdx].firstChild.nodeValue, colType); //這個相當於B參數 64 if (_rowPrevVal < _rowAfterVal) 65 return -1; 66 else if (_rowPrevVal > _rowAfterVal) 67 return 1; 68 else 69 return 0; 70 }); 71 } 72 73 /* 74 * @Grid列頭點擊事件的處理方法 75 * @該方法為處理排序的核心方法,包含了Table內置函數的使用,以及文檔碎片的使用 76 * @通過此方法可以比一般處理DOM來排序的性能高很多 77 */ 78 GridSort.prototype.BindClickHeadSort = function () { 79 var _rowsHead = this.Ghead.rows[0].cells, 80 _gbody = this.Gbody, 81 _gridRows = _gbody.rows, 82 _girdSort = this._Sequence; 83 84 //為每個Grid列頭綁定一個Click的點擊事件,這樣比直接在dom上添加onclick更簡潔 85 for (var i = 0, count = _rowsHead.length; i < count; i++) { 86 87 //注意這里,這里為了避免閉包的影響使用了匿名函數 88 (function (idx) { 89 _rowsHead[idx].onclick = function () { 90 var _sortRows = [], 91 _sortType = this.getAttribute('stype'), 92 _orderby = _gbody.getAttribute('orderby'); 93 94 //首先將Grid中的Row Copy到一個空數組中,以便之后排序 95 for (var i = 0, count = _gridRows.length; i < count; i++) { 96 _sortRows[i] = _gridRows[i]; 97 } 98 99 //這里的_orderby是我們自己設置的屬性,為了區分是降序還是升序 100 if (!_orderby) { 101 //開始執行Array的Sort()函數,可能很多童鞋都還米有看見過Sort()函數中加參數的用法 102 //不了解Sort()函數參數的童鞋,請馬上跳到_Sequence()函數那里繼續看吧,那里我會解釋 103 _sortRows.sort(_girdSort(idx, _sortType)); 104 _gbody.setAttribute('orderby', 'asc'); 105 } else { 106 _sortRows.reverse(); 107 _gbody.removeAttribute('orderby'); 108 } 109 110 //這里創建文檔碎片,然后通過上面已排好序的Rows從新添加到文檔碎片中 111 //使用文檔碎片的好處是,避免了瀏覽器的繪制過程,加快了頁面響應速度 112 var _newRows = document.createDocumentFragment(); 113 for (var j = 0, count2 = _sortRows.length; j < count2; j++) { 114 _newRows.appendChild(_sortRows[j]); 115 } 116 117 //最后一次性的加載到了Grid的內部 118 _gbody.appendChild(_newRows); 119 } 120 })(i); 121 } 122 } 123 124 /* 125 * 將構建好的GridSort類的指針指向window的gridSort 126 * 這樣做的目的是為了封裝,這樣外部就只能通過widnow.gridSort()來訪問GridSort類了 127 * 這樣就起到了對GridSort類的保護作用 128 */ 129 w.gridSort = (function (gid) { new GridSort(gid).BindClickHeadSort(); }); 130 })(window);
1 <table width="400" id="tab"> 2 <thead> 3 <tr> 4 <th width="100">First Name</th> 5 <th width="100">Last Name</th> 6 <th width="100" stype="date">Date</th> 7 <th width="100" stype="int">Int</th> 8 </tr> 9 </thead> 10 <tbody> 11 <tr> 12 <td>Kingwell</td> 13 <td>Leng</td> 14 <td>3/10/2012</td> 15 <td>1</td> 16 </tr> 17 <tr> 18 <td>1aJim</td> 19 <td>Green</td> 20 <td>3/5/2012</td> 21 <td>2</td> 22 </tr> 23 <tr> 24 <td>Kate</td> 25 <td>Bin</td> 26 <td>1/2/2012</td> 27 <td>3</td> 28 </tr> 29 <tr> 30 <td>Zte</td> 31 <td>Ri</td> 32 <td>5/3/2012</td> 33 <td>33</td> 34 </tr> 35 <tr> 36 <td>Bee</td> 37 <td>Dd</td> 38 <td>8/1/2012</td> 39 <td>15</td> 40 </tr> 41 </tbody> 42 </table> 43 <script type="text/javascript"> 44 gridSort('tab'); 45 </script>
Demo下載地址:http://files.cnblogs.com/keke/GridSort.rar