1

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"}
        ]
    } );       
});
1
  • You need access to the data before you apply it to #utilisation. Maybe this can be done in a callback when DataTable gets the data. Inside this callback before it is sent to the target you can manipulate the strings. Commented Mar 16, 2016 at 12:22

1 Answer 1

3

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;
       }
    }
]
Sign up to request clarification or add additional context in comments.

Comments

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.