Need to get the table order column and direction when user click on column sort

Need to get the table order column and direction when user click on column sort

Manikantha RManikantha R Posts: 7Questions: 1Answers: 0
edited April 2023 in Free community support

Hi Team,
I disabled default first column sort order by Appling order[ ] for the table

downloadHistoryTable = $('#myFavorites').DataTable({
        responsive: true,

        processing: true,
        serverSide: true,
        renderer: "bootstrap",
        pagingType: "full_numbers",
        paging: true,
        pageLength: 9,
        lengthMenu: [
            [3, 6, 9, 12],
            [3, 6, 9, 12]
        ],
         order: [], 

        columnDefs: [{
            targets: disableSortColumns,
            orderable: false
        }],   

Until user click sort option in header i have set sortColumn="_score" and Sortdirection="desc"

Now i need to capture the table order event when user click on sorting of the column for example if user click 2nd column i need to get the 2nd column title and direction

I tired with order event like below

$('#myTable').on( 'order.dt', function () {
    var ordering = $('#myTable').DataTable().order();
    console.log( 'Table ordering changed: ' + JSON.stringify(ordering) );
} );

but this function is executing every time irrespective click.

can some one provide how i can achieve this

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    order doesn't just trigger when the user clicks a header - it does so whenever DataTables needs to order the data.

    If you just need a click, add a click listener to the header.

    That said, is it a problem to have it operating on order? If you just want to show a title with the ordering column for example, then this should be fine. I don't really know what it is you are trying to achieve overall.

    Allan

  • Manikantha RManikantha R Posts: 7Questions: 1Answers: 0

    Hi
    In your use case we need to do sorting at server side.
    So before use click sort header we need to send sort parameter like _score and desc as payload in ajax call.

    Now when user click we need to capture the sort column and direction and need to send to ajax call payload

    Let me know how we can achieve this

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    When server-side processing is enabled (which it is here), DataTables will send the sorting information to the server for every draw request, as described in the manual for server-side processing.

    Does that not contain the information you need? I'm not clear on what _score is or what it is expected to do.

    Allan

  • Manikantha RManikantha R Posts: 7Questions: 1Answers: 0

    Hi Allan,

    By default i not required soring on page load of table so i have set order to empty array like order[].
    at that time i am sending different sort part to server called _score and direction is desc.

    Now when user clicks on header of th i need to capture the title of column and sort direction i am able to get the sortId but not able to get direction

    $('#myFavorites thead th').on('click', function() {

    var sortId= $(this).data('sortid');

  • Manikantha RManikantha R Posts: 7Questions: 1Answers: 0
    edited April 2023

    I found the solution to get the sort order and direction

     var api = new $.fn.dataTable.Api(settings);
    
                     var order = api.order();
            if (order && order.length > 0) {
            var sortColumnName = api.columns().header()[order[0][0]];
    
           _order_sortDir = order[0][1];
    
    
    

    Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Sign In or Register to comment.