Sort tables using JavaScript

Source: Internet
Author: User

Sort tables using JavaScript

Table sorter includes JavaScript and a bit of CSS, which enables the original html table to be sorted by data values in each column.

Effect: click on any column in the header. The following rows are sorted in ascending order by the column value.
Click the column again in the order determined by the string comparison, and change it to descending order. Click other columns, and then re-sort by the values of other columns. Note that when sorting, the background color of the column's even-odd line must be white and even-odd. The original html cannot be changed. Only JavaScript and css files can be added. No class libraries can be used. Only native DOM APIJavaScript must be modularized, the JS call entry must follow the figure below:



JS Code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KCjxwcmUgY2xhc3M9 "brush: java;"> "Use strict"; window. onload = function () {var tables = getAllTables (); makeAllTablesSortalbe (tables) ;}; // use the following two rows for embedding .. // var tables = getAllTables (); // makeAllTablesSortalbe (tables); function getAllTables () {return document. getElementsByTagName ("table");} function makeAllTablesSortalbe (tables) {for (var I = 0; I <tables. length; I ++) makeSort (tables [I]);} // makes the list sorted. function makeSort (table) {var th = Table. getElementsByTagName ("th"); for (var I = 0; I <th. length; I ++) {// bind button event th [I]. onclick = function () {var index = 0; changeStyle (th, this); // locate the index value for (var j = 0; j <th. length; j ++) {if (this = th [j]) index = j;} sortByTh (table, index, this. className) ;}}// change the style function changeStyle (th, t) {for (var I = 0; I <th. length; I ++) {if (th [I] = t) {if (th [I]. className. indexOf ("descen D ")! =-1) th [I]. className = th [I]. className. replace ("descend", "ascend"); else if (th [I]. className. indexOf ("ascend ")! =-1) th [I]. className = th [I]. className. replace ("ascend", "descend"); else th [I]. className + = "descend";} else {th [I]. className = th [I]. className. replace ("descend", ""); th [I]. className = th [I]. className. replace ("ascend", "") ;}}// sorting function sortByTh (table, index, className) {var action = className. indexOf ("descend ")! =-1? "Descend": "ascend"; var array = []; for (var I = 1; I <table. getElementsByTagName ("tr "). length; I ++) {array [I-1] = table. getElementsByTagName ("tr") [I];} array. sort (function (a, B) {// ascending if (action = 'scend') {return. cells [index]. innerHTML <= B. cells [index]. innerHTML;} else {// return a in descending order. cells [index]. innerHTML> = B. cells [index]. innerHTML ;}}); for (var I = 0; I <array. length; I ++) table. getElementsByTagName ("tbody") [0]. appendChild (array [I]);}

CSS:

    border: 1px solid gray;    margin: 0;    padding: 3px;    border-collapse: collapse;}tr:nth-child(even),tr:hover {    background-color: #DEDEDE;}th {    text-align: left;    background-color: #041A7F;    color: white;    font-weight:  bold;    padding-right:16px;}.ascend, .descend {    background-color: #A4B0FC;    background-position: right;    background-repeat: no-repeat;}.ascend {    background-image: url("ascend.png");}.descend {    background-image: url("descend.png");}

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.