fnPreDrawCallback revert settings ?
fnPreDrawCallback revert settings ?
SimonR
Posts: 10Questions: 0Answers: 0
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 ?
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 ?
This discussion has been closed.
Replies
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.
Allan
It would need an event triggering at the very begging of every handler that could potentially trigger a call to fnDraw or fnReDraw.
Allan
Anyway thanks for the assist Allan !