Formating a rendered number of a column

Formating a rendered number of a column

carrarachristophecarrarachristophe Posts: 125Questions: 29Answers: 2

Hello,
I have a number column that I am rendering that way:

{data: "reporting_header.balance_CLBD_amount_amount", render: DataTable.render.number(null, null, 2), className: 'dt-body-right' }

But I must add a condition statement which calculate the value of the column:

{data: null, render: function ( data, type, row ) {
     if (data.reporting_header.balance_CLBD_credit_debit_indicator === 'DBIT') {
          return -1 * data.reporting_header.balance_CLBD_amount_amount;
     } else {
          return data.reporting_header.balance_CLBD_amount_amount;
     }
}}

How can I add to the second code the formating of the first?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 22,310Questions: 26Answers: 5,128
    Answer ✓

    You can either use Javascript toFixed() or chain .display( .... ) to the number renderer like this:

    return DataTable.render.number(null, null, 2).display( data.reporting_header.balance_CLBD_amount_amount );
    

    AFAIK the .display( ... ) method is undocumented. I remember it from an old thread.

    Kevin

  • allanallan Posts: 65,298Questions: 1Answers: 10,826 Site admin

    It is yes. The number renderer is handy for dealing with multi-locale tables (i.e. displaying in the locale format that the end user would expect), but if you just want some simple formatting like fix precision, then toFixed() would be the way to do it.

    Allan

  • carrarachristophecarrarachristophe Posts: 125Questions: 29Answers: 2

    Dear both,
    thanks for your prompt reply. I applied it that way and it looks like it worked like a charm:

    {data: null, render: function ( data, type, row ) {
         if (data.reporting_header.balance_CLBD_credit_debit_indicator === 'DBIT') {
              return DataTable.render.number(null, null, 2).display( -1 * data.reporting_header.balance_CLBD_amount );
         } else {
              return DataTable.render.number(null, null, 2).display( data.reporting_header.balance_CLBD_amount );
              }
         }
    }
    
Sign In or Register to comment.