table.column(colIdx).data().sort(), to sort by columnDefs:data:sort?

table.column(colIdx).data().sort(), to sort by columnDefs:data:sort?

Mike_MikeMike_Mike Posts: 6Questions: 1Answers: 0
edited September 2014 in DataTables 1.10

Summary

I want to access the column().data() in the sort order defined for that column.


my table parameters includes defining a hidden sort column, so that when sorting target column "5", it is sorted by the value in column "10". - this works fine for sorting inside the the table.

inTable= $('#tasks').DataTable( {
             "ajax": "data",
              "columnDefs": [ 
                 {"targets": 5,  
                  "data" : {
                        "_" : "5",
                        "sort": "10"
                         }
                 }]
              })

The issue is that I am trying to create a column filter and using the below javascript to fill in the select options. They fill in the right values, but I would like the sorting to be by column 10.

      inTable.columns( '.select-filter' ).eq( 0 ).each( function ( colIdx ) {
          var select = $('<select />');
          ...
          inTable
              .column( colIdx )
              .sort()     //***How do we get this to sort according to columnDefs?***
              .data()
              .unique()   
              .each( function ( d ) {
                  select.append( $('<option value="'+d+'">'+d+'</option>') );
              } );
      })

I would like the .column(colIdx).data() sorted according to the {"data": "sort" : "10"} parameter defined in the table init.

Any ideas? the .column(colIdx).sort() will only sort the same as how the table is sorted, instead of sorted by the sort column "10"

Answers

  • Mike_MikeMike_Mike Posts: 6Questions: 1Answers: 0
    edited September 2014

    example code (look at select for salary column) is here:
    http://www.datatables.net/examples/api/multi_filter_select.html

    Note, the salary column filter select is incorrectly sorted.

  • Mike_MikeMike_Mike Posts: 6Questions: 1Answers: 0
    edited September 2014

    I found an extremely ugly work around. Resort the table before creating each select. This seems like a lot processing. Any better ideas?

    inTable.order([ colIdx, 'asc' ]).draw();    //THIS IS PROBABLY BAD
    
    `inTable
    `  .column( colIdx )
    `  .data()
    `  .unique()
    `  .each( function ( d ) {
    `      select.append( $('<option value="'+d+'">'+d+'</option>') );
    `  } );
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    I want to access the column().data() in the sort order defined for that column.

    Use:

    inTable.column( colIdx, { order: 'current' } ).data();
    

    :-)

    The selector-modifier option can be very useful for this kind of thing.

    Allan

This discussion has been closed.