Sort Callback
Sort Callback
Is there a sort callback that I am overlooking?
I would like to do some things after a column has been sorted. I have gotten around some issues by binding a click event to the th tags however I have ran into a new issue that I would like to do that would involve needing a callback. I could use a timeout but I'd rather avoid that if possible.
Has anyone else ran into this?
Could I use fnRowCallback or fnDrawCallback?
Thanks,
Drew
I would like to do some things after a column has been sorted. I have gotten around some issues by binding a click event to the th tags however I have ran into a new issue that I would like to do that would involve needing a callback. I could use a timeout but I'd rather avoid that if possible.
Has anyone else ran into this?
Could I use fnRowCallback or fnDrawCallback?
Thanks,
Drew
This discussion has been closed.
Replies
Use fnDrawCallback. That is called at the end of every draw, and a draw will occur after a sort. It's possible to refine it more by checking 'this.oSettings.bSorted' (inside the callback, which is executed in the DataTables object scope for 'this'). That flag will be true if the draw is due to a sort, false otherwise.
This is something I want to look at improving in future, providing event bindings for DataTables is very much on the to-do list for DataTables 2 - whenever that happens... :-)
Allan
Totally.
[code]
"fnDrawCallback": function() {
console.log("[fnDrawCallback] enter: " + this.oSettings.bSorted);
}
[/code]
[code]
"fnDrawCallback": function() {
console.log("[fnDrawCallback] enter: " + this.fnSettings().bSorted);
}
[/code]
or assign oSettings = this.fnSettings();
Events: yup - this is becoming increasingly important. It is in the crosshairs for the next major version.
Allan
I need to call this function after a sort:
function stripeTableResults() {
$("[class='item']:even").addClass("alternateItemRow");
}
I currently do this from:
$(function () {
stripeTableResults();
});
But of course a sort mixes up the striping.
So I need a call back after a sort.
I just can't figure out how to do this.
This is incredibly fustrating.
Come on. This is rediculous.
http://www.datatables.net/ref#fnDrawCallback
I have
$(function () {
$("#tblResults").dataTable({
"bPaginate": false,
"bLengthChange": false,
"bFilter": false,
"bSort": true,
"bInfo": false,
"bAutoWidth": false,
"fnDrawCallback": stripeTableResults()
});
});
function stripeTableResults() {
$("[class='item']:even").addClass("alternateItemRow");
}
But now, the striping is messed up after a sort.
Apparently the rows are displayed in the new sorted order but internally they are really considered the same order as the original layout.
so the jQuery statement:
$("[class='item']:even").addClass("alternateItemRow");
shows the stripes all out of wack.
Not the redraw.
function stripeTableResults() {
$("[class='item']").removeClass("alternateItemRow");
alert('removed class');
$("[class='item']:even").addClass("alternateItemRow");
alert('added class');
}
tr.odd
{
background-color:#E6E6E6;
}
But I would still like to know why that callback does not work on every draw.
It is only called once on the initial setup.
Maybe it is just the order how things are laid out.
[code]
"fnDrawCallback": stripeTableResults()
[/code]
now unless "stripeTableResults" returns a function, then this is not what you want. You are executing "stripeTableResults" and then assigning the result to fnDrawCallback. I would imagine that you just want to pass the function by reference:
[code]
"fnDrawCallback": stripeTableResults
[/code]
Allan
Checking for bSorted doesn't help since it is always true.
[code]
$('#example').dataTable().bind('sort', function () {
// DataTables has done a sort
} );
[/code]
Note that you won't get the event on the first initialisation, since the listener won't have been bound by then.
Allan
$('#example').dataTable().bind('sort', function () {
// DataTables has done a sort
} );