Questions regarding sorting - sort order / initial sort column / changing data-sort value

Questions regarding sorting - sort order / initial sort column / changing data-sort value

enigmacloughenigmaclough Posts: 8Questions: 3Answers: 0

I have a table which will have a varying number of columns, but one column will always be present albeit in a different position every time depending on the data to be displayed (eg it could be column 2, column 3, column 4 etc). This column I want to be set as the initial sorted column and sorted DESC. The data is populated with checkboxes and as a result the data-sort attribute is being used to sort the data - with 1 for checked, 0 for unchecked.

What I need to be able to do is the following and I am not sure how to achieve it on a column that could be in any position (ie have a varying index). If there was a static number of columns then this would be fine as I would be able to set the options accordingly knowing the column index, but as the column would be a different number depending on the table content then I need to reference the column accordingly. I need to:

  • Set initial sorting column as this column;
  • Set the sort order as DESC.

  • UNTESTED * - I also need to change the data-sort accordingly when the checkbox is checked / unchecked. I haven't tested as yet but if an answer is available I would appreciate the feedback - does datatables pick up changes to the data-sort attribute and adjust the sorting accordingly?

TIA

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,310Questions: 26Answers: 4,948

    Does order() do what you want?

    Kevin

  • enigmacloughenigmaclough Posts: 8Questions: 3Answers: 0

    @kthorngren unfortunately not as I still need to use the index of the column, which varies every time the table is created.

  • kthorngrenkthorngren Posts: 21,310Questions: 26Answers: 4,948

    unfortunately not as I still need to use the index of the column, which varies every time the table is created.

    I not familiar with another way to set the initial column to sort. How are you applying the data-sort attribute? Maybe from that code you can get the column index.

    does datatables pick up changes to the data-sort attribute and adjust the sorting accordingly?

    You need to use row().invalidate() or cell().invalidate() after updating to indicate the change. Then using draw() Datatables will read the DOM due to the invalidated data.

    Kevin

  • enigmacloughenigmaclough Posts: 8Questions: 3Answers: 0

    @kthorngren thanks again for your response. The data-sort is a HTML5 data attribute. It allows you to set a sort value which can differ from the content of the cell (such as in my case where the cell contains a checkbox which can't be sorted, so I use data-sort set to 1 (checked) or 0 (unchecked). HTML5 data-* attributes - cell data

    It appears as though I may have to look for another way (custom HTML5 data attributes maybe?) and iterate each column detecting the attributes and then using logic to apply options to the datatable such as sort column and sort order.

    Thank you for the answer regarding row().invalidate() / draw(). This will work perfectly for my needs.

  • enigmacloughenigmaclough Posts: 8Questions: 3Answers: 0

    As I could not find any way to achieve this using datatables I have done the following:

    The HTML

    For columns that need to be sorted add a data-order attribute with 'asc' or 'desc'.

    <th data-order="desc">Column Heading</th>
    

    The Scripting

    var ordering = [];
    var theTable = jQuery( '#example' ).DataTable();
    theTable.columns().every( function( i ) {
        var header = this.header();
        if ( jQuery( header ).is( '[data-order]' ) ) {
            var dataOrder = jQuery( header ).attr( 'data-order' );
            var theOrder = [ i, dataOrder ];
            ordering.push( theOrder );
        }
    });
    if( ordering.length > 0 ) {
        theTable.order( ordering ).draw();
    }
    
  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    http://live.datatables.net/ninacake/1/edit sets the sort order before the table DataTable is created. Is this what you are trying to accomplish?

  • enigmacloughenigmaclough Posts: 8Questions: 3Answers: 0

    @bindrid Thank you!!! This is exactly what I was looking for.

This discussion has been closed.