JS to implement HTML table sorting function _javascript skill

Source: Internet
Author: User

The example of this article for everyone to share the JavaScript implementation of HTML table sorting function for your reference, the specific contents are as follows

HTML Code:

<table cellpadding= "0" id= "table" > <tr class= "Top" > <td>click me</td> <td>c
      Lick me</td> <td>click me</td> <td>click me</td> </tr> <tr> <td> <span id= "Bfn_la_bac.usa" >15.43</span> </td> <td class= "Red" >
      700</td> <td>1.220</td> <td class= "Red" > "</td> </tr> <tr> <td> <span id= "Bfn_la_c.usa" >7.05</span> </td> <td class= "Red" >4< /td> <td>3,000</td> <td class= "Red" > Bing </td> </tr> <tr> ;td> <span id= "Bfn_la_jpm.usa" >30.62</span> </td> <td class= "Red" >30</td > <td>2,558,800</td> <td class= "Red" > and </td> </tr> <tr> &L T;td> <span id= "Bfn_la_axp.usa" >22.30</span> </td> <td class= "Red" >5</td> &LT;TD&GT;6&L t;/td> <td class= "Red" > Blind </td> </tr> <tr> <td> <span id= "BF N_la_mrk.usa ">26.31</span> </td> <td class=" Red ">0.6</td> <td>5</td&
      Gt &LT;TD class= "Red" >-</td> </tr> <tr> <td> <span id= "Bfn_la_pg.usa" > 63.16</span> </td> <td class= "Red" >7</td> <td>4</td> &LT;TD cl
 ass= "Red" > Sub </td> </tr> </table>

javascirpt 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 lines this. 
    tags = this.rows[clickrow-1].cells;//label 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 this.setshow () of all affected TD; },//Set Tab Toggle 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 checked column style colclassset:function (NUM,CLA) {//Get 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 sorting num According to the sort Aord order startarray:function (aord,num) {var aftersort = This.sortmethod (this. T2arr,aord,num);//sorted two-dimensional array to the Sort method to This.array2td (num,aftersort);//output},//Convert affected rows and columns to two-dimensional arrays _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 
    InnerHTML array2td:function (Num,arr) {This.colclassset (num,this.selectclass) to output the corresponding rows and columns according to the sorted two-dimensional array; 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 in a two-dimensional array, sorted according to the W entry in the subkey 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; }/*-----------------------------------/function AddEventListener (O,TYPE,FN) {if (o.attachevent) {O.attacheve
  NT (' on ' +type,fn);
  }else if (o.addeventlistener) {O.addeventlistener (type,fn,false);
    }else{o[' on ' +type] = fn; 
  The function hasclass (element, className) {var reg = new RegExp (' (\s|^) ' +classname+ ' (\s|$) '); 
Return Element.className.match (REG); function addclass (element, ClassName) {if (!this.hasclass (element, ClassName)) {element.classname = "" +clas 
  sname; } function Removeclass (element, ClassName) {if (Hasclass (element, className)) {var reg = new RegExp (' (\s|^) ') 
    +classname+ ' (\s|$) '); 
  Element.classname = Element.className.replace (Reg, '); 
  The var Bind = function (object, fun) {return function () {return Fun.apply (object, arguments); 
}///Remove all HTML tag function killhtml (str) {return str.replace (/<[^>]+>/g, ""); //------------------------------------------------//tableid The first few lines are the label rows, sorted from the first few lines, the first lines ending the sort (999 for the last) ascending label style, descending label style Checked column style//note the label line class should be consistent with var Ex1 = new Tablesort (' table ', 1,2,999, ' Up ', ' Down ', ' HOV ');

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.