1

I am currently using Datatable in my laravel project for displaying my day configuration from my database. I would like to display my data in the following order, Monday -> Tuesday -> Wednesday etc.

Currently it is being ordered by alphabetical order from my database where the day column is store as string. Below are my javascript codes for my table.

 var ophrTables = $('#ophrs_table').DataTable({ 
            stateSave: true,
            columnDefs: [{
                    "searchable": false,
                    "orderable": false,
                    "targets": 0
            },{
                    "searchable": true,
                    "orderable": true,
                    "targets": 1
            },{
                    "searchable": false,
                    "orderable": false,
                    "targets": 2
            },{
                    "searchable": false,
                    "orderable": false,
                    "targets": 3
            }],
            order: [[ 1, 'asc' ]]
        });

2 Answers 2

0

Here are two approaches:


Use a Column Renderer

You can create a mapping from day names to numbers:

var days = { 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7 };

You can then use that mapping when you create your DataTable.

Here is my test data in my HTML table:

    <table id="example" class="display dataTable cell-border" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Day</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>Monday</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Tuesday</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Wednesday</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Thursday</td>
            </tr>
            <tr>
                <td>Airi Satou</td>
                <td>Friday</td>
            </tr>
            <tr>
                <td>Brielle Williamson</td>
                <td>Saturday</td>
            </tr>
            <tr>
                <td>Herrod Chandler</td>
                <td>Sunday</td>
            </tr>
        </tbody>
    </table>

Here is my DataTable definition:

$('#example').DataTable( {
  "columnDefs": [ {
    "targets": 1,
    "render": function ( data, type, row, meta ) {
      if (type === 'sort') {
        return days[data];
      } else {
        return data;
      }
    }
  } ]
} );

For the second column (index = 1), I use a render function to map from the name of the day to an integer. DataTables will use this integer for sorting operations (type === 'sort'). Otherwise it will use the name of the day.

The days[data] expression is used to look up the relevant number from my days variable.

The data looks like this when it is sorted by day name:

enter image description here

Warning:

When you use a renderer which produces numeric sort data from data which is alphanumeric, you do have to be careful. Even though numbers are used for sorting, they are treated as alphanumeric. In our case, this makes no difference, because the string values "1" through "7" are sorted the same way as the integer values 1 through 7.

But if you wanted to do something similar with the months of the year, then you would run into problems, as October (10), November (11) and December (12) would potentially be mis-sorted.

One fix for this is to force the column to be treated as if it contains numeric data by default: "type": "num",. Credit to this answer for highlighting this potential issue.

(Forcing the return value to be an integer does not help: return parseInt(days[data]);).


Delegated Sorting

An alternative approach is to populate the relevant number into an extra column when you load your data into the table.

In your DataTable definition, you can hide this column:

"columnDefs": [
  { "visible": false, "targets": 2 }
]

Then you can use the DataTables orderData option to delegate sorting from the visible "day name" column to the hidden "day number" column:

"columnDefs": [
  { "visible": false, "targets": 2 },
  { "orderData": [ 1 ],    "targets": 2 }
]

This tells DataTables to use the data in column index 2 when you sort on column index 1.

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

2 Comments

One more approach is to use DataTables' built-in support for HTML 5 custom attributes. Example: <td data-sort="1">Monday</td> This does not suffer from the same issue as the column renderer approach (see the warning in the answer for details).
Thanks for your detailed explanation! I have tried it out and it is working fine!
0

I don't think that you can order by day. May be you can put a hidden text in the datatable column.

Now

<td>Monday</td>
<td>Tuesday</td>
<td>Friday</td>

Change it to

<td><span style="display:none">1</span>Monday</td>
<td><span style="display:none">2</span>Tuesday</td>
<td><span style="display:none">5</span>Friday</td>

Once you change it to this style, you can order by day.

2 Comments

Hi, thanks for your input. However my data tables value are populated with the php foreach loop so I would need to populate the hiden span value into my loop as well.
Yes.. You should do that

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.