Enabling DeferRender in Datatable improved performance but fails in exporting data.

Enabling DeferRender in Datatable improved performance but fails in exporting data.

puneet.jayeepuneet.jayee Posts: 18Questions: 8Answers: 0
edited July 2019 in Free community support

Hi,

I am using data table to load huge amount of data and making "deferRender to true has helped to improve the performance but I have custom code to export the data to excel including input text column.

When I try to export to excel it fails ONLY WHEN DeferRender IS TRUE.

Kindly help

Here is the code-

table = $('#table').DataTable({
                "ajax": {
                    "url": "../API/GETALLITEMS",
                    "type": "GET"
                },
                "deferRender": true,
                dom: 'T<"clear"><"pull-right"B>lfrtip',
                lengthChange: true,
                buttons: [
                    {
                        extend: 'excelHtml5', text: 'Export to Excel', className: "btn-primary", exportOptions: {
                            format: {
                                body: function (data, row, column, node) {
                                    if (column === 11) {// Column 11 is a INPUT TEXT
                                        debugger;
                                        var id = '#' + node.firstChild.id; // **It is a input text and fails here only when the deferRender is true but working fine when deferRender is false.**
                                        if ($(id).val()) {
                                            return $(id).val();
                                        }
                                        else {
                                            return "";
                                        }
                                    }
                                    else {
                                        return data;
                                    }
                                }
                            }
                        },
                        action: function (e, dt, node, config) {
                            $.fn.dataTable.ext.buttons.excelHtml5.action.call(this, e, dt, node, config);
                        }
                    },
                ],

Thanks!
Puneet

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi @puneet.jayee ,

    It's working as expected here. Could you look at that, please, and see if it helps. If it's still not working for you, please can you update my example, or link to your page, so that we can see the problem.

    Cheers,

    Colin

  • puneet.jayeepuneet.jayee Posts: 18Questions: 8Answers: 0

    Hi Colin,

    Sorry If I was not clear but if you see the below line from the code mentioned in my original request-

    var id = '#' + node.firstChild.id; // It is a input text and fails here only when the deferRender is true but working fine when deferRender is false.

    The export does fails with the below error-
    Uncaught TypeError: Cannot read property 'firstChild' of undefined.

    It only fails when exporting data with input text and deferRender is true.

    Thanks!
    Puneet

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi @puneet.jayee ,

    Yep, that row hasn't been created - so the input elements won't be present. You can check for this with something like

    if ($(id).length > 0) {
    

    and if not, put something default in there instead.

    Cheers,

    Colin

  • puneet.jayeepuneet.jayee Posts: 18Questions: 8Answers: 0
    edited July 2019

    Thanks for the response Colin.

    How to make sure that all the rows has been created before exporting to excel while deferRender is true?

    Is this bug in datatable libraries?

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

    No - this isn't a bug in DataTables. You are attempting to access DOM elements which haven't been created, which you've explicitly told DataTables not to create through the use of the deferRender option.

    How to make sure that all the rows has been created before exporting to excel while deferRender is true?

    You'd have to have the use show all of the rows!

    Simply put, using deferRender is not compatible with exactly what it is that you are trying to do I'm afraid. I'd suggest removing it in this case.

    Allan

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    How to make sure that all the rows has been created before exporting to excel while deferRender is true?
    Is this bug in datatable libraries?

    This is expected behavior with deferRender. From the deferRender docs:

    Note that when enabled, it goes without saying that not all nodes will always be available in the table

    Kevin

  • puneet.jayeepuneet.jayee Posts: 18Questions: 8Answers: 0

    Thanks everyone for the help and response.

This discussion has been closed.