JS實(shí)現(xiàn)點(diǎn)擊表頭表格自動(dòng)排序(含數(shù)字、字符串、日期)
效果圖如下:
Demo演示地址:點(diǎn)擊這里
主要的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'這種格式無法設(shè)置時(shí)間,替換成'/' return new Date(a.split('-').join('/')).getTime()-new Date(b.split('-').join('/')).getTime(); }; };
完整實(shí)例代碼
<!DOCTYPE> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS實(shí)現(xiàn)點(diǎn)擊表頭表格自動(dòng)排序(含數(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">工號(hào)</th> <th data-type="string">姓名</th> <th data-type="string">性別</th> <th data-type="date">時(shí)間</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)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
js設(shè)置cookie過期及清除瀏覽器對應(yīng)名稱的cookie
js設(shè)置cookie過期也就相當(dāng)于清除瀏覽器對應(yīng)名稱的cookie,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-10-10JS動(dòng)畫效果打開、關(guān)閉層的實(shí)現(xiàn)方法
這篇文章主要介紹了JS動(dòng)畫效果打開、關(guān)閉層的實(shí)現(xiàn)方法,可實(shí)現(xiàn)js控制層從中心位置打開與關(guān)閉的功能,涉及javascript操作頁面元素的相關(guān)技巧,需要的朋友可以參考下2015-05-05networkInformation.downlink測用戶網(wǎng)速方法詳解
這篇文章主要為大家介紹了networkInformation.downlink測用戶網(wǎng)速方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05PHP中如何unicode編碼,在JavaScript中h如何解碼
PHP中如何unicode編碼,在JavaScript中如何解碼?js中h這樣的,怎么轉(zhuǎn)碼?2023-07-07Three.js+React實(shí)現(xiàn)帶火焰效果的艾爾登法環(huán)
《艾爾登法環(huán)》是最近比較火的一款游戲,觀察可以發(fā)現(xiàn)它的?Logo?是由幾個(gè)圓弧和線段構(gòu)成。本文使用?React?+?Three.js?技術(shù)棧,實(shí)現(xiàn)具有火焰效果艾爾登法環(huán)?Logo,感興趣的可以了解一下2022-03-03JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之檢索算法示例【二分查找法、計(jì)算重復(fù)次數(shù)】
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之檢索算法,結(jié)合實(shí)例形式分析了二分查找法、計(jì)算重復(fù)次數(shù)相關(guān)算法原理與使用技巧,需要的朋友可以參考下2019-02-02JS+HTML5實(shí)現(xiàn)的前端購物車功能插件實(shí)例【附demo源碼下載】
這篇文章主要介紹了JS+HTML5實(shí)現(xiàn)的前端購物車功能插件,結(jié)合完整實(shí)例形式分析了JS結(jié)合HTML5的storage特性存儲(chǔ)數(shù)據(jù)實(shí)現(xiàn)購物車功能的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-10-10