How to cancel a sort event after it is fired?

How to cancel a sort event after it is fired?

tpctpc Posts: 12Questions: 0Answers: 0
edited May 2013 in DataTables 1.9
Is it possible to cancel a sort event after it is fired?

I have a checkbox column in my table with a "check-all" checkbox in the header. When the checkbox in the header is clicked I want to prevent the sorting from occurring however if any other portion of the header is click I want it to sort. Here's what I've come up with so far but I am not sure how to cancel the sort.

Click event of the "check-all" checkbox

[code]
var checkallClicked = false;
$('#check-all').click(function() {
checkallClicked = true;
...
});
[/code]

Click/sort event of the header

[code]
$('.check-all-header').click(function(){
if (checkallClicked) {
checkallClicked = false;
return false;
}
});
[/code]

Replies

  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    Just don't allow the event to bubble. Use `event.stopPropagation()` .

    Allan
  • tpctpc Posts: 12Questions: 0Answers: 0
    Hmm, I tried that but it didn't stop the sort. Any ideas?

    [code]
    $('.check-all-header').click(function(e){
    if (checkallClicked) {
    checkallClicked = false;
    e.stopPropagation();
    return false;
    }
    });
    [/code]
  • allanallan Posts: 63,383Questions: 1Answers: 10,449 Site admin
    I'd call it regardless of the checkallClicked state. Other than that, I think we'd need a test case showing the issue.

    Allan
  • tpctpc Posts: 12Questions: 0Answers: 0
    Thanks for the response. It'd take me some time to get a test case put together.

    As a very hackish work around I put 1.5% width columns on either side of the checkbox column and turned off sorting on the 1% width checkbox column.

    So now if a user clicks the headers on either side of the checkbox it will sort the checkbox row.
This discussion has been closed.