1

I'm using jQuery Datatables buttons extension.

       "buttons": [
            {
                extend: 'colvis', //column visibility
                className: 'data_export_buttons'
            },
            {
                extend: 'print',
                className: 'data_export_buttons',
                exportOptions: {
                    columns: ':visible'
                }
            },
            {
                extend: 'excel',
                className: 'data_export_buttons',
                exportOptions: {
                    columns: ':visible'
                }
            },
            {
                extend: 'csv',
                className: 'data_export_buttons',
                exportOptions: {
                    columns: ':visible'
                }
            },
            {
                extend: 'pdf',
                className: 'data_export_buttons',
                exportOptions: {
                    columns: ':visible'
                }
            }
        ]

Everything works, however, I would like to use the above code as a call-back function. So instead of repeating the same lines across all my tables (I have over 15), I would just call the function like this:

   var table = $('#table').DataTable({ 
        "paging": true,
        "info": false,
        "ordering": true,
        "columnDefs": [
            { "targets": [0], "orderable": false}
        ],
        export_data() // the call-back function for colvis and export button extensions
    });

I have tried this but it didn't work. My table lost data. Is there a way to achieve this?

1 Answer 1

1

Yep, you can't do a callback there, but a just as tidy way would be to create a variable, something like

myButtons = [
        {
            extend: 'colvis', //column visibility
            className: 'data_export_buttons'
        },
        // snip
    ];

Then, when you create your tables:

   var table = $('#table').DataTable({ 
    "paging": true,
    "info": false,
    "ordering": true,
    "columnDefs": [
        { "targets": [0], "orderable": false}
    ],
    "buttons": myButtons
});
Sign up to request clarification or add additional context in comments.

3 Comments

I'm afraid that didn't work. Tried that and my table lost data
I don't understand that, sorry, you shouldn't lose data. Take a look at these two examples - this has the buttons declared in the iniitialisation (live.datatables.net/zafakupu/1/edit), and this, within a variable (live.datatables.net/xelavora/1/edit). All it's doing is using a variable to avoid repetition.
Hi colin117, I was able to get it to work by removing the double quotes around the DataTable parameters. So instead of "buttons", I have buttons, taking cue from the links you shared. I don't understand the reason for that behavior but it works, and that works for me. Thanks for your answer and the links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.