Render number on computed value

Render number on computed value

bindridbindrid Posts: 730Questions: 0Answers: 119

Simple question:

I see how you use the formatters such as

columns:[
            {
                data: 'valueA', render:  $.fn.dataTable.render.number(',', '.', 0, '$');
              }
            }
]

However, I am trying to use it on a computed number. Its easy enough to do this but I was wondering if there is a way to actually leverage what is already there. Something like

columns:[
            {
                data: null, render: function(rowData){ 
                    var sum = rowData.valueA + rowData.valueB;
                     return $.fn.dataTable.render.number(',', '.', 0, '$', sum);
                 }
              }
            
]

Scott

Replies

  • allanallan Posts: 61,771Questions: 1Answers: 10,112 Site admin

    Hi Scott,

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

    is what you want.

    The $.fn.dataTable.render.number() call returns an object which DataTables can then use for the orthogonal data calls. The display property of that object is the function that will do the display aspect. Source code for it is here.

    Allan

  • hondaman900hondaman900 Posts: 7Questions: 2Answers: 0

    Hi Allen,

    I'm using this to render currency column in a datatable (nightly build) as follows

    {data: 'real_current_value', name: 'real_current_value', render: $.fn.dataTable.render.number( ',', '.', 2, '$' )}

    But whenever there is a value of zero in the database it renders a $NaN.N. I was originally using version 1.10.9 but changed to the nightly build as I saw this as a potential solution in another post here, but it made no difference (other than alt-row highlighting).

    I have to show correctly-formatted currency, but can't show the NaN for zeros. Without the render option, numbers, including zeros, display fine (just plain).

    It would seem that the rendering function is the issue. How can I fix or workaround this?

    Thanks in advance.

  • colincolin Posts: 15,154Questions: 1Answers: 2,587

    Hi @hondaman900 ,

    It looks like you posted this comment on two threads. I replied on the other,

    Cheers,

    Colin

This discussion has been closed.