Is there a way to format numbers without knowing column indices ahead of time?

Is there a way to format numbers without knowing column indices ahead of time?

pandaEaterpandaEater Posts: 6Questions: 0Answers: 0
edited April 2013 in General
I'm relatively new to DataTables and having trouble determining if it's possible to render all unformatted numbers to comma delimeted format (e.g. 1000000 -> 1,000,000) without knowing the table layout before hand. The reason I ask is because I'm using datatables to show the results of a number of different user selected queries and writing custom formats for each different query would be very tedious.

Thanks!

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    You could use a class target using aTargets - define the class on the header cell and DataTables will target that.

    Allan
  • pandaEaterpandaEater Posts: 6Questions: 0Answers: 0
    edited April 2013
    I'm not completely clear on how I would implement the method you mentioned above but I was thinking an option would be to use mRender on every column and figure out numbers vs text with my own formatting function doing something like this:

    [code]
    var colDefs = [
    {
    "aTargets": [0 ... N],
    "mRender": function (data, type, row) {
    if (type === 'display') {
    return format(data);
    }
    else if (type === 'filter') {
    return data + ' ' + format(data);
    }
    return data;
    }
    }
    ];
    [/code]

    Is there a value that can be assigned to aTargets to have it affect all columns?
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    > Is there a value that can be assigned to aTargets to have it affect all columns?

    Yes `"_all"` would do it. But I worry it might be a little slow depending on how much data you have.

    Here is an example using aTargets with a class: http://live.datatables.net/uqugik/edit#javascript,html

    Allan
  • pandaEaterpandaEater Posts: 6Questions: 0Answers: 0
    It still seems like I would have to know the columns ahead of time using the aTargets method. I'm passing data to the aaData and aoColumns properties to fill the table. No information can be gained from reading the names of the column headers so I may have to stick with the "_all" and mRender method unless you can think of a more efficient alternative?
This discussion has been closed.