Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Datatables jQuery Format Problem

Posted on 2013-11-13
9
Medium Priority
?
1,411 Views
Last Modified: 2016-03-02
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
Comment
Question by:maddisoncr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39646300
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
 

Author Comment

by:maddisoncr
ID: 39647453
Thank you so much, i really appreciate it

How would i go about looking at calculated columns ?
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:maddisoncr
ID: 39647481
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
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 2000 total points
ID: 39647581
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
 

Author Comment

by:maddisoncr
ID: 39651106
Thanks so much, i'll test that this afternoon :-)
0
 

Author Comment

by:maddisoncr
ID: 39665233
Thanks again.. Brilliant
0
 

Author Closing Comment

by:maddisoncr
ID: 39665235
Great help
0
 

Expert Comment

by:plpitcher
ID: 41489578
{
                               "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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question