Sort table not based on values in dataset
Sort table not based on values in dataset
Good morning,
I am trying to sort a table, based on values, which are combined in one Column. For instance I have this render function for the column:
render: function (val, type, row) {
return row['date1'] + ' - ' + row['date2'];
}
Server side I have this:
Editor::inst( $db, 'table' )
->fields(
Field::inst( 'date1'),
Field::inst( 'date2'),
Field::inst( 'description'),
Field::inst( 'xyz' )
...
This is working, but now I would like to sort the table either by date1 oder by date2, which are both in on column.
Additionally, I would like to have those sort-options in a separate dropdown, so that sorting is not done by clicking on the the table head, but by selecting the desired value in a dropdown.
Thank you!
Ben
Answers
ok, I think I found a solution:
I added date1 and date2 as columns with
visible: false;
so they won't affect the table. Then I am binding the following to the change of the dropdown:
table.order( [ 3, 'asc' ] ).draw()
(3 in this case is the index of the invisible date1 column).
Is this the right approach or is there a better one?
Thank you!
Ben
Hi Ben,
That looks good to me. The problem with the concatenated dates is that DataTables will just see a string and unless you use some kind of plug-in, it won't know what to do with it.
Allan