Tabulator - download to csv

Hi Stu,

I am still using your original Tabulator that you created in 2020. In this dependency, one could download data into csv using
table.download(“csv”, “data.csv”)

However, I would like to catch the Blob file before triggering saving action. Tabulator allows using a downloadEncoder (codes copied from Tabulator)

var table = new Tabulator(“#example-table”, {
downloadEncoder:function(fileContents, mimeType){
//fileContents - the unencoded contents of the file
//mimeType - the suggested mime type for the output

    //custom action to send blob to server could be included here

    return new Blob([fileContents], {type:mimeType}); //must return a blob to proceed with the download, return false to abort download
}

});

Could you help me understand how I could enable this in the Tabulator dependency? Many thanks for your help in advance.

Huarong

Hi @huarong and welcome to the forum,

Some admin:
I moved the question to a new thread since it was a new question

when formatting code it’s helpful to use code formatting markdown

```javascript
// insert your code here

```

For Tabulator specific questions it’s usually best to visit the github discussions page here and ask the question there.


One of the motivating factors for upgrading tabulator was to make this sort of thing doable and straight forward.
It isn’t supported - and won’t be supported in version 1.

Worth noting that Tabulator v2 currently uses Tabulator JS version 5.1 and when reading tabulator.info documentation you should switch to whichever version is currently being used by the Anvil Tabulator dependency.
downloadEncoder isn’t available in Tabulator JS v5.1 but downloadReady is and is probably sufficient.

(I am planning to upgrade to version 5.3 but haven’t got round to finishing that)
(Tabulator v1 uses Tabulator JS v4.6)

In Tabulator v2 your code will look something like this:

from tabulator.Tabulator import Tabulator

Tabulator.modules.add("Download")
Tabulator.modules.add("Export")

class Form1(Form1Template):
    def __init__(self, **properties):
        ...
        self.tabulator.options = {
            "download_ready": self.download_ready
        }

    def download_ready(self, contents, blob):
        # contents - the unencoded contents of the file
        # blob - the blob object for the download
        # custom action to send blob to server could be included here e.g.
        anvil.server.call("save_download", anvil.js.to_media(blob))

        # must return a blob to proceed with the download, return false to abort download
        return blob


    def button_1_click(self, **event_args):
        """This method is called when the button is clicked"""
        self.tabulator.download("csv", "data.csv")

Many thanks for your kind reply, Stu.

I have made quite a few custom changes in Tabulator v1 dependency package so would prefer to stay on this version for now.

If I want to enable download_ready in Tabulator v1 dependency package, is there a quick way to do so?

Best to move the discussion to here at this stage, it will depend on which version you’re running.