fnPreDrawCallback revert settings ?

fnPreDrawCallback revert settings ?

SimonRSimonR Posts: 10Questions: 0Answers: 0
edited November 2013 in DataTables 1.9
Hi all,
I'm having a bit of a problem here, I'm trying to implement my own editable datatable, using server side processing, creating input elements with mRender depending on the server's data and most importantly posting back the data before fetching the new one.

To achieve that I surrounded the table with a form, post it back to the server in fnServerData with ajax and on success I go fetch the new data.

All was fine until I reach the validation part, basically I added validation to the inputs and wanted to prevent any action, page change, sorting, number of rows displayed, on DT until the currents rows were all valid.

As far as I understand it the earliest callback we have access to is fnPreDrawCallback and I can use it prevent DT from redrawing, depending on the validation result of the form.

The problem is the DT's setting will still change even if the table isn't redrawn, thing like the current page will increment, the sorting will change etc etc, even if it doesn't affect the displayed data.

I was about to dig into the code to implement an even earlier call back in DT, but I was wondering if it wasn't possible to simply restore the settings in fnPreDrawCallback to their earlier state ?

Replies

  • SimonRSimonR Posts: 10Questions: 0Answers: 0
    Well I settled for trickery instead, by adding a earlier event handler to all the link, buttons etc. I can prevent the corresponding actions from happening.

    This will add an handler to be executed first
    [code]
    //Bind an even to be the first in line
    $.fn.bindUp = function(type, fn) {
    type = type.split(/\s+/);
    this.each(function() {
    var len = type.length;
    while( len-- ) {
    $(this).bind(type[len], fn);

    var evt = $.data(this, 'events')[type[len]];
    evt.splice(0, 0, evt.pop());
    }
    });
    };
    [/code]

    This will prevent the event to reach DT handlers
    [code]
    function eventValidity(event){
    if(!$('#SaveForm').valid()){
    event.stopImmediatePropagation();
    }
    [/code]

    And now all there is left to do, is to hook that up to DT
    [code]
    //Rebind for validation
    $('.dataTables_length select').bindUp('change',eventValidity);
    $('.dataTables_paginate a').bindUp('click',eventValidity);
    $('.dataTable th').bindUp('click',eventValidity);
    [/code]

    And voila, no more setting change
    I still would like to know if it's possible to revert the settings to a previous state however.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    I'm afraid trickery is what is needed at the moment. This is quite a bit flaw in fnPreDrawCallback in that it isn't currently possible to revert the settings since it doesn't have a clue what has changed. Possibly the best that can be does is to utilise the state saving, but I've not yet implemented that or come up with a better way I'm afraid.

    Allan
  • SimonRSimonR Posts: 10Questions: 0Answers: 0
    I was thinking of using state saving as well, but there is still the problem of when to save, since the earliest event in the flow is fnPreDrawCallback and that's when the state should be restored (wouldn't that trigger a redraw btw ?).

    It would need an event triggering at the very begging of every handler that could potentially trigger a call to fnDraw or fnReDraw.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Yeah - we'd need to hang on to the current state and the previous state, since as you say the current state would be a reflection of the new draw state. Also I'd completely agree, it would need to call, likely, fnDraw for a full redraw to be on the safe side, since filtering or sorting much have been changed which would need to be cancelled. Sounds a bit messy!

    Allan
  • SimonRSimonR Posts: 10Questions: 0Answers: 0
    So trickery it is then, well I say trickery, doesn't look too shabby to me, I mean it's just a matter of preventing changes from happening rather than reverting the state, sounds better to me, or maybe that's just pride talking.

    Anyway thanks for the assist Allan !
This discussion has been closed.