EatingTooMuch
EatingTooMuch

Reputation: 15

Adding text to json data in Datatables

I'm trying to add text to my data before creating a table with jQuery DataTables.

For example, my JSON data is [1,5,6,12] and I want to present it as [1 seconds, 5 seconds, 6 seconds, 12 seconds].

JavaScript:

$(document).ready(function () {
    $('#utilisation').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'print'
        ],           
        'ajax': {
            "type": "POST",
            "url": '../Servlet?',
            "dataSrc": ""
        },
        'columns': [
            {"data": "router"},
            {"data": "local"},
            {"data": "startdate"},
            {"data": "enddate"},
            {"data": "duration"}
        ]
    } );       
});

Upvotes: 1

Views: 1011

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58860

Use columns.render option to render the data for use in the table.

'columns': [
    {"data": "router"},
    {"data": "local"},
    {"data": "startdate"},
    {"data": "enddate"},
    {
       "data": "duration",
       "render": function(data, type, row, meta){
          if(type === 'display'){
             data = data + ((data == 1) ? " second" : " seconds");
          }
          return data;
       }
    }
]

Upvotes: 3

Related Questions