Multiple currency collection separately

Multiple currency collection separately

markbfxmarkbfx Posts: 4Questions: 1Answers: 0

Hello all,

I have a transaction table, but there are different currencies in the same column, for example:

€ 100
$ 100
€ 100

I get the sum of all with footercallback, how can I parse them together and print their sums individually?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin

    Presumably you are doing a footer callback like in this example? There I just have it stripping everything that isn't numeric data and summing that. In this case you would need to have an if condition based on the currency. If it is only USD and EUR then you could just save the respective counts into two different variables. If you have other currencies you might want to use a map.

    Allan

  • markbfxmarkbfx Posts: 4Questions: 1Answers: 0
    edited October 2022

    It gives the sum of the amount I did as you said, can you show it with an example?

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416

    Just loop through the rows and use an acculumulator for each currency. You seem to have a specific problem though -TRY 1.00 should rather be TRY -1.00

    You'll need to handle that, too.

  • markbfxmarkbfx Posts: 4Questions: 1Answers: 0

    You can check code

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416

    I assume you are using footerCallback? If not I wouldn't know how this should work.

    I wouldn't use "reduce()" here but a simple loop with accumulators for the different currency amounts.

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416
    edited October 2022 Answer ✓

    Something like this could work:

    footerCallback: function (row, data, start, end, display) {                
        var api = this.api(); 
        // Remove the formatting to get float data for summation
        var floatVal = function (i) {
            return typeof i === 'string' ? i.replace(/[\,]/g, '') * 1 : typeof i === 'number' ? i : 0;
        };
        
        var sumDollars = 0;
        var sumLiras = 0;
        var sumEuros = 0;
        api.rows().every( function (rowIdx, tableLoop, rowLoop) { 
            var data = this.data();
            if ( data.YourCurrencyField === "$" ) {
                sumDollars += floatVal(data.YourAmountField);
            } else if ( data.YourCurrencyField === "€" ) {
                sumEuros += floatVal(data.YourAmountField);
            } else {
                sumLiras += floatVal(data.YourAmountField);
            }
        });
        
        //do something with sumLiras etc. and update the footer cells accordingly
    }
    

    Of course those currencies are hard coded in my example. You could create an array of the distinct currencies and do this all dynamically with a nested loop but that is nothing data tables specific...

  • markbfxmarkbfx Posts: 4Questions: 1Answers: 0
    edited October 2022
                var api = this.api(),data;
                var floatVal = function (i) {
                    return typeof i === 'string' ? i.replace(/[\,]/g, '') * 1 : typeof i === 'number' ? i : 0;
                };
                var sumDollars = 0;
                var sumLiras = 0;
                var sumEuros = 0;
                api.rows().every( function (rowIdx, tableLoop, rowLoop) {
                    if ( data[rowIdx].currency === "EUR" ) {
                        sumEuros += floatVal(data[rowIdx].amount);
                    } else if ( data[rowIdx].currency === "USD" ) {
                        sumDollars  += floatVal(data[rowIdx].amount);
                    } else {
                        sumLiras += floatVal(data[rowIdx].amount);
                    }
                });
    

    It's ok like that, thank you <3

Sign In or Register to comment.