How to reverse orderSequence globally and then override it with a class name

How to reverse orderSequence globally and then override it with a class name

jmiresjmires Posts: 2Questions: 1Answers: 0

I'm working on a large project that will have many DataTables (using the latest version - 1.10.7). I'm trying to set reasonable defaults in a global file, and override specific settings for specific tables with class names or data-* attributes so HTML authors don't have to mess with JavaScript on individual tables.

One thing I want to do is make the default ordering for all columns be ["desc", "asc"]. That part is simple enough with:

var table = $(".datatable").DataTable({
    "columnDefs": [
        {
            "targets": '_all', // default to descending sort first
            "orderSequence": ['desc','asc']
        }
    ]
});

But then if I try to also override it for some columns with a class name, this does not work:

var table = $(".datatable").DataTable({
    "columnDefs": [
        {
            "targets": '_all', // default to descending sort first
            "orderSequence": ['desc','asc']
        },
        {
            "targets": 'sort-asc-first', // sort some columns ascending first
            "orderSequence": ['asc','desc']
        }       
    ]
});

An example of that not working is in this JSFiddle.

I have also tried various ways of trying to set this in the global defaults using asSorting, column.orderSequence, etc. but I can't get it to override the ["asc", "desc"] default

$.extend($.fn.dataTable.defaults, {
    searching: false, // no search box
    paging:  false, // no paging
    info: false // no table summary info
    /* What can I do here to set default orderSequence to ["desc", "asc"] ? */
});

Many thanks for anyone who can offer some direction.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Answer ✓
        $.extend(true, $.fn.dataTable.defaults, {
            searching: false, // no search box
            paging:  false, // no paging
            info: false, // no table summary info
            column: {
                orderSequence: ['desc','asc']
            }
        });
    

    The column defaults are in column. Note that you have to set the first parameter of $.extend to true to make it a deep copy. Otherwise the column object given will overwrite all of the defaults!

    Allan

  • jmiresjmires Posts: 2Questions: 1Answers: 0

    Ah, thank you so much - that solved it perfectly. I had missed the true parameter on $.extend. Thanks for the very fast answer, and for such a terrific plugin!

This discussion has been closed.