add/change callback after initialization, or else clone settings to re-build table?

add/change callback after initialization, or else clone settings to re-build table?

TwoMiceTwoMice Posts: 5Questions: 0Answers: 0
edited September 2010 in General
Hi,

I'm working with a framework that initializes a table with its own set of parameters which I don't have access to change. But I want to change the value of the fnDrawCallback function for this table, after the framework has initialized it.

Is this possible? The sample code below is simply not changing that setting but otherwise working well.

If this isn't possible, my next idea would be to copy the initialization settings from the table into a new var, then fnDestroy() the table, and re-initialize it with that saved settings configuration.

Is there a way to extract those initialization values from a table that's been initialized?

This is with DataTables 1.7.1.

Here is some code that attempts to change fnDrawCallback, with no luck. The table performs nicely, I assume because it's initialized on line 8. But the code at lines 16-23 seems to have no effect at all.
[code]






$(document).ready(function() {
$('table#table_id').dataTable();
});

<!-- Any code above is from an existing framework and not editable by me -->



$(document).ready( function () {
$('table#table_id').dataTable(
{
"bRetrieve": true,
"fnDrawCallback": function() {
alert( 'DataTables has redrawn the table' );
}
}
);
} );







NumberText



1-0Foo.
1-1Bar





[/code]

Thanks,
TM

Replies

  • allanallan Posts: 61,726Questions: 1Answers: 10,110 Site admin
    Hi TM,

    bRetrieve ( http://datatables.net/usage/options#bRetrieve ) is a flag which let's DataTables know that you acknowledge the fact that any information you pass to it in the object will not be applied, and that you simply want the object instance that was original created returned to you. This is why it's not having any effect.

    bDestroy ( http://datatables.net/usage/options#bDestroy ) on the other hand, will destroy the old table and initialise a new one for you, with the parameters you pass in. So switching to this variable sounds like what you want to do.

    It does mean that the table is initialised twice on page load though!

    Another option is to use the DataTables' internals (exposed through the API although not the "official public" API - i.e. it might change in future versions, although I have no plans to atm) to add a draw callback after the initialisation is complete:

    [code]
    $('#table_id').dataTable().fnSettings().aoDrawCallback.push( {
    "fn": function () {...},
    "sName": "user"
    } );
    [/code]
    would do it.

    Regards,
    Allan
  • TwoMiceTwoMice Posts: 5Questions: 0Answers: 0
    Hi Allan,

    Thanks for your response. Using fnDestroy() and re-initializing was kind of a last-resort idea. And anyway, that will only work for me if I can re-initialize with the exact same parameters, which I would have to copy from the existing table.

    But pushing another function onto aoDrawCallback is a really neat idea, and, by the way, does exactly what I'm looking for.

    From reading the sample code you gave, am I right to think that it actually adds on to whatever functions are already defined in fnDrawCallback, rather than just replacing them?

    Thanks,
    TM
  • allanallan Posts: 61,726Questions: 1Answers: 10,110 Site admin
    Hi TM,

    It is actually possible to re-initialise the table with the original settings object, and the destroy option... :-) With 1.7 I opted to save a pointer to the initialisation object, thinking it might prove to be useful at some point, and while it's not used in DataTables itself, it would work nicely here. oTable.fnSettings().oInit is the original object. When you can extend and pass back to DataTables with the bDestroy option.

    However, if the method described with aoDrawCallback does the job, then that's ideal. The array aoDrawCallback (and array of objects) is looped over when the draw function completes, executing each function in the array in turn. There are a number of actions that must be taken by the various DataTables components (paging etc) when the draw completes, so this is how it is done internally. With the dev defined fnDrawCallback, this is simply tagged on to the array during initialisation (basically like above).

    Regards,
    Allan
  • duranddurand Posts: 1Questions: 0Answers: 0
    Hey Allan,

    Thanks a lot for that comment on using aoDrawCallback, it helped me a lot! Thanks for datatables too ;)

    Durand
This discussion has been closed.