Is it possible to let an ajax request decide to export?

Is it possible to let an ajax request decide to export?

HakunHakun Posts: 4Questions: 1Answers: 0

Description of problem:
I am using the export functionality of Datatables which is really awesome. However, I've ran into difficulties that I can't find an anwer to in this forum or elsewhere.
I am logging an export to an audit table. This is done via an ajax request.

If this request fails I want this to stop the export.

What I am failing to discover is if I can halt the export activity until the ajax has received a successful response.
Is this possible?

Code:

let sendToAudit = (function(title, type, count) {
    $.ajax({
        url: Routing.generate('log_export'),
        type: 'POST',
        data: {
            csrf:  $('.table').data('export-csrf'),
            title: 'export,
            type: 'csv/xls'
        }
    }).done(function (data) {
        console.info(data); // REQUIRED: Allow export button to appear
    }).fail(function (data) {
        console.error(data); // REQUIRED: Don't allow button to appear
    });
});
module.exports = {
    'csv': function(tableColumns, exportColumns, li) {
        var rejected = rejection(li, '<i class="fad fa-lg fa-file-csv"></i> CSV file');
        if (rejected !== false) {
            return rejected;
        }
        return {
            extend: 'csvHtml5',
            text: '<i class="fad fa-lg fa-file-csv"></i> CSV file',
            exportOptions: {
                customizeData: function (d) {
                    sendToAudit(title, type);
                },
                columns: (function() {
                    if (exportColumns === null) {
                        exportColumns = [];
                        for (var col in tableColumns) {
                            if (tableColumns[col].hasOwnProperty('csv')) {
                                exportColumns.push(col);
                            }
                        }
                    }
                    return exportColumns;
                })(),
                format: {
                    body: function ( data, row, column, node ) {
                        data = formatHtml(data);
                        return data;
                    }
                },
                orthogonal: 'display'
            },
        };
    }

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Answer ✓

    At the moment, there isn't a built in way to do that I'm afraid, although I do like the idea.

    At the moment what you would have to do is replace the default action function of the button with your own which will make the Ajax call and then if it is successful call the original action function. The final example of this page shows how you can call the default action function of a button.

    Allan

  • HakunHakun Posts: 4Questions: 1Answers: 0

    Thank you for your quick reply!

    I spent hours pondering if this was possible and your answer has helped tremendously.

  • HakunHakun Posts: 4Questions: 1Answers: 0

    Hi Allan, could I bother you with a follow-up question?

    But first, I got it to work for CSV export with your help :smiley: !!
    Only issue is I have is when doing the same to the XSLX export, the xslx variable passed to the customize function is a garbled string of text...

    e.g. YEsundefinedLiverpool City Councilundefinedpending some weird contingency rubbishundefined5 months

    It's like a delimiter was replaced with an undefined?

    The code is very basic :(

                customize: function( xlsx ) {
                    const sheet = xlsx.xl.worksheets['sheet1.xml']; // xslx is now a broken string and has no properties
                    let count = $('row', sheet).length;
                },
    

    Thanks in advance, I am unsure how to go from here

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Answer ✓

    It's like a delimiter was replaced with an undefined?

    Or perhaps not supplied. It sounds like a join without a value. Can you link to a page showing the issue so I can see how it is configured and the structure of the data?

    Allan

  • HakunHakun Posts: 4Questions: 1Answers: 0

    Hi Allan, it was my mistake. I had mixed up excelHthml5 with csvHtml5, false alarm!

    Thanks for you and your team's invaluable work

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Great to hear you've got it working :)

    Allan

Sign In or Register to comment.