Start event AFTER a redraw

Start event AFTER a redraw

ApolymoxicApolymoxic Posts: 15Questions: 4Answers: 0

I am trying to start an event after a redraw, but only when the redraw is complete.

I have a pretty big data set, and I have a custom filter in place that allows people to search for particular items. Sometimes, the search will return a large amount of data (sometimes upwards of 50k lines), which the system handles just fine. But I am trying to kick off an event (specifically, a data export) AFTER the data has been retrieved.

I've looked at https://datatables.net/reference/option/drawCallback, and I think this could be useful. My dilemma is knowing when exactly to export... I don't want it to export data after every draw.

So, is it possible to call the drawCallback at a certain time and not on every draw? Or is there, perhaps, something in the draw function that would let me know when it's complete?

Thank you in advance!

Answers

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    So, is it possible to call the drawCallback at a certain time and not on every draw?

    You could use a flag in drawCallback to determine if you want to execute certain code in the function. Its meant to run after every draw. If you only want to run something once after initialization you can use initComplete.

    Or is there, perhaps, something in the draw function that would let me know when it's complete?

    AFAIK this is called once the table draw is complete. Are you finding something different?

    Kevin

  • ApolymoxicApolymoxic Posts: 15Questions: 4Answers: 0

    The way that I want this to be working is that I filter the data, draw the table, and THEN export.

    But what's happening is that it filters the data, attempts to draw and export at the same time, which sometimes makes the export not work correctly.

    So I was trying to make it where the export wouldn't even happen until after the table was drawn.

    You said
    You could use a flag in drawCallback to determine if you want to execute certain code in the function.

    Does this mean that I could pass in some argument that say to run it only when the export has been attempted? How does that look?

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited September 2022

    Does this mean that I could pass in some argument that say to run it only when the export has been attempted?

    No. You would use a global variable.

    The way that I want this to be working is that I filter the data, draw the table, and THEN export.

    That helps to understand what you are trying to do. Maybe this example will help:
    http://live.datatables.net/kalofamu/1/edit

    It uses draw and creates a one time event handler using jQuery one().

    If this doesn't help please update or provide a test case showing what you mean be draw and export run at the same time. Unless you have some sort of asynchronous process happening I don't see how they can run simultaneously.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • ApolymoxicApolymoxic Posts: 15Questions: 4Answers: 0

    Oo. I think that will work. If that doesn't work, I think I could do the global variable - I hadn't thought about that.

    I will give those two suggestions a shot and let you know how it goes.

    Thank you!

  • ApolymoxicApolymoxic Posts: 15Questions: 4Answers: 0

    I decided against the one method as, if I am understanding it correctly, would be a one time deal. There are times when the export (which is running as a separate process) would need to be done multiple times.

    My export function kicks off a server side PHP method which generates an excel spreadsheet, places the file on the server in a temp location, and redraws the table. In order to force the download, I open up a new tab, start the download, and then close the tab (I am sure there are other methods out there, but this is the one I have chosen).

    The export and the data pull for the redraw happen at the same time, but unfortunately, that meant that the new tab opening also happened at the same time. If the data pull was not complete around the same time, the download would not start.

    To correct, I used the global variable idea and the drawCallback to kick off the download when the export (and redraw) was complete.

    drawCallback: function() {
        if (globalThis.export_data) window.open('api/export-file.php', '_blank');
    }
    

    Thank you very much for your help! I truly appreciate it.

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    Ah, yes that make sense. You will need either the global variable or a callback that is called once the server side function is complete. Glad its working.

    Kevin

Sign In or Register to comment.