JS實現(xiàn)點擊表頭表格自動排序(含數(shù)字、字符串、日期)
更新時間:2017年01月22日 15:14:23 投稿:daisy
這篇文章主要介紹了利用JS如何實現(xiàn)點擊表頭后表格自動排序,其中包含數(shù)字排序、字符串排序以及日期格式的排序,文中給出了完整的示例代碼,相信對大家具有一定的參考價值,感興趣的朋友們一起來看看吧。
效果圖如下:
Demo演示地址:點擊這里
主要的JS代碼如下:
var tbody = document.querySelector('#tableSort').tBodies[0]; var th = document.querySelector('#tableSort').tHead.rows[0].cells; var td = tbody.rows; for (var i = 0;i < th.length;i++){ th[i].flag = 1; th[i].onclick = function(){ sort(this.getAttribute('data-type'),this.flag,this.cellIndex); this.flag = -this.flag; }; }; function sort(str,flag,n){ var arr = []; //存放DOM for (var i = 0;i < td.length;i++){ arr.push(td[i]); }; //排序 arr.sort(function(a,b){ return method(str,a.cells[n].innerHTML,b.cells[n].innerHTML) * flag; }); //添加 for (var i = 0;i < arr.length;i++){ tbody.appendChild(arr[i]); }; }; //排序方法 function method(str,a,b){ switch (str){ case 'num': //數(shù)字排序 return a-b; break; case 'string': //字符串排序 return a.localeCompare(b); break; default: //日期排序,IE8下'2012-12-12'這種格式無法設置時間,替換成'/' return new Date(a.split('-').join('/')).getTime()-new Date(b.split('-').join('/')).getTime(); }; };
完整實例代碼
<!DOCTYPE> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS實現(xiàn)點擊表頭表格自動排序(含數(shù)字、字符串、日期)</title> <style>#tableSort{moz-user-select: -moz-none;-moz-user-select: none;-o-user-select:none;-khtml-user-select:none;-webkit-user-select:none;-ms-user-select:none;user-select:none;border-collapse: collapse; border-spacing: 0; margin: 0; padding: 0;width: 100%;text-align: center;margin:15px 0;} #tableSort th{cursor: pointer; background: #eee} #tableSort tr:nth-child(even){background: #f9f9f9} #tableSort th,#tableSort td{padding: 10px; border:1px solid #ccc;} </style> </head> <body> <table id="tableSort"> <thead> <tr> <th data-type="num">工號</th> <th data-type="string">姓名</th> <th data-type="string">性別</th> <th data-type="date">時間</th> </tr> </thead> <tbody> <tr> <td>07</td> <td>aaaa</td> <td>男</td> <td>2012-12-12</td> </tr> <tr> <td>03</td> <td>mmmm</td> <td>女</td> <td>2013-12-16</td> </tr> <tr> <td>01</td> <td>cccc</td> <td>男</td> <td>2014-12-12</td> </tr> <tr> <td>04</td> <td>ffff</td> <td>女</td> <td>2015-12-12</td> </tr> <tr> <td>02</td> <td>bbbb</td> <td>男</td> <td>2016-12-18</td> </tr> <tr> <td>06</td> <td>ssss</td> <td>女</td> <td>2008-10-07</td> </tr> <tr> <td>05</td> <td>tttt</td> <td>男</td> <td>2012-07-22</td> </tr> </tbody> </table> <script> ;(function(){ var tbody = document.querySelector('#tableSort').tBodies[0]; var th = document.querySelector('#tableSort').tHead.rows[0].cells; var td = tbody.rows; for(var i = 0;i < th.length;i++){ th[i].flag = 1; th[i].onclick = function(){ sort(this.getAttribute('data-type'),this.flag,this.cellIndex); this.flag = -this.flag; }; }; function sort(str,flag,n){ var arr = []; for(var i = 0;i < td.length;i++){ arr.push(td[i]); }; arr.sort(function(a,b){ return method(str,a.cells[n].innerHTML,b.cells[n].innerHTML) * flag; }); for(var i = 0;i < arr.length;i++){ tbody.appendChild(arr[i]); }; }; function method(str,a,b){ switch(str){ case 'num': return a-b; break; case 'string': return a.localeCompare(b); break; default: return new Date(a.split('-').join('/')).getTime()-new Date(b.split('-').join('/')).getTime(); }; }; })(); </script> </body> </html>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
networkInformation.downlink測用戶網(wǎng)速方法詳解
這篇文章主要為大家介紹了networkInformation.downlink測用戶網(wǎng)速方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05PHP中如何unicode編碼,在JavaScript中h如何解碼
PHP中如何unicode編碼,在JavaScript中如何解碼?js中h這樣的,怎么轉(zhuǎn)碼?2023-07-07Three.js+React實現(xiàn)帶火焰效果的艾爾登法環(huán)
《艾爾登法環(huán)》是最近比較火的一款游戲,觀察可以發(fā)現(xiàn)它的?Logo?是由幾個圓弧和線段構(gòu)成。本文使用?React?+?Three.js?技術(shù)棧,實現(xiàn)具有火焰效果艾爾登法環(huán)?Logo,感興趣的可以了解一下2022-03-03JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之檢索算法示例【二分查找法、計算重復次數(shù)】
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之檢索算法,結(jié)合實例形式分析了二分查找法、計算重復次數(shù)相關(guān)算法原理與使用技巧,需要的朋友可以參考下2019-02-02JS+HTML5實現(xiàn)的前端購物車功能插件實例【附demo源碼下載】
這篇文章主要介紹了JS+HTML5實現(xiàn)的前端購物車功能插件,結(jié)合完整實例形式分析了JS結(jié)合HTML5的storage特性存儲數(shù)據(jù)實現(xiàn)購物車功能的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-10-10