Default columnDefs Behavior

Default columnDefs Behavior

CollectonianCollectonian Posts: 1Questions: 1Answers: 0

Currently on DataTables 2.03. In our base JavaScript, we update the defaults like this:

$.extend( $.fn.dataTable.defaults, {
            autoWidth: true,
            columnDefs: [
                { className: 'text-start', targets: [ 'text-start' ] },
                { className: 'text-center', targets: [ 'text-center' ] },
                { className: 'text-end', targets: [ 'text-end' ] },
                { width: '50px', targets: [ 'width-50'] },
                { width: '100px', targets: [ 'width-100' ] },
                { width: '150px', targets: [ 'width-150' ] },
                { width: '200px', targets: [ 'width-200' ] },
                { width: '250px', targets: [ 'width-250' ] },
                { width: '300px', targets: [ 'width-300' ] },
                { orderable: false, targets: [ 'no-sort' ] },
            ],
            pagingType: 'simple_numbers',
            processing: true,
            responsive: false,
            searchHighlight: true,
            language: {
                paginate: {
                    first: 'First',
                    last: 'Last',
                    next: 'Next',
                    previous: 'Previous'
                },
                search: 'Filter Table:',
            }
        });

This lets us set up various column stylings that we use regularly across the many DataTables in our app.

My question though is if we have a table where we need to add something unique, such as this:

    $('#data-list').DataTable({
        columnDefs: [
            { width: "175px", targets: [ 4,5 ] },
        ],
        order: [ [5, 'desc'], [1, 'asc'] ],
        drawCallback: toggleDataTableControls
    });

Will the defaults be combined with the new ones, or will only the new ones apply? If only the new ones apply, is there an easy way to pull in the defaults to avoid duplicating the options?

Answers

  • kthorngrenkthorngren Posts: 21,545Questions: 26Answers: 4,988

    I just tried it and the default options are overwritten by the Datatables config options. This example shows the Name column with the assigned class blue but the defaults aren't applied.
    https://live.datatables.net/redetiju/1/edit

    $.fn.dataTable.defaults is a Javascript object which can be manipulated before initializing Datatables to add more defaults. This example adds class blue to Name and class green to Age to the defaults using a for loop:
    https://live.datatables.net/hulupasi/1/edit

    Kevin

Sign In or Register to comment.