order() without draw()

order() without draw()

YanKonYanKon Posts: 2Questions: 1Answers: 0
edited July 2018 in Free community support

Hey,
is it possible to order the table but not output it again?
Because when I run the code below I always got the original order (column 0):

var table = $('#example').DataTable();
table.order( [ 1, 'asc' ] );
console.log(table.columns([0,1]).data());

I'd like to do some calculations in the background.
Thank you :)

edit: same problem with this code:

var table = $('#example').DataTable();
console.log(table.order( [ 1, 'asc' ]).columns([0,1]).data());

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @YanKon ,

    You need the draw to change the table - but if you just want to get an ordered array of column data for your background calculations, you can use sort() instead. This will order your data array, but not affect the table.

    Cheers,

    Colin

  • YanKonYanKon Posts: 2Questions: 1Answers: 0

    Hey Colin,

    but how can I sort the data with the second column?

    For example:
    I get with the command below, an array with two arrays. Now I want to sort both inside arrays with the second column

    table.columns([0,1]).data().sort()

    Cheers,

    Yannick

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Assuming you want to sort the data and not the table as Colin said, you'd need to pass in a sorting function to the sort() method. See the MDN documentation for how to use a compare function for an array sort.

    If you just want the DataTable to order the data over two columns do table.order([[0,'asc'], [1,'asc']]).draw();.

    Allan

This discussion has been closed.