First of all, the API inside, where the first one is Order, which is set which sort of what is not sorted, such as:$(
‘#example‘
).dataTable( {
"order"
: (function(){
var arr=
[[ 0,
‘asc‘
], [ 1,
‘asc‘
]];
return arr;
})();//匿名函数里的自运行不影响其他外部的变量
} );
The second is columndefs, which deals with the rules of a particular column, for example: A component in my package, which is passed in to customize the sort, can be written as: Columndefs: (function () {var arry=[],i=0;if ( IpAddress && ipAddress length>0) {Arry.push ({type: ' ip-address ',//This is a custom name targets:ipaddress,// This is an array to customize the column index
})}return Arry;}) (), for a small example: https://datatables.net/examples/plug-ins/sorting_manual
$.fn.dataTable.ext.type.order[
‘salary-grade-pre‘
] =
function
( d ) {
switch
( d ) {
case
‘Low‘
:
return
1;
case
‘Medium‘
:
return
2;
case
‘High‘
:
return
3;
}
return
0;
};
$(document).ready(
function
() {
$(
‘#example‘
).DataTable( {
"columnDefs"
: [ {
"type"
:
"salary-grade"
,
"targets"
: -1
} ]
} );
} );
After the search has a netizen also encountered the same problem, written very well, the restrictions posted as follows. Can be used as a reference:
Original Http://wuchong.me/blog/2014/02/25/jquery-datatable-sort-plugin/?utm_source=tuicool&utm_medium=referral
Recently used in the project Jquery.datatables, this is a very powerful jQuery plug-in, call convenient, support callback to the data sorting, query, paging and other operations, and bootstrap framework also has its encapsulation, save our interface design live. DataTables comes with a sort of string,date,numeric, but when it comes to a more special sort of demand, it has to find another way out.
These days, it's just a requirement, a few columns of cells in a table are percentages, and HTML tags. You need to sort the percentages for these columns.
This is the HTML code in the cell.
<class="label label-important" ><class="icon-caret-up" ></i> &nbsp;&nbsp;100.0%</span> |
The table is probably long like this.
And DataTables's own sort treats this column as a string sort. It's obviously not satisfying our needs. At first thought to be a big fuss, later read the API document found that DataTables's third-party extension support is very flexible. Two methods are available in the official documentation: (1) Type based column sorting; (2) Custom data source sorting
。 I'm using the first method. The main idea is to turn the cell contents into a sortable float type.
First create a file called dataTables.sort.plungin.js
, add the following code.
Jquery.extend (JQuery.fn.dataTableExt.oSort, { "Html-percent-pre":function (A) { var x =String (a). Replace (/<[\s\s]*?>/g,"");Remove HTML tagsx = X.replace (/&nbsp;/ig,"");Remove spacesx = X.replace (/%/,"");Remove percent sign Returnparsefloat (x);}, ' HTML-PERCENT-ASC ': function (a, b) {//positive ordering reference method return ((a < b)? -1: ((a > B)? 1: 0)); }, ' Html-percent-desc ': function (a, b) {//reverse sort Reference Method return ((a < b)? 1: ((a > B)? -1: 0)); |
Add the following JS reference to the foreground page.
<ScriptType="Text/javascript"Src="Jquery.dataTables.js" ></Script><ScriptType="Text/javascript"Src="DataTables.numericComma.js" ></Script><ScriptType="Text/javascript" > var oTable1 = $ (' #table_report '). DataTable ({ "Aocolumndefs": [{"Stype":"Html-percent","Atargets": [8]},Specify a column number to use a custom sort], "Blengthchange":Trueswitch, whether to display a drop-down box for each page size "Alengthmenu": [[5, 10, 25, -1], [5, 10, 25, " all "]], ' idisplaylength ': 25, //show 10 records per page Span class= "string" > ' Bfilter ': true, //whether to use built-in filtering function "Binfo": true, //switch, whether to display some information about the table "Bpaginate": true //switch, Whether to display the paging device }); </SCRIPT> |
Complete.
Resources
- DataTables About sorting documents
- Another way to customize
The project structure I have encountered is:
It is necessary to note that one of the collations I have named is: ip-address, changing the constructor of DataTables inside the relevant API is:
Order: (function () {//This is the initialization of which tables to sort, as well as ascending or descending sort
var arry=[];
if (IPAddress && ipaddress.length>0) {
for (Var i=0;i<ipaddress.length;i++) {
Arry.push ([ipaddress[i], ' ASC ');
}
return array;
})(),
Columndefs: (function () {var arry=[],i=0;if (ipAddress && ipAddress. length>0) {Arry.push ({type: ' ip-address ',//This is a custom name targets:ipaddress,//this is an array to customize the column index
})}return Arry;}) (), then say the custom JS file (to be loaded before this table is constructed). Write the corresponding JS file I named Ip-address-sort.js inside the content is: Jquery.extend (jquery.fn.datatableext.osort,{"Ip-address-pre": function (a) {//This is a click on the corresponding biographies into the TD inside the content, pre is the data preprocessing, this before the comparison of data called ... Omit the process of data processing return x;//This is the last to return the processed individual data in each list, not the array ha, just a value}, "Ip-address-asc": function (A, B) {//Click on the list to increment the order when the call Return ((A<B) 1: ((a> b)? 1:0));//Comparison size}, "Ip-address-desc": function (A, a, b) {//click-List descending sort to call return ((a<b)?-1: ((a> b)? 1:0);});
DataTables a custom sort for a particular column