jquery datatable sorting on multiple columns including one hidden column which is always by desc

jquery datatable sorting on multiple columns including one hidden column which is always by desc

freeeverfreeever Posts: 2Questions: 1Answers: 0

I have a jQuery datatable with multiple columns including one hidden column ("Updated Time"), and the requirement is: when users click on one column such as "Type", the result will be sorted by "Type" + "Updated Time", and, no matter the "Type" is sorted by "asc" or "desc", the sorting direction for "Updated Time" must always be "desc". I have no issue with sorting by multiple columns as following:

      "columnDefs": [ {
        "targets": [ 0 ],
        "orderData": [ 0, 6 ]
      }, {
        "targets": [ 1 ],
        "orderData": [ 1, 6 ]
      }, {
        "targets": [ 2 ],
        "orderData": [ 2, 6 ]
      }, {
        "targets": [ 3 ],
        "orderData": [ 3, 6 ]
      }, {
        "targets": [ 4 ],
        "orderData": [ 4, 6 ]
      } ],

The above column #6 is the hidden column "Updated Time". This solution only satisfy sorting by the 2 columns at the same time with the same direction, which is either both "asc" or both "desc". Is there any way to keep the sorting direction for "Updated Time" always "desc", and can chang the clicked column to sort by "asc" or "desc"? Your help will be really appreciated!

Answers

  • freeeverfreeever Posts: 2Questions: 1Answers: 0

    Found the solution. This can be done using the existing API of "orderFixed".

    For my case, the post fixed sorting is the solution:

    "orderFixed": {
         "post": [ 6, 'desc' ]
     },
    

    And the following original multiple column sorting is NOT needed:

            "columnDefs": [ {
                "targets": [ 0 ],
                "orderData": [ 0, 6 ]
              }, {
                "targets": [ 1 ],
                "orderData": [ 1, 6 ]
              }, {
                "targets": [ 2 ],
                "orderData": [ 2, 6 ]
              }, {
                "targets": [ 3 ],
                "orderData": [ 3, 6 ]
              }, {
                "targets": [ 4 ],
                "orderData": [ 4, 6 ]
              } ],
    
This discussion has been closed.