This article shares the JavaScript implementation of HTML table sorting function for your reference. The specific content is as follows
HTML code:
<table cellpadding="0" id="table"> <tr> <td>click me</td> <td>click me</td> <td>click me</td> <td>click me</td> <td>click me</td> <td>click me</td> </tr> <tr> <td> <span id="bfn_la_bac.usa">15.43</span> </td> <td>700</td> <td>1.220</td> <td>A</td> </tr> <tr> <td> <span id="bfn_la_c.usa">7.05</span> </td> <td>4</td> <td>3,000</td> <td>Bing</td> </tr> <tr> <td> <span id="bfn_la_jpm.usa">30.62</span> </td> <td>30</td> <td>2,558,800</td> <td>and</td> </tr> <tr> <td> <span id="bfn_la_axp.usa">22.30</span> </td> <td>5</td> <td>6</td> <td>blind</td> </tr> <tr> <td> <span id="bfn_la_mrk.usa">26.31</span> </td> <td>0.6</td> <td>5</td> <td>-</td> </tr> <tr> <td> <span id="bfn_la_mrk.usa">26.31</span> </td> <td>0.6</td> <td>5</td> <td>-</td> </tr> <tr> <td> <span id="bfn_la_pg.usa">63.16</span> </td> <td>7</td> <td>4</td> <td>Sub</td> </tr> </table>
JavaScipt code:
var tableSort = function(){ this.initialize.apply(this,arguments); } tableSort.prototype = { initialize : function(tableId,clickRow,startRow,endRow,classUp,classDown,selectClass){ this.Table = document.getElementById(tableId); this.rows = this.Table.rows;//All rows this.Tags = this.rows[clickRow-1].cells;//Tags td this.up = classUp; this.down = classDown; this.startRow = startRow; this.selectClass = selectClass; this.endRow = (endRow == 999? this.rows.length : endRow); this.T2Arr = this._td2Array();//The two-dimensional array of all affected tds is this.setShow(); }, //Set the tag switch setShow:function(){ var defaultClass = this.Tags[0].className; for(var Tag,i=0;Tag = this.Tags[i];i++){ Tag.index = i; addEventListener(Tag ,'click', Bind(Tag,statu)); } var _this =this; var turn = 0; function statu(){ for(var i=0;i<_this.Tags.length;i++){ _this.Tags[i].className = defaultClass; } if(turn==0){ addClass(this,_this.down) _this.startArray(0,this.index); turn=1; }else{ addClass(this,_this.up) _this.startArray(1,this.index); turn=0; } } }, //Set the selected column style colClassSet:function(num,cla){ //Get the associated td for(var i= (this.startRow-1);i<(this.endRow);i++){ for(var n=0;n<this.rows[i].cells.length;n++){ removeClass(this.rows[i].cells[n],cla); } addClass(this.rows[i].cells[num],cla); } }, //Start sort num according to which column aord reverse order or order startArray:function(aord,num){ var afterSort = this.sortMethod(this.T2Arr,aord,num);//Paste the sorted two-dimensional array into the sorting method to go to this.array2Td(num,afterSort);//Output}, //Convert the affected rows and columns into a two-dimensional array_td2Array:function(){ var arr=[]; for(var i=(this.startRow-1),l=0;i<(this.endRow);i++,l++){ arr[l]=[]; for(var n=0;n<this.rows[i].cells.length;n++){ arr[l].push(this.rows[i].cells[n].innerHTML); } } return arr; }, //Output the innerHTML of the corresponding rows and columns based on the sorted two-dimensional array array2Td:function(num,arr){ this.colClassSet(num,this.selectClass); for(var i= (this.startRow-1),l=0;i<(this.endRow);i++,l++){ for(var n=0;n<this.Tags.length;n++){ this.rows[i].cells[n].innerHTML = arr[l][n]; } } }, //Pass a two-dimensional array, sort according to the w items in the child items of the two-dimensional array, and then return the sorted two-dimensional array sortMethod:function(arr,aord,w){ arr.sort(function(a,b){ x = killHTML(a[w]); y = killHTML(b[w]); x = x.replace(/,/g,''); y = y.replace(/,/g,''); switch (isNaN(x)){ case false: return Number(x) - Number(y); break; case true: return x.localeCompare(y); break; } }); arr = aord==0?arr:arr.reverse(); return arr; } } /*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- RegExp('(/s|^)'+className+'(/s|$)'); return element.className.match(reg); } function addClass(element, className) { if (!this.hasClass(element, className)) { element.className += " "+className; } } function removeClass(element, className) { if (hasClass(element, className)) { var reg = new RegExp('(/s|^)'+className+'(/s|$)'); element.className = element.className.replace(reg,' '); } } var Bind = function(object, fun) { return function() { return fun.apply(object, arguments); } } //Remove all html tags function killHTML(str){ return str.replace(/<[^>]+>/g,""); } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ = new tableSort('table',1,2,999,'up','down','hov');The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.