Sort tables using JavaScript (from JavaScript advanced programming)

Source: Internet
Author: User

// Sorttable. JS File
Function convert (svalue, sdatatype)
{
Switch (sdatatype)
{
Case "int ":
Return parseint (svalue );
Break;
Case "float ":
Return parsefloat (svalue );
Break;
Case "date ":
Return new date (date. parse (svalue ));
Break;
Default:
Return svalue. tostring ();
}
}

Function generatecomparetrs (icol, sdatatype)
{
Return function comparetrs (otr1, otr2)
{
VaR vvalue1 = convert (otr1.cells [icol]. firstchild. nodevalue, sdatatype );
VaR vvalue2 = convert (otr2.cells [icol]. firstchild. nodevalue, sdatatype );
If (vvalue1 <vvalue2)
{
Return-1;
}
Else if (vvalue1> vvalue2)
{
Return 1;
}
Else
{
Return 0;
}
}
}
Function sorttable (stableid, icol, sdatatype)
{
VaR otable = Document. getelementbyid (stableid );
VaR otbody = otable. tbodies [0];
VaR coldatarows = otbody. Rows;
VaR ATRS = new array ();
For (VAR I = 0; I <coldatarows. length; I ++)
{
ATRS. Push (coldatarows [I]);
}
If (otable. sortcol = icol)
{
ATRS. Reverse ();
}
Else
{
ATRS. Sort (generatecomparetrs (icol, sdatatype ));
}
VaR ofragment = Document. createdocumentfragment ();
For (VAR I = 0; I <ATRS. length; I ++)
{
Ofragment. appendchild (ATRS [I]);
}
Otbody. appendchild (ofragment );
Otable. sortcol = icol;
}

// The End

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> No title page </title>
<SCRIPT type = "text/JavaScript" src = "sorttable. js"> </SCRIPT>
</Head>
<Body>
<Table border = "1" id = "tblsort">
<Thead>
<Tr>
<TH onclick = "sorttable ('tblsort ', 0)" style = "cursor: Pointer"> last name </Th>
<TH onclick = "sorttable ('tblsort ', 1)" style = "cursor: Pointer"> first name </Th>
<TH onclick = "sorttable ('tblsort ', 2, 'date')" style = "cursor: Pointer"> birthday </Th>
<TH onclick = "sorttable ('tblsort ', 3, 'int')" style = "cursor: Pointer"> siblings </Th>
</Tr>
</Thead>
<Tbody>
<Tr>
<TD> Smith </TD>
<TD> JOHN </TD>
<TD> 7/12/1978 </TD>
<TD> 2 </TD>
</Tr>
<Tr>
<TD> sjohnson </TD>
<TD> Betty </TD>
<TD> 10/15/1977 </TD>
<TD> 4 </TD>
</Tr>
<Tr>
<TD> Henderson </TD>
<TD> Nathan </TD>
<TD> 2/25/1949 </TD>
<TD> 1 </TD>
</Tr>
<Tr>
<TD> Williams </TD>
<TD> James </TD>
<TD> 7/8/1980 </TD>
<TD> 4 </TD>
</Tr>
<Tr>
<TD> Gilliam </TD>
<TD> Michael </TD>
<TD> 7/22/1949 </TD>
<TD> 1 </TD>
</Tr>
<Tr>
<TD> Walker </TD>
<TD> Matthew </TD>
<TD> 1/14/2000 </TD>
<TD> 3 </TD>
</Tr>
</Tbody>
</Table>
</Body>
</Html>

Related Article

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.