JavaScript implements table row sorting

Source: Internet
Author: User

Web site development in general to use the table to display the data, often according to a column to sort the row, the following is a row I see from the book example, see after the benefit, so share out.

Complete the code directly:

<!doctype html>//sorts the rows in the first tbody based on the value of the nth cell in each row of a specified table//If there is a comparator function then use it, otherwise compare in alphabetical orderfunctionsortrows (table,n,comparator) {varTbody = Table.tbodies[0]; varrows = Tbody.getelementsbytagname ("tr"); Rows= Array.prototype.slice.call (rows,0);//convert to Real array    //sort rows based on the values of nth <td> elementsRows.sort (function(row1,row2) {varCell1 = Row1.getelementsbytagname ("TD") [n]; varCell2 = Row2.getelementsbytagname ("TD") [n]; varVal1 = Cell1.textcontent | | Cell1.innertext;//IE compatible        varVal2 = Cell2.textcontent | |Cell2.innertext; if(Comparator)returnComparator (VAL1,VAL2); if(VAL1&LT;VAL2)return-1; Else if(VAL1&GT;VAL2)return1; Else return0;    });  for(vari=0;i<rows.length;i++) Tbody.appendchild (Rows[i]);//the original will be automatically removed}//find the <th> elements of the table (assuming only one row) so that they can be clicked so that the column is sorted by clicking on itfunctionmakesorttable (table) {varheaders = table.getelementsbytagname ("th");  for(vari=0;i){        (function(n) {//nested functions to create a local scope (this code does not quite understand itself, I tried to remove the nested function, but the result is wrong, I hope to understand this code of people also teach me)Headers[i].onclick =function() {sortrows (table,n);};    } (i)); }}window.onload=function(){    varTable = document.getElementById ("table");  Makesorttable (table); }</script><style type= "Text/css" >table{Table-Layout:auto; Border-Collapse:collapse;} th{border:solid 1px rgb (0,0,0); Cursor:pointer;} th:hover{background:red;} td{border:solid 1px rgb (0,0,0);}</style>

JavaScript implements table row sorting

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.