Home  >  Article  >  Web Front-end  >  Using jquery.sortElements to implement table sorting_javascript skills

Using jquery.sortElements to implement table sorting_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:50:15886browse

The table sorting function needs to be implemented in the project.

There are many solutions online, many of which are based on jQuery.

jquery.tablesorter, size 17KB, but its homepage has some compatibility issues under IE10.
DataTables, 75KB in size, powerful, with paging, search and other functions.
There is also a plug-in called sortElements, which is very small, only 3KB, has good compatibility, and has 818 stars on Github.

Finally I chose to use sortElements, the implementation is very simple:

1. Introduce jQuery

Copy code The code is as follows:



2. Introduction sortElements.js
Copy code The code is as follows:



3. js code
Copy code The code is as follows:

$(document).ready(function(){
var table = $('#mytable');//table's id
$('#sort_header')//Headerid to be sorted
.each(function(){
var th = $(this),
thIndex = th.index(),
inverse = false;

th.click(function(){
table.find('td').filter(function(){
return $(this).index() === thIndex;
}).sortElements(function(a, b){
return $.text([a]) > $.text([b]) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
}, function(){
return this.parentNode;
});
inverse = !inverse;

});
});
});

4. html code
Copy code The code is as follows:









;GGG

...
Facility name Phone # City Speciality
CCC 00001111


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn