Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap datatable sort column based on another column

I have a column which is calculated based on another date column to show in a years/months/days format. I also want this column to be sorted as the date column but since it's a string it is sorting differently.

enter image description here

Here is my code to for datatable config.

            "columnDefs": [{  // set default column settings
                'orderable': false,
                'targets': [1, 9]
            }, {
                "searchable": false,
                "targets": [1, 9]
            }],
            "order": [
                [5, "asc"]
            ] // set first column as a default sort by asc

Is there any way I can attach the sort for column 6 "At Work" to column 5?

Thanks for the help..

like image 696
Anupam Avatar asked Feb 20 '26 10:02

Anupam


1 Answers

Ok found the answer to the question.

Datatable provide a way to sort orthogonal data by letting you add data-order which will determine the sort order rather then the value contained by the table cell.

Below is the example (from dataTable documentation) which suggests how to do it :)

<tr>
    <td data-search="Tiger Nixon">T. Nixon</td>
    <td>System Architect</td>
    <td>Edinburgh</td>
    <td>61</td>
    <td data-order="1303682400">Mon 25th Apr 11</td>
    <td data-order="3120">$3,120/m</td>
</tr>

So in my case I have to add timestamp to table cell using data-order to sort it correctly.

like image 78
Anupam Avatar answered Feb 24 '26 09:02

Anupam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!