13

I use the latest Datatables plugin 1.10 version.

I have 3 columns (0 , 1, 2). Columns 1 and 2 contain numbers which should be formatted like:

1000 -> 1.000
10000 -> 10.000

I searched the documentation and I found these relevant functions:

https://datatables.net/reference/option/formatNumber

https://datatables.net/reference/option/language.thousands

Are the columns that need to be formatted detected automatically?

What is the correct usage of the above functions?

3
  • 1
    you should use regex inside mRender on your targets[2] and format the number respectively with regex and bind it to table depending on your requirement . ex: "render": function(data) {//return formatted value;} }, Commented Dec 11, 2014 at 10:46
  • 1
    @supercool this works. u can write an answer to mark as accepted Commented Dec 12, 2014 at 12:45
  • @gosom What regex did you use for this? Commented Feb 2, 2022 at 13:26

3 Answers 3

42

There is actually an even easier way to do this, also found on the datatables documentation:

        "columns": [
            { "data": "ReceiptQuantity", render: $.fn.dataTable.render.number(',', '.', 2, '') },
            { "data": "ReceiptPrice", render: $.fn.dataTable.render.number(',', '.', 2, '') },
            { "data": "LineTotal", render: $.fn.dataTable.render.number(',', '.', 2, '') }
        ],
Sign up to request clarification or add additional context in comments.

4 Comments

Here's the documentation: datatables.net/manual/data/renderers#Built-in-helpers. I was having some difficulty locating this, so thought the link might help others.
Filter initialization crashes and i recieve Uncaught TypeError: Cannot read property 'mData' of undefined.
Perfect. It's fine for me this solution :)
It works, also the ordering by column works after number formatting.
5
$('#table-dg').dataTable({
    "columns": columnNames,
    "columnDefs": [
        {
            "render": function (data, type, row) {
                 return commaSeparateNumber(data);
            },
            "targets": [1,2]
        },
    ]
});

function commaSeparateNumber(val) {
    while (/(\d+)(\d{3})/.test(val.toString())) {
        val = val.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
    }

    return val;
}

2 Comments

Welcome to stackoverflow! When answering questions, it's best to add a description to the answer code you have provided.
It works for formatting numbers but it brokes the ordering by column: all values will be ordered in a wrong order. @JustLearning's answer works for me.
4

As mentioned in my comment you have to do something like this

Inside Datatable initialization :

 "aoColumnDefs": [ {
      "aTargets": [ 2 ],
    "mRender": function (data, type, full) {
     var formmatedvalue=data.replace(//regex expression)
      return formmatedvalue;
    }
}]

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.