Datatable custom sorting conditional to the values of first column
Datatable custom sorting conditional to the values of first column

Hi, I'm trying to figure if it's possible to sort multi-columns of the datatable, based on the value of the first column.
I've verified all the post to find something similar but without success ...
For example, my first column has one of these fourth possible values, "New|1, Active|2, Closing soon|3, closed|4".
I can order the entire table, by using the ==> orderMulti: true, order: [[0, 'asc'], [7, 'asc'], [6, 'asc'], [9, 'asc']], properties.
But this kind of ordering is global for each rows of the datatable.
What I want to accomplish is something like,
//Datatable configuration
export const getTableConfig = () => {
return {
searching: false,
paging: false,
retrieve: true,
info: false,
//defaultIndexColumnSorted: 6,
orderMulti: true,
// I know this is not working code, i just want to image the the scenario
order: function (row) {
if (row.status === '1') {
return "[[0, 'asc'], [7, 'asc'], [6, 'asc'], [9, 'asc']]";
} else if (row.status === '2') {
return "[[0, 'asc'], [7, 'asc'], [5, 'asc'], [7, 'asc']]";
} else if (row.status === '3') {
return "[[0, 'asc'], [6, 'asc']]";
} else {
return "[[0, 'asc'], [5, 'asc'], [6, 'desc']]";
}
} ...
I hope I'm enought clear on what I'm expecting. I know this one is a particular request.
Thanks
This question has an accepted answers - jump to answer
Answers
I forget to tell that I'm using DataTables-1.10.16 ...
Don't think you will be able to create ordering groups based on row data like this. Are you allowing the user to sort the rows? One option may be to use
order
(order: []
) to set the initial order the order Datatables receives the data and order the rows the way you want in your server script.Kevin
Kevin, I understand what you've mentionned. The problem is if I've to order the data by two or three columns, for a particula row's value, this mean that the data will need to be ordered two or three times by server script, because the js function could only takes 2 parameters. A custom solution should be spliting the data by four categories eqivalent to the first column value, sorting each categories of data separately and at the end, join all the data back together? But this mean having four datatables config, not optimal?
Thanks
Datatables doesn't have a mechanism to order some rows by a certain set of columns and other rows by other columns. Not sure what you are using for your server script or DB but if using SQL you could get creative with the SELECT statement in order to sort each
status
by the desired fields. So its one query with one set of data for one Datatables config.Stack Overflow would be a good place to start to find DB queries that might help.
Kevin