DataTables sort by non-orderable column

DataTables sort by non-orderable column

RomiCRomiC Posts: 3Questions: 0Answers: 0

Hi, everyone!

I've faced with a problem (or probably feature). When I make the first column of a table non-orderable via data-orderable="false" attribute, and don't setup default order, DataTable sorts data by this field anyway. Here is an example. IMO, more appropriate behavior will be sorting by first "orderable" column instead of first one.

Replies

  • RomiCRomiC Posts: 3Questions: 0Answers: 0

    Looks like data-order='[]' attribute could solve this problem. Although, the default behaviour looks weird to me.

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
    edited February 2020

    For ajax sourced data this is not the default behavior. If you don't specify "order" like in here:

    //order: [[ 2, 'desc' ]],
    

    you get the data in the exact sequence the server returns the data to the client.

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Yep, by default the sorting occurs on the first column, whether ordering is permitted or not. If you want to change that order, you can use order,

    Colin

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    ...mhhh... that surprises me a little.

    Since Editor doesn't support it I built my own "SELECT DISTINCT" and "ORDER BY" using PHP like this:

    ->on( 'postGet', function ( $e, &$data, $id ){ 
       $data = array_values(array_unique($data, SORT_REGULAR));
       array_multisort( array_column($data, "serial"), SORT_ASC,
                        array_column($data, "update_time"), SORT_ASC,
                        $data );
    })
    

    Without specifying "order" on the client side this provides me with exactly the ordering expected which is not on the first column of the Data Table.

    I do this because I need the "orginal order" (the one from the server) for my Excel export:

    exportOptions: {
        modifier: { order: 'index' } //order as returned from the server
    }
    

    to get my Excel export right while for the Data Table I want to implement a different order which is this one:

    order: [[ 2, 'desc' ]],
    
  • RomiCRomiC Posts: 3Questions: 0Answers: 0

    @rf1234, I have the same problem with ajax-data as well. Sorry, couldn't provide you with an example. But based on the source code I see no different between ajax and non-ajax data in this particular case.

    @colin, thank you for your answer. Suppose, this is the way I should follow. So, it isn't a bug, but feature.

This discussion has been closed.