Home > front end >  Append an object inside the datatable function
Append an object inside the datatable function

Time:12-03

Need to append an object inside the datatable function. I've the below code in multiple pages in my application. I trying to append an object to this function from a common JS file across the application.

var table;
$(document).ready(function() {
table = $('#table').DataTable({ 
    dom: 'lBfrtip',
    lengthMenu: [
        [ 10, 25, 50, 100, -1 ],
        [ '10', '25', '50', 100, 'Show all' ]
    ],
    "processing": true, //Feature control the processing indicator.
    "serverSide": true, //Feature control DataTables' server-side processing mode.
    "columnDefs": [
    { 
        "targets": [ -1 ], //last column
        "orderable": false, //set not orderable
    },
    ],
});
});

I tried pushing the buttons property to the DataTable function like below in the common JS file:

table.push({buttons: [
    {
        text:'Save as PDF',
        className: "btn btn-primary",
        extend: 'pdfHtml5',
        download: 'open',
    }
]
});

But getting this error:

Uncaught TypeError: Cannot read properties of undefined (reading 'push')

CodePudding user response:

Try adding the buttons to the dataTable.defaults object:

$.extend(true, $.fn.dataTable.defaults, {
    buttons: [
        {
            text:'Save as PDF',
            className: "btn btn-primary",
            extend: 'pdfHtml5',
            download: 'open'
        }
    ]
});
  • Related