Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1542
  • Last Modified:

Datatables jQuery Format Problem

I am using the datatables plug in and i want to show numerical data with 2 decimal places.

I am using similar php scripts that the author uses and as such, i don't to format the data at that stage.. i want to format the data client side

I have found a datatables article : http://datatables.net/forums/discussion/comment/23760 but it uses depreciated methods

I also want to produce my own calculated columns

Thanks
0
maddisoncr
Asked:
maddisoncr
  • 6
  • 2
1 Solution
 
maddisoncrAuthor Commented:
0
 
Rainer JeschorCommented:
Hi,
depending on your data / html source, the following option should work:
            "aoColumnDefs": [ 
            {
                "mRender": function ( data, type, row ) {

                    return parseFloat(data).toFixed(2);
                },
                "aTargets": [ 6 ]
            }

Open in new window

Just verify the column index - from the attached PDF it looks like column 7 (hence index 6).

Sample:
http://jsfiddle.net/EE_RainerJ/3frAT/

HTH
Rainer
0
 
maddisoncrAuthor Commented:
Thank you so much, i really appreciate it

How would i go about looking at calculated columns ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
maddisoncrAuthor Commented:
thanks again for that, it's working great

in terms of adding commas.. i presume i would still use the plug in as before ?
0
 
Rainer JeschorCommented:
Hi,
calculated columns should probably work the same.

I have updated the above sample:
http://jsfiddle.net/EE_RainerJ/3frAT/

Using this as mRender function:
            "aoColumnDefs": [ 
            {
                "mRender": function ( data, type, row ) {
                    var tempNumber = parseFloat(data.trim()).toFixed(2);
                    if (isNaN(tempNumber)) {
                        return "";
                    } else {
                    var sArray = (""+tempNumber).split('.');
                    return sArray[0].split(/(?=(?:.{3})+$)/).join(",") + "." + sArray[1];
                }
                },
                "aTargets": [ 3 ]
            }
        ]

Open in new window


HTH
Rainer
0
 
maddisoncrAuthor Commented:
Thanks so much, i'll test that this afternoon :-)
0
 
maddisoncrAuthor Commented:
Thanks again.. Brilliant
0
 
maddisoncrAuthor Commented:
Great help
0
 
plpitcherCommented:
{
                               "mRender": function (data, type, row) {
                                   return parseFloat('currentBalance').toFixed(2);
                               }
                           },
The above is not working for me.  What do I need to do?  The data type for 'currentBalance' is decimal.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now