Datatables, trying to use the currency format renderer inside conditional statement

Datatables, trying to use the currency format renderer inside conditional statement

hondaman900hondaman900 Posts: 7Questions: 2Answers: 0

I have a Javascript implementation of Datatables in a Laravel 6 (PHP) app. It's not published anywhere yet so I can't link a "test case". I'm trying to show a blank cell (as opposed to $0.00) when the value is zero, but otherwise render the value as currency.

In my $(document).ready function where the Datatables code defines the datables, the following line works fine to render the cell data as currency:

{data: 'expense_amount', name: 'expense_amount', render: $.fn.dataTable.render.number( ',', '.', 0, '$' )},

but if I try to do that inside a conditional statement I get an error:

{data: 'expense_amount', name: 'expense_amount', render:function ( data, type, row, meta ) { if (data == 0){ return ''; }else{ return render: $.fn.dataTable.render.number( ',', '.', 0, '$' ); }}},

The error is "Unexpected token ':' "

I've searched exhaustively and can't find a remedy. I'm obviously applying the rendering function incorrectly, but can't find any reference to using this inside an IF statement.

Any assistance is very much appreciated.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    edited September 2021 Answer ✓

    I don't think the .display() method of the number renderer is documented anywhere but you can find it on the forum, if you know what to look for :smile:

    The render: in the return statement is a syntax error. Remove that. You will need to use the .display() method with the number renderer. Something like this:

    return $.fn.dataTable.render.number( ',', '.', 0, '$' ).display( data );

    Kevin

  • hondaman900hondaman900 Posts: 7Questions: 2Answers: 0

    Perfect! That worked :) Many thanks - never would have found that solution.

Sign In or Register to comment.