Intercepting specific event handling (pagination click)

Intercepting specific event handling (pagination click)

frenzyfrenzy Posts: 21Questions: 0Answers: 0
edited February 2012 in General
Hi,

is there a way to intercept the default datatables handling when user clicks on pagination button (to fetch next page for example). I would do a little bit of validation before getting new content from the server (using bServerSide = true;). So, if the validation goes wrong, i would display a message instead of fetching new data from the server.

Thanks.

Replies

  • frenzyfrenzy Posts: 21Questions: 0Answers: 0
    I tried using fnServerParams (because its the function that gets called _before_ going to server, and it triggers on pagination click, which is something I need), and it actually works, but I'm not sure if its the best way to do it.

    "fnServerParams": function (aoData) {
    //do validate
    if (isValid() == false) {
    //errors on page, just return
    return false;
    } else {
    aoData.push(...);
    etc..
    }

    Is there a correct way to do this?
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Yup that looks valid. The other way of doing it is to use fnPreDrawCallback . This is the function that is intended for exactly this sort of thing and allows you to cancel a draw. You won't know if it is causes by paging or sorting or something else though, unless you keep track of the paging variables in the table.

    Allan
  • frenzyfrenzy Posts: 21Questions: 0Answers: 0
    I knew theres already a method that fully fits my needs! :) This works, tho I noticed a strange thing (a bug?) while testing. My datatable is writeable, and suppose i do a change which is not 'valid' (the isValid() method returns false) and I click to fetch the second page - nothing happens (the same page is shown) since it is supposed to work that way. Afterwards the change is corrected and I click to fetch next page again - it works but the page returned is actually the 3. page instead the second one. It looks like datatables remembers the navigation the user tried to do although it shouldnt in this scenario?
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Ah yes - that is exactly what is happening. All the setup is occurring before the draw, and then the draw is cancelled. So redrawing the table would put it back in tot he state that it would be.

    So actually yes, in this case fnPreDrawCallback is not suitable... (it could be modified to restore the state of the table, but I think that's probably not trivial, since it would need to reset filtering, sorting, paging etc).

    So in fact, the best way to pick up and intercept the paging change event is to have a custom pagination control that will do your check and then either go with it or not.

    http://datatables.net/development/pagination - how to create a paging plug-in
    http://datatables.net/plug-ins/pagination - already made plug-ins
    https://github.com/DataTables/DataTables/blob/master/media/src/ext/ext.paging.js - the build in paging methods

    Allan
  • frenzyfrenzy Posts: 21Questions: 0Answers: 0
    Thanks for the directions Allan. I indeed ended up with creating a paging plug-in, and it works great!
This discussion has been closed.