Sum of columns

Sum of columns

CaptainNemoCaptainNemo Posts: 13Questions: 6Answers: 1
edited October 2015 in Free community support

Hi every all,

I made a fiddle to explain the case.

http://live.datatables.net/nudusuto/1/edit?html,js,output

Actually, i made the sum of columns (not all... No goal to make a sum of text :p ) : Salary June / Salary July.

This is OK.

I want to make the sum of salary, for each person. And i want to set the result in the last cell on each row.

And this is not OK :p .

If you can help me...

Thanks !

This question has accepted answers - jump to:

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    http://live.datatables.net/nudusuto/2/edit

    $(document).ready( function () {
        var table = $('#example').DataTable({
          "columnDefs": [
            { targets : [ 5 ], data:  function ( row, type, val, meta ) {
                return parseInt(row[4],10) + parseInt(row[3],10);
                }
             }  
          ]    
        });
    });
    
    
  • CaptainNemoCaptainNemo Posts: 13Questions: 6Answers: 1
    edited October 2015

    Thanks, it's a first step.

    In my case, number of columns could be different. In fact, the table is the result of a query, i.e : Display all months of salary, from 2015-02 to 2015-06, or 2013-01 to 2015-01, or etc.

    So i can't write in my javascript file specific columns indexes, range month could be different.

    And, contrary to column sum, if i use an instruction like "table.cell(3,2).data(5000).draw(), sum column are automatically recalculated, but not sum row with your script proposal ;)

    http://live.datatables.net/nudusuto/4/edit?html,js,output

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    edited October 2015

    Don't be changing the requirements mid-project :)

    If you can put a class in the column headers of the then you should be able to use the createdRow callback to get the values of the selected cells.

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    Here is how you can do it if each TD has a class on it.

    $(document).ready( function () {
        var table = $('#example').DataTable({
          
        "createdRow": function( row, data, dataIndex ) {
          $('td:eq(3)', row).addClass("sumMe");
          $('td:eq(4)', row).addClass("sumMe");
              var mySum = 0;
              $('td.sumMe', row).each(function() {
                mySum += parseInt( $(this).html(), 10);     
              });
             $('td:eq(5)', row).text(mySum);     
        }      
        });
    });
    
    

    That could be tightened up, but it would be harder to read.

    Figuring out how to add the class is left as an exercise for the student. I'm adding the class manually to show that it works.

  • CaptainNemoCaptainNemo Posts: 13Questions: 6Answers: 1

    Re :D

    A last thing.

    In this fiddle : http://live.datatables.net/nudusuto/6/edit?html,js

    sum of rows is OK, sum of column is OK.
    Except for the ultimate sum, the sum of all sum :)
    In the cell, top-right.

    If I click on my button test "Click to change value", the cell (3,3) was modified. The sum of concerned row was recalculated, and column sum too..

    But the ultimatum sum stay to 0.

    Any idea ?

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin

    The last column is summing the data that DataTables knows about (you are using column().data() after all) - that data is all zeros for that last column - hence the issue.

    You need to tell DataTables about the data value change rather than just writing it into the DOM (since DataTables can't know that you've done that). I would suggest calculating the value of the last column in the table using columns.render as a function.

    Allan

  • CaptainNemoCaptainNemo Posts: 13Questions: 6Answers: 1
    edited October 2015

    Thanks Allan.
    If I understand, i define the columns.render option in the Datatables Initialisation, specifying the column concerned. Target -1 in my case.

    But no result... I f you can help me...

    http://live.datatables.net/nudusuto/7/edit?js,output

  • allanallan Posts: 61,822Questions: 1Answers: 10,127 Site admin
    Answer ✓

    http://live.datatables.net/nudusuto/8/edit

    The function used for the renderer was returning 0 for whatever reason. I've simplified it a little and it works now.

    I've also change the sum loop to use cells().render() to get the rendered value.

    Allan

This discussion has been closed.