HattrickNZ
HattrickNZ

Reputation: 4653

formatting cell values in datatables

Using this as an example how do I control the format of the values in the cells?

for example, how would I format the Extn. column to read 5,407 or 54.07?

Name    Position    Office  Extn.   Start date  Salary
Airi Satou  Accountant  Tokyo   5407    $2008/11/28 $162,700

I have been searching here and here but I can't quite work it out. can anyone advise?

I have tried something like this, but am having no success:

$(document).ready(function() {
    $('#example').DataTable( {
        data: dataSet,
        columns: [
            { title: "Name" },
            { title: "Position" },
            { title: "Office" },
            { title: "Extn." },
            { title: "Start date" },
            { title: "Salary" }
        ],
      "aoColumnDefs": [ {
                    "aTargets": [ 4 ],
                        "mRender": function (data, type, full) {
                                //var formmatedvalue=data.replace("TEST")
                            //return formmatedvalue;
                  return '$'+ data;
                            }
            }]
    } );
} );

Upvotes: 0

Views: 7454

Answers (1)

Shiffty
Shiffty

Reputation: 2156

Use the columns.render option.

Either with the built-in helper, to get a thousand seperator (5,407):

{
  title: "Extn.",
  render: $.fn.dataTable.render.number(',', '.', 0, '')
},

JSFiddle example

Or do it yourself with a custom function (54.07):

{
    title: "Extn.", render: function (data, type, row) {
        return data / 100;
    }

},

JSFiddle example

Upvotes: 2

Related Questions