Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How DataTables determine columns type

Tags:

datatables

I have a dynamic table enhanced by jQuery DataTables, which display a custom object similar to this example.

JSON:

{
  "data": [
    {
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": {
        "display": "SomeString",
        "timestamp": 1303686000
      },
      "office": "Edinburgh",
      "extn": "5421"
    },
    // ... skipped ...
]}

JavaScript:

$(document).ready(function() {
    $('#example').DataTable( {
        ajax: "data/orthogonal.txt",
        columns: [
            { data: "name" },
            { data: "position" },
            { data: "office" },
            { data: "extn" },
            { data: {
                _:    "start_date.display",
                sort: "start_date.timestamp"
            } },
            { data: "salary" }
        ]
    } );
} );

The difference is that I dynamically build the columns configuration, because the columns can be in any order, and others columns can be added or removed from the list. For this example (my case is very similar) the problem is that for some reason the timestamp property is ordered as a String instead of being ordered as a number.

I discovered that after setting the column "type" as "number" the ordering works perfectly. I'm presuming that DataTables is auto detecting the column as "String" for some reason (maybe because the display element is a string).

How does DataTables set the type of the columns, when is not explicitly declared?

Edit 1

I made a sample code to show the problem http://jsfiddle.net/Teles/agrLjd2n/16/

like image 852
Rafael Teles Avatar asked Aug 28 '15 18:08

Rafael Teles


People also ask

How can you identify a column in a DataTable?

By using the Column name or Column index we can identify a column in a data table.

What is FixedColumns in DataTables?

FixedColumns provides the option to freeze one or more columns to the left or right of a horizontally scrolling DataTable. This allows information in the fixed columns to remain visible even when scrolling through the data set. This can be particularly useful if you wish to show a large number of columns.

What is autoWidth in DataTable?

The autoWidth option is used to specify whether the automatic calculation of the column width in the DataTable is enabled or not. This calculation takes a little time and may be disabled when the column widths are explicitly passed using the columns.


1 Answers

jQuery DataTables has built-in mechanism for type detection. There are multiple predefined functions for various types with fallback to string data type.

It's also possible to use third-party plug-ins or write your own.

There are multiple ways to specify data type, below are just the few.

SOLUTION 1

Use type option.

$(document).ready(function() {
    $('#example').DataTable( {
        ajax: "data/orthogonal.txt",
        columns: [
            { data: "name" },
            { data: "position" },
            { data: "office" },
            { data: "extn" },
            { data: "start_date.display", type: "date" },
            { data: "salary" }
        ]
    } );
} );

SOLUTION 2

Use returned JSON data for type detection, see columns.data for more information.

$(document).ready(function() {
    $('#example').DataTable( {
        ajax: "data/orthogonal.txt",
        columns: [
            { data: "name" },
            { data: "position" },
            { data: "office" },
            { data: "extn" },
            { data: {
                _:    "start_date.display",
                sort: "start_date.timestamp",
                type: "start_date.timestamp",
            } },
            { data: "salary" }
        ]
    } );
} );
like image 173
Gyrocode.com Avatar answered Oct 05 '22 23:10

Gyrocode.com