Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get jQuery DataTables to sort on hidden value, but search on displayed value?

I have a simple DataTables grid which contains date columns. I have provided two values for the date in my JSON data set, one for display and one specifically designed so that DataTables can sort it. My web application allows users to choose a bunch of different date formats, so it needs to be flexible.

This is my JSON data that DataTables gets from from the web server via sAjaxSource.

{   Reports : [     { Date: { Sort = "20101131133000", Display : "11/31/2010 1:30 PM" } },      { Date: { Sort = "20100912120000", Display : "1200 EST 2010-09-12" } },    ] } 

It is easy to tell DataTables to sort based on the Date.SortValue property and to make the Display property visible to the user by using fnRender(). So this gets me halfway to my goal.

var dataTableConfig = {   sAjaxSource: "/getreports",   sAjaxDataProp: "Reports",   aoColumns: [     { mDataProp: "User" },     { mDataProp: "Date.Sort",        bSortable: true,        sName: "Date",        bUseRendered: false,        fnRender: function (oObj) {         return oObj.aData[oObj.oSettings.aoColumns[oObj.iDataColumn].sName].Display;       }     }   ] }; 

Here's my problem. I want to allow the user to enter a filter (using the built-in filter input that DataTables provides) based on the displayed value, but they cannot.

For example. If a user entered "EST", they would get zero results because datatables filters based on the value specified in mDataProp not based on the value returned from fnRender.

Can anyone help me figure out how to sort AND filter a date column? Thanks.

like image 791
jessegavin Avatar asked Oct 05 '11 21:10

jessegavin


People also ask

How do I trigger a DataTable search?

So: var table = $('#myTable'). DataTable({ responsive: true, serverSide: true, ajax: { url: myUrl, dataSrc: '' }, fnServerData: function (sSource, aoData, fnCallback, oSettings) { oSettings. jqXHR = $.

What is Aocolumns in jquery DataTable?

aoColumnDefs: This array allows you to target a specific column, multiple columns, or all columns, using the aTargets property of each object in the array (please note that aoColumnDefs was introduced in DataTables 1.7).

What is aaSorting in DataTable in jquery?

The aaSorting parameter is an array of arrays where the first value is the column to sort on, and the second is 'asc' or 'desc' as required (it is a double array for multi-column sorting).

How does DataTable sorting work?

Using the order initialisation parameter, you can set the table to display the data in exactly the order that you want. The order parameter is an array of arrays where the first value of the inner array is the column to order on, and the second is 'asc' (ascending ordering) or 'desc' (descending ordering) as required.


2 Answers

I believe the existing answers are deprecated due to updates to DataTables. HTML5 supports attributes that DataTables can use to easily sort columns. Specifically the data-sort attribute. (See https://datatables.net/examples/advanced_init/html5-data-attributes.html)

I can easily sort tables like so:

<table>   <thead>     <tr>       <td>Name</td>       <td>Age</td>     </tr>   </thead>   <tbody>     <tr>       <td>John Doe</td>       <td data-sort="37">2/1/78 (37 years old)</td>     </tr>     <tr>       <td>Jane Doe</td>       <td data-sort="35">12/1/80 (35 years old)</td>     </tr>   </tbody> </table> 

It doesn't matter that the 'Age' column contains numbers, symbols, and letters, DataTables will sort using the 'data-sort' attribute.

like image 85
Aaron C. de Bruyn Avatar answered Sep 19 '22 17:09

Aaron C. de Bruyn


This is an old post, but hopefully this will help someone else that comes here.

In a more recent version of DataTables, bUseRendered and fnRender are deprecated.

mRender is the new way of doing this sort of thing and has a slightly different approach.

You can solve your issue with something along the lines of:

... { mDataProp: "Date.Sort"   bSortable: true,    sName: "Date",    // this will return the Display value for everything   // except for when it's used for sorting,   // which then it will use the Sort value   mRender: function (data, type, full) {     if(type == 'sort') return data.Sort;     return data.Display   } } ... 
like image 29
CWSpear Avatar answered Sep 17 '22 17:09

CWSpear