stateSaveCallback apparently fired in unnecessary cases

stateSaveCallback apparently fired in unnecessary cases

TraderToolsTraderTools Posts: 6Questions: 2Answers: 0

Hello,
I have couple of DataTables in my application and I allow to move and hide columns in them.
I use stateSaveCallback to save the state in a database.
Recently I found that there are too many DB hits for save the state and it turned out that actually each table draw fires this callback: function _fnDraw( oSettings ) calls _fnCallbackFire( oSettings, 'aoDrawCallback', 'draw', [oSettings] ); .
It looks like a bug to me - I expected this callback invocation only when user changed the table structure.
Is there any way to overcome this issue?

Thanks.

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    page numbering is included in the saved state so it will change every time you page.

  • TraderToolsTraderTools Posts: 6Questions: 2Answers: 0

    But callback executed on every draw. For example, when I open "New" dialog. I would expect some optimizations, for example to invoke it if paging changed.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    @bindrid is correct - the paging information is included in the state object, so it does need to be included on basically every draw.

    It would be possible to put in some optimisation to check if there are any values changed, but that isn't something that DataTables does at the moment - instead assuming that generally when a draw happens, the state will have changed in some way (otherwise the draw likely wouldn't have happened - the exception I can think of for that is Editor, when it will redraw the table on edit to take account of any data changes).

    If you are using stateSaveCallback and this is causing problems for you, one option would be to do that difference check in that function. I can put an example of that together if you like.

    Perhaps another interesting question would be to know why it is doing a draw? If you are using Editor's New button, clicking that certainly shouldn't cause it to redraw.

    Regards,
    Allan

  • TraderToolsTraderTools Posts: 6Questions: 2Answers: 0

    With regard to draw as the result of pressing "New" button please forget it - it is my bug, caused by a requirement to put it inside table header to save real estate. So when dialog was open the sort occurred as well... But it is not main issue, since I show data, that updated very often, of course each time causing redraw.

    It would be great if you post an example for state comparison. If it is possible, please take in account that I use plugins: ColReorder, Column Visibility and yadcf, which also have their state inside.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    This example shows how it could be done. I haven't used the extensions, but that all just call the draw function anyway, so they won't make any difference.

    I've used the object comparison method from here to detect the changes. You could use the easier JSON.stringify, but that assumes that the browser will also construct the object with the parameters in the same order. Normally it will - its a trade-off between simplicity of code and edge cases.

    Regards,
    Allan

  • TraderToolsTraderTools Posts: 6Questions: 2Answers: 0

    Thanks for the idea.

This discussion has been closed.