Datatables - format one of the columns as a currency

Related searches

I have a simple table with header and I'm using a DataTables plugin for it:

$('#uc-dashboard-top-customers .uc-dashboard-sortable').dataTable({
  'aaSorting': [[1, "desc"]],
  'sPaginationType': 'full_numbers'
});

In second column I have numerical values like 124.012, 9, 8.15, 16.5048 and so on. Now I need to order that column by numerical value, but format them as a currency, e.g 1224,21 €. I could format the values server side, but then it will be sorted as a string by Datatables.

Is there a way to define format for specific column?

Why not use a sorting plugin like currency? Basically it does nothing but extracting the float value out of a string, suitable for any kind of currency formats :

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "currency-pre": function ( a ) {
        a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
        return parseFloat( a );
    },
    "currency-asc": function ( a, b ) {
        return a - b;
    },
    "currency-desc": function ( a, b ) {
        return b - a;
    }
} );

var table = $('#example').DataTable({
  columnDefs : [
   { targets: 0, type: 'currency' }
  ]
})  

demo -> http://jsfiddle.net/zfkcj8g2/

A sorting plugin works regardless of how the dataTable is initialised, i.e DOM tables, JSON / array sourced or serverside.

Currency formatting — DataTables forums, Hello, I've been struggling a little bit with European currency formatting and ordering the columns. Right now I have the following: Table init. Could you give me a link to the page so I can debug it directly please. if that isn't possible, can you give show me the data that tableData contains.. If the data contains numbers that are number types (rather than a string) then I would suggest you use the built in number renderer which will do all the formatting for you and also will correctly handle sorting.

This should give you an idea of how to initialize the datatable. You will need to tweak it as necessary but it should certainly put you on the right track.

$('#uc-dashboard-top-customers .uc-dashboard-sortable').dataTable({
  'aaSorting': [[1, "desc"]],
  'sPaginationType': 'full_numbers',
    columns:   
        [{  
            data: "Field1",
            type: "date",
            width: "10%"
        }, {  
            data: "Field2",
            width: "10%"
        }, {  
            data: "Field3",
            type: "html",
            width: "10%"
        }, {  
            data: "Field4",
            width: "30%"
        }, {  
            data: "Field5",
            width: "10%"
        }, {  
            data: "Field6",
            render: $.fn.dataTable.render.number(",", ".", 2),
            width: "10%"
        }]
});

Can't format last column as currency — DataTables forums, are 2 days that I'm trying to format the last column of my datatable as currency, with no success. pedrocsa Posts: 9Questions: 1Answers: 0. The datatables buttons.html5.js file has a regex that matches $ amounts and applies a dollar format to them (which is unfortunately after the number, but that's a different topic). That regex doesn't look for dot as a thousand separator or comma for the decimal, it looks for the opposite.

In case you want to format a calculated field in datatables

I do a lot of financial applications and like to use the 'numeral' javascript library:

<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>

Here's the calculated column before formatting as currency:

{ 
  "data": null,                                                         
  "render": function(data,type,row) { return (data["Revenue"] - data["Expenses"]) }
} 

Here's the calculated column formatting with the numeral library:

{ 
"data": null, 
 "render": function(data,type,row)
   { 
    return (numeral(data["Revenue"] - data["Expenses"]).format('$0,0.00'))
   }
}

Datatables - format one of the columns as a currency, I have a simple table with header and I'm using a DataTables plugin for it: $('#uc- dashboard-top-customers .uc-dashboard-sortable').dataTable({ 'aaSorting': [[1� Adding formatting. In our DataTable, if we wish to have a column that shows the price, it is relatively common to wish to prefix it with a currency sign. In this case we use a dollar sign (see also the built-in number renderer below which provides advanced formatting options):

formatCurrency: Format table columns in DT: A Wrapper of the , Format numeric columns in a table as currency (formatCurrency()) or column E to 3 decimal places datatable(m) %>% formatCurrency(1:2,� Format numeric columns in a table as currency (formatCurrency ()) or percentages (formatPercentage ()), or round numbers to a specified number of decimal places (formatRound ()), or a specified number of significant figures (formatSignif ()). The function formatStyle () applies CSS styles to table cells by column.

Currency, percentage and other columns - wpDataTables, These entrees will not affect sorting, filtering, or any other column functions. ExampleDemo table with currency columns. E.g. for� Hi, I am trying to format a column in a datatable using AddColumns and giving the format The source column in the SP list is a currency In the List in SharePoint it shows as a currency e.g. £41.40 here is my AddColumns code: AddColumns(TrackMySalesCollection, "Item Description", Descriptio

Format numeric columns in a table as currency (formatCurrency ()) or percentages (formatPercentage ()), or round numbers to a specified number of decimal places (formatRound ()), or a specified number of significant figures (formatSignif ()). The function formatStyle () applies CSS styles to table cells by column.

Comments
  • How is the table being populated?
  • It is generated by php, why?
  • If you are familiar with datatables specify the columns in the initialization and for currency use the following: render: $.fn.dataTable.render.number(",", ".", 2),
  • How exactly could I use it in my code?
  • This works, however for my older version of datatables I had to use: "aoColumnDefs": [ { "sType": "currency", "aTargets": [ 1 ] } ]