How to make order() return multiple ordering columns

How to make order() return multiple ordering columns

cinsidecinside Posts: 2Questions: 1Answers: 0
edited September 2015 in Free community support

According to manual: https://datatables.net/reference/option/order ,
"function returns array of arrays containing information about the currently applied sort. This 2D array is the same format as the array used for setting the order to apply to the table (see below)".

When using "orderData" option to set multiple columns sorting, order() function still returns only one element in array.
You may try to execute this code on the page, example of use orderData option ( https://datatables.net/examples/basic_init/multi_col_sort.html ):
$("#example").DataTable().order();
If you didn't change the order of columns, the result would be:
[[0, "asc"]],
but according to "orderData" option result should be:
[[0, "asc"], [1, "asc"]]

This question has an accepted answers - jump to answer

Answers

  • cinsidecinside Posts: 2Questions: 1Answers: 0

    After closer look, I found that order() returns "aaSorting" property, which is a "Sorting that is applied to the table" value, but it doesn't include "orderData" columns, which are included to the sorting rules on every sorting event by function "_fnSortFlatten". This function returns extended multi-dimension array with next fields:

    1 col

    2 dir

    3 formatter

    4 index

    5 src

    6 type

    In my case I need only "col" and "dir" fields, so to get return value in same format as API order() function, it is easy to implement the next new API function:

    $.fn.dataTableExt.oApi.allSorters = function (oSettings) {
        var rules = this.oApi._fnSortFlatten(oSettings);
        var sorters = [];
        
        for (var i = 0, len = rules.length; i < len; i++)
        {
            sorters[i] = [rules[i].col, rules[i].dir];
        }
        
        return sorters;
    };
    

    I could miss the other way to solve this problem, so it would be great if someone share with me the other (true) way :)

  • allanallan Posts: 61,761Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Yup - perhaps the built in API should be extended to be able to get the full ordering array. I'll look into that - thanks for the suggestion.

    Allan

This discussion has been closed.