Override specific default options

Override specific default options

JumpyJumpy Posts: 3Questions: 1Answers: 1

Hi,
I defined the default options, for example, like this:

$.extend(true, $.fn.dataTable.defaults, {
        columnDefs: [
            { orderable: false, searchable: false, className: "text-center", targets: 'ButtonColumns' }
        ]
    });

Now, for a specific table, I need to specify the responsivePriority:

var table = $('table').DataTable({
               columnDefs: [
                    { responsivePriority: 1, targets: 0 },
                    { responsivePriority: 2, targets: -1 }
                ]
            });

My problem is that the columnDefs property defined for the specific table overrides the default one.
Is there a way to override only the responsivePriority option?

This question has an accepted answers - jump to answer

Answers

  • JumpyJumpy Posts: 3Questions: 1Answers: 1
    edited May 2017 Answer ✓

    I returned to this question after a while and this is the solution I adopted:

    $.fn.dataTable.defaults.columnDefs.push(
                    { responsivePriority: 1, targets: 0 },
                    { responsivePriority: 2, targets: -1 }                
                );
    
    var table = $('table').DataTable();
    

    If there are more tables in the same page it can be used this solution:

    var customColumnDefs=$.extend(true, {}, $.fn.dataTable.defaults).columnDefs;
                    
                customColumnDefs.push(
                        { responsivePriority: 1, targets: 0 },
                        { responsivePriority: 2, targets: -1 }                
                    );
    
    var table = $('#table').DataTable({
        columnDefs:customColumnDefs
    });
    
    var anotherTable=$('#anotherTable').DataTable();
    
  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin

    Hi,

    Sorry I never managed to reply to you originally. That looks like an ideal solution - thanks for posting back and sharing it with everyone!

    Allan

  • JumpyJumpy Posts: 3Questions: 1Answers: 1

    Don't worry ;)

    I edited the post to add another solution for more tables in the same page.

This discussion has been closed.