put data money + quantity together

put data money + quantity together

bilusesbiluses Posts: 26Questions: 10Answers: 0

in have this:
column [
{ data: "incomes.revenue" , render: $.fn.dataTable.render.number( ',', '.', 2, '$' ) },
]
So in the cell i get i.ex: 1,234.98$
i have now a data:"incomes.money", it can be $ or €

what i need is something like:
column [
{ data: "incomes.revenue" , render: $.fn.dataTable.render.number( ',', '.', 2, 'incomes.money' ) },
]

could it be possible?

i end up with that, but i lose the thousand separator also de inline edit property i have.
{ data: null, render: function ( data, type, row ) {
return data.incomes.money + '' +data.incomes.revenue;
} },

any solutions please

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,987Questions: 87Answers: 421
    edited July 2017

    Not sure whether I understand what you want to achieve but I think something is wrong with your code. I guess you would need to replace

    return data.incomes.money + '' + data.incomes.revenue;
    

    with

    return row.incomes.money + '' + row.incomes.revenue;
    

    Please see: https://datatables.net/reference/option/columns.render

    If you are using PHP at the back end you could do the rendering there as well. I use Editor there as well. This is my getFormatter for amounts.

    function getFormatterAmount(&$val) {
        if ($val == '0') {
            return '';
        } else {
            if ($_SESSION['lang'] === 'de') {     
                return number_format($val, 2, ',', '.');
            } else {
                return number_format($val, 2);
            }
        }
    }
    

    called here:

    Field::inst( 'fixed.amount' )
                    ->validator( function ( $val, $data, $opts ) {
                        return validatorAmount($data['fixed'], $val);
                    } )
                    ->getFormatter( function($val, $data, $opts) {
                        return getFormatterAmount($val);
                    })
                    ->setFormatter( function($val, $data, $opts) {
                        return setFormatterAmount($val, 'pcAmount', $data['fixed']['element']);
                    }),  
    
  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14
    edited July 2017

    Hi biluses, my money formatting requirements are not as complex as yours, but most of my applications deal with currency. I don't know if this will help you, but I found this tiny library extremely useful for formatting money, easy to use, and well documented.

    http://openexchangerates.github.io/accounting.js/

  • allanallan Posts: 63,482Questions: 1Answers: 10,467 Site admin
    Answer ✓

    Yes, you can do what you are looking for on the client-side:

    var euroRenderer = $.fn.dataTable.render.number( ',', '.', 2, '€' ).display;
    var dollarRenderer = $.fn.dataTable.render.number( ',', '.', 2, '$' ).display;
    

    then use columns.render as a function:

    render: function ( data, type, row ) {
      if ( row.incomes.money === '€' ) {
        return euroRenderer( data );
      }
      return dollarRenderer( data );
    }
    

    This basically makes use of the fact that we can save functions into variables in Javascript. In this case we are saving the display rendering function from the number formatter (very functional... :smile:).

    Allan

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    edited July 2017

    So, incomes.money contains your currency symbol and incomes.revenue the value?
    Try something like this:

    {
        data: "incomes.revenue" , render: function(data, type, row) {
            if(type !== 'display') {
                return data;
            }
    
            return $.fn.dataTable.render.number( ',', '.', 2, row.money)(data, type, row);
        }
    }
    

    Basically, $.fn.dataTable.render.number(...) returns a function (in a closure) that takes (data, type, row) parameters that then gets executed immediately with your row (that now has access to the rest of the row data because we implement a render function (the outer function). It's a bit dirty since it calls $.fn.dataTable.render.number(...) EVERY time it's called instead of once at initialisation time

  • rf1234rf1234 Posts: 2,987Questions: 87Answers: 421

    @allan: Your solution is something you should put into the docs! I wasn't smart enough to figure this out. That's why I am doing all of this stuff with PHP at the back end.

  • bilusesbiluses Posts: 26Questions: 10Answers: 0

    @allan thank you as allways that was great! working perfectly!

This discussion has been closed.