Jquery implements local table sorting
This article mainly introduces jquery's method of sorting local tables. The example analyzes the jQuery Operation Array's technique of sorting local tables. For more information, see
This example describes how jquery performs local table sorting. Share it with you for your reference. The specific implementation method is as follows:
The Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> jquery table sorting </title>
<Style type = "text/css">
Thead
{
Background-color: Blue;
Color: White;
}
Tr. odd
{
Background-color: # ddd;
}
Tr. even
{
Background-color: # eee;
}
. Clickable
{
Text-decoration: underline;
}
. Hover
{
Background-color: #5dd354;
}
. Sorted
{
Background-color: # ded070;
}
. Page-number
{
Color: Black;
Margin: 2px 10px;
Padding: 2px 5px;
}
. Active
{
Border: solid 1px red;
Background-color: #76a7d2;
}
. Pager
{
Margin-bottom: 10px;
Margin-left: 20px;
}
</Style>
<Script type = "text/javascript" language = "javascript" src = "js/jquery1.3.2.js"> </script>
<Script type = "text/javascript" language = "javascript">
$ (Function (){
JQuery. fn. alternateRowColors = function () {// form a plug-in
$ ('Tbody tr: odd', this). removeClass ('even'). addClass ('odd'); // an odd number of rows with a color change on each line
$ ('Tbody tr: even', this). removeClass ('odd'). addClass ('even'); // The even rows with a color change on the line.
Return this;
};
$ ('Table. mytable'). each (function (){
Var $ table = $ (this); // store the table as a jquery object
$ Table. alternateRowColors ($ table); // color changes after sorting by row
$ ('Th', $ table). each (function (column ){
Var findSortKey;
If ($ (this). is ('. sort-alpha') {// sort by letter
FindSortKey = function ($ cell ){
Return $ cell. find ('sort-key'). text (). toUpperCase () + ''+ $ cell. text (). toUpperCase ();
};
} Else if ($ (this). is ('. sort-numeric') {// sort by number
FindSortKey = function ($ cell ){
Var key = parseFloat ($ cell. text (). replace (/^ [^ \ d.] */, '');
Return isNaN (key )? 0: key;
};
} Else if ($ (this). is ('. sort-date') {// sort by date
FindSortKey = function ($ cell ){
Return Date. parse ('1' + $ cell. text ());
};
}
If (findSortKey ){
$ (This ). addClass ('clickable '). hover (function () {$ (this ). addClass ('hover ');}, function () {$ (this ). removeClass ('hover ');}). click (function (){
// Reverse sorting state Declaration
Var newDirection = 1;
If ($ (this). is ('. sorted-asc ')){
NewDirection =-1;
}
Var rows = $ table. find ('tbody> tr'). get (); // convert data rows to arrays.
$. Each (rows, function (index, row ){
Row. sortKey = findSortKey ($ (row). children ('td '). eq (column ));
});
Rows. sort (function (a, B ){
If (a. sortKey <B. sortKey) return-newDirection;
If (a. sortKey> B. sortKey) return newDirection;
Return 0;
});
$. Each (rows, function (index, row ){
$ Table. children ('tbody'). append (row );
Row. sortKey = null;
});
$ Table. find ('th '). removeClass ('sorted-asc'). removeClass ('sorted-desc ');
Var $ sortHead = $ table. find ('th'). filter (': nth-child (' + (column + 1) + ')');
// Reverse sorting
If (newDirection = 1 ){
$ SortHead. addClass ('sorted-asc ');
} Else {
$ SortHead. addClass ('sorted-desc ');
}
// Call the function for changing the color of the line
$ Table. alternateRowColors ($ table );
// Remove the style of the sorted column and add the style to the current column
$ Table. find ('td '). removeClass ('sorted '). filter (': nth-child (' + (column + 1) + ')'). addClass ('sorted ');
$ Table. trigger ('repaginate ');
});
}
});
});
});
// Pagination
$ (Function (){
$ ('Table. paginated '). each (function (){
Var currentPage = 0;
Var numPerPage = 10;
Var $ table = $ (this );
$ Table. bind ('repaginate', function (){
$ Table. find ('tbody tr'). hide (). slice (currentPage * numPerPage, (currentPage + 1) * numPerPage). show ();
});
Var numRows = $ table. find ('tbody tr'). length;
Var numPages = Math. ceil (numRows/numPerPage );
Var $ pager =$ ('<div class = "pager"> </div> ');
For (var page = 0; page <numPages; page ++ ){
$ ('<Span class = "page-number"> </span>'). text (page + 1)
. Bind ('click', {newPage: page}, function (event ){
CurrentPage = event. data ['newpage'];
$ Table. trigger ('repaginate ');
$ (This). addClass ('active'). siblings (). removeClass ('active ');
}). AppendTo ($ pager). addClass ('clickable ');
}
$ Pager. insertBefore ($ table );
$ Table. trigger ('repaginate ');
$ Pager. find ('span. page-number: first '). addClass ('active ');
});
});
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Table class = "myTable paginated">
<Thead>
<Tr>
<Th class = "sort-alpha">
Last Name
</Th>
<Th class = "sort-alpha">
First Name
</Th>
<Th>
Email
</Th>
<Th class = "sort-numeric">
Due
</Th>
<Th class = "sort-date">
Date
</Th>
<Th>
Web Site
</Th>
</Tr>
</Thead>
<Tbody>
<Tr>
<Td>
Tmith
</Td>
<Td>
Erthn
</Td>
<Td>
Eth@gmail.com
</Td>
<Td>
$34.00
</Td>
<Td>
14 2009
</Td>
<Td>
Ftp://www.baidu.com
</Td>
</Tr>
<Tr>
<Td>
TTmith
</Td>
<Td>
BJohn
</Td>
<Td>
Jsmith@gmail.com
</Td>
<Td>
$50.00
</Td>
<Td>
Mar 2009
</Td>
<Td>
Ftp://www.baidu.com
</Td>
</Tr>
<Tr>
<Td>
CSmith
</Td>
<Td>
John
</Td>
<Td>
DDDD@gmail.com
</Td>
<Td>
$50.00
</Td>
<Td>
Mar 2009
</Td>
<Td>
Http://www.jb51.net
</Td>
</Tr>
<Tr>
<Td>
Smith
</Td>
<Td>
John
</Td>
<Td>
Sdsf@gmail.com
</Td>
<Td>
$50.00
</Td>
<Td>
F32 2009
</Td>
<Td>
Ffttp: // www.jb51.net
</Td>
</Tr>
</Tbody>
</Table>
</Div>
</Form>
</Body>
</Html>
I hope this article will help you with jquery programming.