Sum of values in a table column with different currency symbol(DataTables - laravel)

Sum of values in a table column with different currency symbol(DataTables - laravel)

jdiaz11jdiaz11 Posts: 5Questions: 4Answers: 0

Cordial greetings I have a problem because I have a project where in a table I have numerical values that I want to make the total sum of that column but the problem is that in that column there are two types of monetary values which are dollars and soles (local currency of my country).

What I want is to separate those values to only add the totals depending on the currency symbol in this case only the total of the amounts in soles and the total of the amounts in dollars of that column.

I use datatables and so far I have used a code to do the total sum but it adds it in general and I don't know how to divide them.

<script> $(document).ready(function(){ var table = $('#servicios').DataTable({ processing: true, serverSider: true, ordering: true, stateSave: true, "order": [[ 2, "asc" ]], }); //SUM var arrSalePriceOne = table.column(4).data().sum(); $("#tblProfit").text((+arrSalePriceOne)); </script>
my query is better specified in this link
https://stackoverflow.com/questions/73396143/sum-of-values-in-a-table-column-with-different-currency-symboldatatables-lara

Answers

  • kthorngrenkthorngren Posts: 16,773Questions: 25Answers: 3,971

    You will need to create your own loop with two sum variables. In the loop determine the currency type to know which sum variable to add the value to. This pseudo code shows one option:

    var dollars = 0;
    var soles
    table.column(4)
        .data()
        .each(function (d, j) {
            if (d is dollar) {
              dollar += d;
           else {
             soles += d;
           }
        });
    

    You will need to work out the if statement. Maybe check if the string has a $ will work.

    Kevin

  • jdiaz11jdiaz11 Posts: 5Questions: 4Answers: 0

    I did not understand very well because I am new in this world but I appreciate your help.

Sign In or Register to comment.