3

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..

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! It's hard to find what you are looking for in their documentation
God damnit, exactly what I was looking for!
Perfect! Nice! You saved me!
I didn't know about this way to sort one column from different data. I always used to hide one column then fix the js code with targets. This way is far more easy to understand!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.