How exclude first 3 rows from all sorting ?
How exclude first 3 rows from all sorting ?
a_svyrydov
Posts: 2Questions: 1Answers: 0
Hi all, help me please. When table sorted, I want leave all <tr> with class="static" in the top of table and never move them.
This discussion has been closed.
Answers
I got a little curious about this one myself, so I looked at some of the custom sorting examples and came up with something that basically looks to see if the closest
tr
element of thetd
being sorted has the classstatic
, if so, just returnaaaaa
(or whatever will place it at the top)Heres what ive got so far. I can get it to put the rows at the top, then sort the rows below it based on the cell value, so that works fine. The problem im running into, is im not sure how to make it so when you sort it descending, those rows still stay on top.
Theres gotta be a way to see what direction the column is being sorted from within the custom sorting function, then return something like
zzzz
instead ofaaaa
... but idk.Hopefully this helps. Im sure @allan knows of a better way, but hes usually pretty busy, so I would recommend looking at some of the existing sorting plugins and going at it yourself.
Yup - you need to use a sorting plug-in for this. I plan to write one and a blog post explaining it, but there isn't one that exists yet I'm afraid.
The one "trick" here is that the sorting doesn't have access to the class names for rows or cells, and moreover, the sorting is per column. So we'd need to find some way of indicating to the plug-in which rows should be at the start or the end of the sorting (and any sorting that should be applied inside the selected rows as well!).
Allan
Hi thanks you for answer.
Here my solution:
$.fn.dataTable.ext.order['dom-numeric'] = function (settings, col )
{
var res, value, sorting;
sorting = settings['aLastSort'][0]['dir']
return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
if($(td).parent().find('.left .fa-star').length > 0){
if(sorting == 'desc'){
res = -1000;
}else if(sorting == 'asc'){
res = 500;
}else{
res = 0;
}
}else{
value = $(td).attr('data-order');
if(value == '' || value == '-'){
if(sorting == 'desc'){
res = 500;
}else if(sorting == 'asc'){
res = -1000;
}else{
res = 0;
}
}else{
res = parseInt(value);
}
}
return res;
});
}
$.fn.dataTable.ext.order['dom-string'] = function (settings, col )
{
var res, value, sorting;
sorting = settings['aLastSort'][0]['dir']
return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
if($(td).parent().find('.left .fa-star').length > 0){
if(sorting == 'desc'){
res = 'aaaa';
}else if(sorting == 'asc'){
res = 'zzzz';
}else{
res = 'bbbb';
}
}else{
value = $(td).attr('data-order');
if(value == '' || value == '-'){
if(sorting == 'desc'){
res = 'zzzz';
}else if(sorting == 'asc'){
res = 'aaaa';
}else{
res = 'bbbb';
}
}else{
res = value;
}
}
return res;
});
}
var table = $('#table-graph').DataTable(
{
It's works but with bug. Only after second sorting operation.
Of Course code can be more clear. But I have not time for bug fixing.