Do a sum operation inside render

Do a sum operation inside render

EPettersonEPetterson Posts: 9Questions: 3Answers: 0

Hello! First of all thank you for this excellent app.
I'm trying to do a sum inside a render just like the title said, where I can send 2 variables from a query and return a result, might be very simple but I'm not sure how I'm supposed to do that.

This is the code of table with render I'm working on right now

       var table = $('#table').dataTable({          
          "bPaginate": false,
          "bLengthChange": false,
          "bFilter": true,
          "bSort": true,
          "bInfo": false,
          "bAutoWidth": false,
          "stateSave": true,
          "order": [[ 0, "asc" ]],
          dom: 'Bfrtip',
          buttons: [
              'excel'
          ]
          
           "columnDefs": [ {
    "targets": 1,
    "data": 1, // Use the full data source object for the renderer's source
    "render": function ( data, type, row, meta ) {
      //ok = row[2];
      //console.log(ok);
      //precio = parseFloat(300000.15 + 33);
      //return typeof i === 'string' ? i.replace(/[\,]/g, '') * 1 : typeof i === 'number' ? i : precio;
      //return 0;
      //ok = table.rows().count();
      //
      return '0';
      //var some_number = parseFloat(row[2]);
      //var saldo = some_number;
      //return  some_number;
    }
  }]

And this is what something like I'm trying to implement here:

$i = 0;
    if ($i == 0){
    $saldo1 = $saldo + $amount;
      }else{
    $saldo1 = $saldo1 + $amount;
    $saldototal +=  $saldo1 + $amount;
      }

    $i++;

If anyone can help me I'd appreciate it, thank you!

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Your render function needs to return a value.
    I presume you want this:

        if ($i == 0){
        $saldo1 = $saldo + $amount;
    return $saldo1;      
    }else{
        $saldo1 = $saldo1 + $amount;
        $saldototal +=  $saldo1 + $amount;
    return $saldototal;
          }
    
  • EPettersonEPetterson Posts: 9Questions: 3Answers: 0

    Hello tangerine, thanks for the quick reply, I now understand better what I have to do, one more question, how do I pass a variable inside datatable code which is from a query? In this case it would be $saldo and $amount. Thanks in advance!

  • EPettersonEPetterson Posts: 9Questions: 3Answers: 0

    Nevermind, I figured, thank you!

  • EPettersonEPetterson Posts: 9Questions: 3Answers: 0

    Hello, sorry for bringing the subject again but I can't find the way to bring all the data from a variable outside of the datatable code, in this case $amount delivers only the last amount from a while. Is it possible to do a query inside the render?

  • kthorngrenkthorngren Posts: 21,328Questions: 26Answers: 4,949
    edited February 2023

    Is it possible to do a query inside the render?

    Its possible but not recommended as it would slow the page rendering as the query would run for each row.

    I can't find the way to bring all the data from a variable outside of the datatable code,

    Do you mean its a Javascript variable? You just need to make sure the variable is in a scope available to the scope the Datatables init code is in.

    Can you post a link to your page or a test case showing what you are doing so we can understand your solution to provide more specific suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • EPettersonEPetterson Posts: 9Questions: 3Answers: 0

    Ok, I found the source of the problem, as of now the table generated shows the data like this:

    That -118.000 from the import column subtracts with the 7445.41 from the balance, returning the balance above of -111.536.
    It shouldn't be doing that, but it is because the table is made with two separate queries, one that takes bank transactions and one for the bills.

    So when the first query finish the loop, it saves the last import as "saldo", and the second query starts by substracting this saldo saved by the last import from query 1.
    This is the operation I use mentioned above:

    I'd need to know if there is a solution for this, because if it isn't I would just try to make both queries into only one.
    Thanks for the support as always!

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    I would recommend using the render function for calculations in a single row only. Never across multiple rows. You get tied to the data structure that way (e.g. imagine you had to add an extra row at the top of the table for whatever reason, then row[2] would be wrong.

    I would strongly recommend you populate the table with data that has already been calculated across the row boundaries, if that is what you need. I think it will be far more efficient to do that in whatever your data source is (looks like a plain HTML table?).

    Allan

Sign In or Register to comment.