Use JavaScript to sort simple table clicks

Source: Internet
Author: User
From: http://www.cnblogs.com/robot/archive/2008/04/20/1161801.html

A new bug was found: 10 is smaller than 2 because sort is sorted by ascii by default. This problem has been fixed.
Download the latest demo [fixed some known problems, and optimized some with replies from netizens Code , Usage is similar to the original]
HTML code [You can ignore it. It has nothing to do with HTML.]:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <title> k2046.tablesorter </title> <br/> <style type = "text/CSS"> <br/> body {font-size: 12px; line-Height: 25px ;}< br/> tr {Height: 25px ;}< br/> Th {font-size: 14px; border-bottom: solid 1px # dddddd; Background-color: # eeeeee ;}< br/> TD {font-size: 12px; text-align: center ;}< br/>. sortdesccss {background-image: URL (http://k2046.cn/tb/Desc.gif); background-repeat: No-Repeat; Background-position: Right Center;} <br/>. sortasccss {background-image: URL (http://k2046.cn/tb/Asc.gif); background-repeat: No-Repeat; Background-position: Right Center ;} <br/> </style> <br/> <script language = "JavaScript" type = "text/JavaScript" src = "http://files.cnblogs.com /Robot/tablesorter. JS "> </SCRIPT> <br/> </pead> <br/> <body> <br/> code: New tablesorter (" tb1 "); <br/> effect: Click any header to perform sorting. <br/> <Table border = "0" style = "border: solid 1px # dddddddd; width: 360px; "cellpadding =" 0 "cellspacing =" 0 "id =" tb1 "> <br/> <tr> <br/> <TH> NO. </Th> <br/> <TH> name </Th> <br/> <TH> employee ID </Th> <br/> <TH> position </Th> <br/> <TH> gender </Th> <br/> <TH> Performance </Th> <br/> <TH> report time </Th> <br/> </tr> <br/> <tr> <br/> <TD> 1 </TD> <Br/> <TD> Andy Lau </TD> <br/> <TD> 8008 </TD> <br/> <TD> President </TD> <br/> <TD> male </TD> <br/> <TD> 98 </TD> <br/> <TD> 2008-04-12 </TD> <br/> </tr> <br/> <tr> <br/> <TD> 2 </TD> <br/> <TD> Wu qilong </TD> <br/> <TD> 8004 </TD> <br/> <TD> supervisor </TD> <br/> <TD> male </TD> <br/> <TD> 80 </TD> <br/> <TD> 2008-04-16 </TD> <br/> </tr> <br/> <TD> 3 </TD> <br/> <TD> Wang Guangliang </TD> <br/> <TD> 8003 </TD> <br/> <TD> Manager </TD> <br/> <TD> male </TD> <br/> <TD> 85 </TD> <br/> <TD> 2008 -04-15 </TD> <br/> </tr> <br/> <TD> 4 </TD> <br/> <TD> Zhang Xueyou </TD> <br/> <TD> 8009 </TD> <br/> <TD> Vice President </TD> <br/> <TD> male </TD> <br/> <TD> 90 </TD> <br/> <TD> 2008-04-11 </TD> <br/> </tr> <br/> <tr> <br/> <TD> 5 </TD> <br/> <TD> Cecilia Cheung </TD> <br/> <TD> 8005 </TD> <br /> <TD> assistant </TD> <br/> <TD> female </TD> <br/> <TD> 78 </TD> <br/> <TD> 2008-04-13 </TD> <br/> </tr> <br/> <TD> 6 </TD> <br/> <TD> * ** </TD> <br/> <TD> 8001 </TD> <br/> <TD> Director </TD> <Br/> <TD> male </TD> <br/> <TD> 60 </TD> <br/> <TD> 2008-04-18 </TD> <br/> </tr> <br/> <TD> 7 </TD> <br/> <TD> Chen Huilin </TD> <br/> <TD> 8002 </TD> <br/> <TD> trial period </TD> <br/> <TD> female </TD> <br/> <TD> 85 </TD> <br/> <TD> 2008-04-18 </TD> <br/> </tr> <br/> <TD> 8 </TD> <br/> <TD> Zhang mamayu </TD> <br/> <TD> 8007 </TD> <br/> <TD> Senior Manager </TD> <br/> <TD> female </TD> <br/> <TD> 82 </TD> <br/> <TD> 2008-04-16 </TD> <br/> </tr> <br/> <TD> 9 </TD> <br /> <TD> Zhou runfa </TD> <br/> <TD> 8006 </TD> <br/> <TD> Vice President </TD> <br/> <TD> male </TD> <br/> <TD> 88 </TD> <br/> <TD> 2008-04-13 </TD> <br/> </tr> <br /> </table> <br/> code: new tablesorter ("tb2", 0, 2, 5, 6); <br/> effect: Click columns 0, 2, 5, and 6 in the header to perform sorting. <br/> <Table border = "0" style = "border: solid 1px # dddddddd; width: 360px; "cellpadding =" 0 "cellspacing =" 0 "id =" tb2 "> <br/> <tr> <br/> <TH> NO. </Th> <br/> <TH> name </Th> <br/> <TH> employee ID </T H> <br/> <TH> position </Th> <br/> <TH> gender </Th> <br/> <TH> Performance </Th> <br /> <TH> report time </Th> <br/> </tr> <br/> <TD> 1 </TD> <br /> <TD> Andy Lau </TD> <br/> <TD> 8008 </TD> <br/> <TD> President </TD> <br/> <TD> male </TD> <br/> <TD> 98 </TD> <br/> <TD> 2008-04-12 </TD> <br/> </tr> <br /> <tr> <br/> <TD> 2 </TD> <br/> <TD> Wu qilong </TD> <br/> <TD> 8004 </TD> <br/> <TD> supervisor </TD> <br/> <TD> male </TD> <br/> <TD> 80 </TD> <br/> <TD> 2008-04-16 </TD> <br/> </tr> <br/> <TD> 3 </TD> <br/> <TD> Wang Guangliang </TD> <br/> <TD> 8003 </TD> <br/> <TD> Manager </ TD> <br/> <TD> male </TD> <br/> <TD> 85 </TD> <br/> <TD> 2008-04-15 </TD> <br /> </tr> <br/> <TD> 4 </TD> <br/> <TD> Zhang Xueyou </TD> <br/> <TD> 8009 </TD> <br/> <TD> Vice President </TD> <br/> <TD> male </TD> <br/> <TD> 90 </TD> <br/> <TD> 2008-04-11 </TD> <br/> </tr> <br/> <TD> 5 </TD> <br/> <TD> Cecilia Cheung </TD> <br/> <TD> 8005 </TD> <br/> <TD> assistant </TD> <br/> <TD> female </TD> <br/> <TD> 78 </TD> <br/> <TD> 2008-04-13 </TD> <br/> </tr> <br/> <TD> 6 </TD> <br/> <TD> *** </TD> <br/> <TD> 8001 </TD> <br/> <TD> Director </TD> <br/> <TD> male </TD> <br/> <TD> 60 </TD> <br/> <TD> 2008-04-18 </TD> <br/> </tr> <br/> <tr> <br/> <TD> 7 </TD> <br/> <TD> Chen Huilin </TD> <br/> <TD> 8002 </TD> <br/> <TD> trial period </TD> <br/> <TD> female </TD> <br/> <TD> 85 </TD> <br/> <TD> 2008-04-18 </TD> <br/> </tr> <br/> <TD> 8 </TD> <br/> <TD> Zhang manyu </TD> <br/> <TD> 8007 </TD> <br/> <TD> Senior Manager </TD> <br/> <TD> female </TD> <br/> <TD> 82 </TD> <br/> <TD> 2008-04-16 </TD> <br/> </tr> <br/> <TD> 9 </TD> <br/> <TD> Zhou runfa </ TD> <br/> <TD> 8006 </TD> <br/> <TD> Vice President </TD> <br/> <TD> male </TD> <br /> <TD> 88 </TD> <br/> <TD> 2008-04-13 </TD> <br/> </tr> <br/> </table> <br /> <br/> code: new tablesorter ("Fig "). onsorted = function (C, T) <br/> <br/>{< br/> <br/> alert ("table is sorted by" + C. innerhtml + "" + (T? "ASC": "DESC"); <br/> <br/>}< br/> <br/> effect: Click any header to perform sorting and return a prompt. <br/> <Table border = "0" style = "border: solid 1px # dddddddd; width: 360px; "cellpadding =" 0 "cellspacing =" 0 "id =" int32 "> <br/> <tr> <br/> <TH> NO. </Th> <br/> <TH> name </Th> <br/> <TH> employee ID </Th> <br/> <TH> position </Th> <br/> <TH> gender </Th> <br/> <TH> Performance </Th> <br/> <TH> report time </Th> <br/> </tr> <br/> <tr> <br/> <TD> 1 </TD> <br/> <TD> Andy Lau </TD> <br/> <TD> 8008 </TD> <br/> <TD> President </TD> <Br/> <TD> male </TD> <br/> <TD> 98 </TD> <br/> <TD> 2008-04-12 </TD> <br/> </tr> <br/> <TD> 2 </TD> <br/> <TD> Wu qilong </TD> <br/> <TD> 8004 </TD> <br/> <TD> supervisor </TD> <br/> <TD> male </TD> <br/> <TD> 80 </TD> <br/> <TD> 2008-04-16 </TD> <br/> </tr> <br/> <TD> 3 </TD> <br/> <TD> Wang Guangliang </TD> <br/> <TD> 8003 </TD> <br/> <TD> Manager </TD> <br/> <TD> male </TD> <br/> <TD> 85 </TD> <br/> <TD> 2008-04-15 </TD> <br/> </tr> <br/> <TD> 4 </TD> <br/> <TD> Zhang Xueyou </TD> <br/> <TD> 8009 </TD> <br/> <TD> Vice President </TD> <br/> <TD> male </TD> <br/> <TD> 90 </TD> <br/> <TD> 2008-04-11 </TD> <br/> </tr> <br/> <tr> <br/> <TD> 5 </TD> <br/> <TD> Cecilia Cheung </TD> <br/> <TD> 8005 </TD> <br/> <TD> assistant </TD> <br/> <TD> female </TD> <br/> <TD> 78 </TD> <br/> <TD> 2008-04-13 </TD> <br/> </tr> <br/> <TD> 6 </TD> <br/> <TD> *** </TD> <br/> <TD> 8001 </TD> <br/> <TD> Director </TD> <br/> <TD> male </TD> <br/> <TD> 60 </TD> <br/> <TD> 2008-04-18 </TD> <br/> </tr> <br/> <TD> 7 </TD> <br/> <TD> Chen Huilin </TD> <br/> <TD> 8002 </TD> <br/> <TD> trial period </TD> <br/> <TD> female </TD> <br/> <TD> 85 </TD> <br/> <TD> 2008-04-18 </TD> <br/> </tr> <br/> <tr> <br /> <TD> 8 </TD> <br/> <TD> Zhang manyu </TD> <br/> <TD> 8007 </TD> <br/> <TD> Senior Manager </TD> <br/> <TD> female </TD> <br/> <TD> 82 </TD> <br/> <TD> 2008-04-16 </TD> <br/> </tr> <br/> <TD> 9 </TD> <br/> <TD> Zhou runfa </ TD> <br/> <TD> 8006 </TD> <br/> <TD> Vice President </TD> <B R/> <TD> male </TD> <br/> <TD> 88 </TD> <br/> <TD> 2008-04-13 </TD> <br/> </tr> <br/> </table> <br/> <script language = "JavaScript" type = "text/JavaScript"> <br /> New tablesorter ("tb1 "); <br/> New tablesorter ("tb2", 0, 2, 5, 6); <br/> New tablesorter ("interfaces "). onsorted = function (C, T) <br/>{< br/> alert ("table is sorted by" + C. innerhtml + "" + (T? "ASC": "DESC ")); <br/>}< br/> </SCRIPT> <br/> </body> <br/> </ptml> <br/>
JavaScript code:
// Author: Wolf robot <br/> // contact: robot@k2046.cn <br/> // Date: 2008-04-19 <br/> // explain: enables the table to be sorted by clicking. <br/>/* usage: <br/> Method 1: <br/> New tablesorter ("tb1"); <br/> effect: <br/> Any cell in the first row of the table whose ID is tb1 can be clicked for sorting. <br/> Method 2: <br/> New tablesorter ("tb1", 0, 1, 3); <br/> effect: <br/> cells 0, 1, and 3 of the first row of the table whose ID is tb1 can be clicked and sorted. <br/> */<br/> function tablesorter (table) <br/> {<br/> This. table = This. $ (table); <br/> If (this. table. Rows. length <= 1) <br/>{< br/> return; <br/>}< br/> This. init (arguments); <br/>}< br/> // The following style is applicable to the cells in the header. <br/> tablesorter. prototype. normalcss = "normalcss"; // The style when the sorting is not executed. <br/> tablesorter. prototype. sortasccss = "sortasccss"; // The Style in ascending order. <br/> tablesorter. prototype. sortdesccss = "sortdesccss"; // The Style in descending order. <br/> // initialize table information and operations. <br/> tablesorter. prototype. init = function (ARGs) <br/>{< br/> This. viewstate = []; <Br/> for (VAR x = 0; x <this. table. rows [0]. cells. length; X ++) <br/>{< br/> This. viewstate [x] = false; <br/>}< br/> If (ARGs. length> 1) <br/>{< br/> for (VAR x = 1; x <args. length; X ++) <br/>{< br/> If (ARGs [x]> This. table. rows [0]. cells. length) <br/>{< br/> continue; <br/>}< br/> else <br/>{< br/> This. table. rows [0]. cells [ARGs [x]. onclick = This. getfunction (this, "sort", argS [x]); <br/> This. table. rows [0]. Cells [ARGs [x]. style. cursor = "Pointer "; <br/>}< br/> else <br/>{< br/> for (VAR x = 0; x <this. table. rows [0]. cells. length; X ++) <br/>{< br/> This. table. rows [0]. cells [X]. onclick = This. getfunction (this, "sort", x); <br/> This. table. rows [0]. cells [X]. style. cursor = "Pointer"; <br/>}< br/> // abbreviated as document. getelementbyid method. <br/> tablesorter. prototype. $ = function (element) <br/>{< br/> Return document. getelementbyid (element); <br/>}< br/> // gets the shell function of the specified object. <br/> tablesorter. prototype. getfunction = function (variable, method, Param) <br/>{< br/> return function () <br/>{< br/> variable [Method] (PARAM ); <br/>}< br/> // perform sorting. <br/> tablesorter. prototype. sort = function (COL) <br/>{< br/> var sortasnumber = true; <br/> for (VAR x = 0; x <this. table. rows [0]. cells. length; X ++) <br/>{< br/> This. Table. rows [0]. cells [X]. classname = This. normalcss; <br/>}< br/> var sorter = []; <br/> for (VAR x = 1; x <this. table. rows. length; X ++) <br/>{< br/> sorter [x-1] = [this. table. rows [X]. cells [col]. innerhtml, X]; <br/> sortasnumber = sortasnumber & this. isnumeric (sorter [x-1] [0]); <br/>}< br/> If (sortasnumber) <br/>{< br/> for (VAR x = 0; x <sorter. length; X ++) <br/>{< br/> for (var y = x + 1; y <sorter. lengt H; y ++) <br/>{< br/> If (parsefloat (sorter [y] [0]) <parsefloat (sorter [x] [0]) <br/>{< br/> var TMP = sorter [X]; <br/> sorter [x] = sorter [y]; <br/> sorter [y] = TMP; <br/>}< br/> else <br/>{< br/> sorter. sort (); <br/>}< br/> If (this. viewstate [col]) <br/>{< br/> sorter. reverse (); <br/> This. viewstate [col] = false; <br/> This. table. rows [0]. cells [col]. classname = This. sortdesccss; <br/>}< br/> Else <br/>{< br/> This. viewstate [col] = true; <br/> This. table. rows [0]. cells [col]. classname = This. sortasccss; <br/>}< br/> var Rank = []; <br/> for (VAR x = 0; x <sorter. length; X ++) <br/>{< br/> rank [x] = This. getrowhtml (this. table. rows [sorter [x] [1]); <br/>}< br/> for (VAR x = 1; x <this. table. rows. length; X ++) <br/>{< br/> for (var y = 0; y <this. table. rows [X]. cells. length; y ++) <br/>{< br/> This. tab Le. rows [X]. cells [Y]. innerhtml = rank [x-1] [Y]; <br/>}< br/> This. onsorted (this. table. rows [0]. cells [col], this. viewstate [col]); <br/>}< br/> // get the content of the specified row. <br/> tablesorter. prototype. getrowhtml = function (ROW) <br/> {<br/> var result = []; <br/> for (VAR x = 0; x <row. cells. length; X ++) <br/>{< br/> result [x] = row. cells [X]. innerhtml; <br/>}< br/> return result; <br/>}< br/> tablesorter. prototype. isnum Eric = function (Num) <br/>{< br/> return/^ \ D + (\. \ D + )? $ /. Test (Num); <br/>}< br/> // you can perform sorting by yourself. <br/> tablesorter. prototype. onsorted = function (cell, isasc) <br/>{< br/> return; <br/>}< br/>

Code: New tablesorter ("tb1 ");
Effect: Click any header to perform sorting.

Serial number Name Employee ID Position Gender Performance Report Time
1 Andy Lau 8008 President Male 98 2008-04-12
2 Wu qilong 8004 Supervisor Male 80 2008-04-16
3 Wang Guangliang 8003 Manager Male 85 2008-04-15
4 Zhang Xueyou 8009 Vice President Male 90 2008-04-11
5 Cecilia Cheung 8005 Assistant Female 78 2008-04-13
6 *** 8001 Director Male 60 2008-04-18
7 Chen Huilin 8002 Trial Period Female 85 2008-04-18
8 Zhang manyu 8007 Senior Manager Female 82 2008-04-16
9 Zhou runfa 8006 Vice President Male 88 2008-04-13

Code: New tablesorter ("tb2", 0, 2, 5, 6 );
Effect: Click the, columns in the header to perform sorting.

Serial number Name Employee ID Position Gender Performance Report Time
1 Andy Lau 8008 President Male 98 2008-04-12
2 Wu qilong 8004 Supervisor Male 80 2008-04-16
3 Wang Guangliang 8003 Manager Male 85 2008-04-15
4 Zhang Xueyou 8009 Vice President Male 90 2008-04-11
5 Cecilia Cheung 8005 Assistant Female 78 2008-04-13
6 *** 8001 Director Male 60 2008-04-18
7 Chen Huilin 8002 Trial Period Female 85 2008-04-18
8 Zhang manyu 8007 Senior Manager Female 82 2008-04-16
9 Zhou runfa 8006 Vice President Male 88 2008-04-13

Code: New tablesorter ("int32"). onsorted = function (C, T)
{
Alert ("table is sorted by" + C. innerhtml + "" + (T? "ASC": "DESC "));
}
Effect: Click any header to perform sorting and return a prompt.

Serial number Name Employee ID Position Gender Performance Report Time
1 Andy Lau 8008 President Male 98 2008-04-12
2 Wu qilong 8004 Supervisor Male 80 2008-04-16
3 Wang Guangliang 8003 Manager Male 85 2008-04-15
4 Zhang Xueyou 8009 Vice President Male 90 2008-04-11
5 Cecilia Cheung 8005 Assistant Female 78 2008-04-13
6 *** 8001 Director Male 60 2008-04-18
7 Chen Huilin 8002 Trial Period Female 85 2008-04-18
8 Zhang manyu 8007 Senior Manager Female 82 2008-04-16
9 Zhou runfa 8006 Vice President Male 88 2008-04-13

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.