Calculation in startRender irrespective of pagination (while using the rowGroup)

Calculation in startRender irrespective of pagination (while using the rowGroup)

Sangeetha_NithyaSangeetha_Nithya Posts: 2Questions: 1Answers: 0
edited July 2019 in Free community support

Working on data tables and everything seems great. I am using the rowGroup and it works great and showing me the grouping properly. Also using startRender to find and show group vice calculations.

My problem is that pagination affects my calculations; if display length is set to 10, and a group have 17 rows, in start render calculation it gives the calculation result of first 10 rows in page one and calculation remaining 7 in next page.

I want to make the calculations irrespective of page, it is needed the calculation done in the all 17 rows under that group
Code I used is given below ..
Any help appreciated!
Thanks In advance ..

var data_table = $('#myTable').DataTable( {

 order: [[0, 'asc'], [1, 'asc']],
 "displayLength": 25,
 rowGroup:{
  startRender: function ( rows, group ) {

   var clickTotal = rows
   .data()
   .pluck(3)
   .reduce( function (a, b) {
    return parseInt(a) + parseInt(b);
  });

            var ageCTR = rows
            .data()
            .pluck(5)
            .reduce( function (a, b) {
             return parseInt(a) + parseInt(b);
               });

            return $('<tr>')
            .append( '<td>'+group+'</td>' )
            .append( '<td>'+clickTotal+'</td>' )                    
            .append( '<td>'+ageCTR +'</td></tr>' );

          }, dataSrc: [0, 1]
        },
        columnDefs: [ {
          targets: [ 0, 1,5 ],
          visible: false
        },
        { orderable: false, targets: [2,3,4] } ],
      } );

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @Sangeetha_Nithya ,

    This example here should help - it's showing the sums for the Age column for both the group as a whole and what's shown on the current page.

    Cheers,

    Colin

  • Sangeetha_NithyaSangeetha_Nithya Posts: 2Questions: 1Answers: 0

    Thanks a lot @colin :smile:
    Just as the mentioned example, Using filter() is the key.
    My updated sum function is given below, Hope some one will find it helpful.

    [Because I used multilevel grouping based on column 0 & column 1,both of those columns are compared to the group value in the filter function.]

     var clickTotal = $('#myTable').DataTable()
              .rows()
              .data()
              .filter( function ( data, index ) {
    
                return (data[0] == group || data[1] == group) ? true : false;
              } )
              .pluck(5)
              .sum();
    
  • middeuggermiddeugger Posts: 7Questions: 1Answers: 0

    @colin @Sangeetha_Nithya you guys solution is working. But if there is multiple item then how I get the total of subgroup of each row group in pagination ?

    First Page:

    Country A - 100
    City - 50
    city 1 - 25
    city 2 - 25
    Town - 50
    town 1 - 25
    town 2 - 25

    Country B - 50
    City - 50
    city 1 - 25
    city 2 - 25

    Second Page

    Country B - 150
    City - 100
    city 1 - 25
    city 2 - 25
    city 3 - 25
    city 4 - 25
    Town - 50
    town 1 - 25
    town 2 - 25

    In the above example the total is showing correct as per our example. But in my case I should show the total of Country B is 200 (from first and second page) and City value should be 150.

    So how I can find a solution for this ?

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    @middeugger the best way to find a solution is to provide a test case showing an example of what you have so we can understand your code and provide suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • middeuggermiddeugger Posts: 7Questions: 1Answers: 0

    Do you have any example of multiple rowgroup total in pagination ?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I'm not clear what you mean by "in pagination"? Can you expand on that, please. And, as Kevin asked, please can you post a test case of what you've got so far,

    Colin

This discussion has been closed.