How to extend default properties?

How to extend default properties?

markzzzmarkzzz Posts: 49Questions: 8Answers: 1

I've these default settings for every DataTables within my project:

$.extend( true, $.fn.dataTable.defaults, {
    "searching": false,
    "ordering": false,
    buttons: {
        dom: {
            button: {
                tag: 'i',
                className: ''
            }
        },
        buttons: [
            {
                extend: 'excel',
                className: 'far fa-file-excel grid-button export',
                text: '',
                exportOptions: {
                    columns: ':visible:not(.no-export)',
                    modifier: {
                        page: 'all'
                    }
                },
                title: null
            }
        ]
    }       
});


$(document).ready(function() {
    $('.basicDataTable').DataTable();
});

Now, for my $('.paymentsDataTable'), I want to keep these properties, and add/extend some new ones, such as stripNewlines: false within the exportOptions object, without rewriting the whole buttons (already defined with its custom properties), or add customize/format functions:

exportOptions: {
    stripNewlines: false,
    format: {
        body: function (data, row, column, node) {
            // data manipulation

            return data;
        }
    }
},

customize: function (xlsx) {
    var sheet1 = xlsx.xl.worksheets['sheet1.xml'];

    $('row c[r^="E"]', sheet1).attr('s', '55');
}

If I do somethings like this:

$(document).ready(function() {
    $('.paymentsDataTable').DataTable({
        buttons: {
            buttons: [
                {
                    exportOptions: {
                        stripNewlines: false,
                        format: {
                            body: function (data, row, column, node) {
                                // data manipulation

                                return data;
                            }
                        }
                    },

                    customize: function (xlsx) {
                        var sheet1 = xlsx.xl.worksheets['sheet1.xml'];

                        $('row c[r^="E"]', sheet1).attr('s', '55');
                    }
                }
            ]
        }
    });
});

it obviously clean the default settings, and replace only these properties.
I need to extend the defaults without rewriting it.

How can I do it?

This question has an accepted answers - jump to answer

Answers

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1
    edited July 2018 Answer ✓

    Nevermind,

    I risolved it placing the custom settings I need before placing the DataTable:

    $.extend(true, $.fn.dataTable.defaults, {
        buttons: {
            buttons: [
                {
                    // my extended data
                }
            ]
        }
    });
    

    Thanks anyway ;)

This discussion has been closed.