Row group with dynamic columns

Row group with dynamic columns

Khalid TeliKhalid Teli Posts: 251Questions: 71Answers: 0

Hi
I am using the rowGrop and dynamically create the sub row totals as shown in the example:

live.datatables.net/jixireyu/1/edit

1) The code used for generating the regroup totals and appending to td works perfect. However when I use the exaclt same code in my original file it doesn't work (I am using serverside )

 rowGroup: {
               startRender: function ( rows, group,level ) {
 var api = rows;

                var container = $('<tr/>');

                container.append('<td colspan= "2"> ' + group + '</td>');
                var j;

                    var intVal = function ( i ) {
             return typeof i === 'string' ?
                 i.replace(/[\%£,]/g, '')*1 :
                 typeof i === 'number' ?
                     i : 0;
         };
                for (j = 2; j < api.columns().header().length; j++) {
                     var hourSum = rows
                     .data()
                      .pluck(j)
                    .reduce( function (a, b) {
                    return intVal(a) + intVal(b);}, 0 );
                                               addClass = 'colorCheck';

                     container.append('<td>' + hourSum + '</td>').addClass(addClass);

                }
                return $(container)
            },
            dataSrc: [2]
        },

2) After this step, I was trying to export the row group and sub totals created. If you click on the link, The export button is able to export the rowGroup data and also subtotals generated . However, in my table the columns are generated dynamically and I would like to dynamically sum the columns and append to the row for exporting.

For example, in my case I am using the age column and then inserting the data at column_index = 1
How can I do it for all columns dynamically?

                        ageArray = [];

      ageArray.push(parseInt(age));
            var sum_ages = ageArray.reduce( function (a, b) {
                    return a + b ;
                 }, 0 );

 for (var $column_index = 1; $column_index < iColspan; $column_index++) {


                    table_data[$column_index] = sum_ages ;


            }

Thank you

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    However when I use the exaclt same code in my original file it doesn't work (I am using serverside )

    What doesn't work?

    Maybe you can use one of the server side examples here to build a server side processing test case to show the issue.

    However, in my table the columns are generated dynamically and I would like to dynamically sum the columns and append to the row for exporting.

    I would look at refactoring and moving the code in lines 1-6, of your second code snippet, inside the loop on line 8. inside the loop get the column data and sum it then save it in the column on line 11.

    Kevin

  • Khalid TeliKhalid Teli Posts: 251Questions: 71Answers: 0

    @kthorngren Thank you

    What doesn't work?

    Maybe you can use one of the server side examples here to build a server side processing test case to show the issue.

    I have created a test case and here is the link live.datatables.net/datedave/1/edit

    In the code below, if you changepluck(j) with the actual column name eg pluck(extn); this works fine and it shows correct totals for the rowGrop

               var hourSum = rows
                                 .data()
                                 .pluck('extn')
                                 //.pluck(j)
    
                        .reduce( function (a, b) {
                        return intVal(a) + intVal(b);}, 0 );
    

    2)

    I would look at refactoring and moving the code in lines 1-6, of your second code snippet, inside the loop on line 8. inside the loop get the column data and sum it then save it in the column on line 11.
    live.datatables.net/pumoxedi/1/edit

    I tried different ways and changed the code as you said, it works fine an calculates the column sum. But it does it for the whole column not for the row group .
    So the total displayed is total for whole column not for the rowGroup data :(

                    ageArray.push(parseInt(age));
    
                 for (var $column_index = 1; $column_index < iColspan; $column_index++) { 
          var sum = table
         .column( $column_index )
                .data()
                .reduce( function (a,b) {
                     return intVal(a) + intVal(b);}, 0 );
    
            console.log(sum);
             table_data[$column_index] = sum ;
                 }
    

    Thank you

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    In the code below, if you change pluck(j) with the actual column name eg pluck(extn)

    You are using object data with columns.data so you need to access the data using the property name, ie, extn. Using .pluck(j) is using an integer which is an index into array data and won't work with objects. You can use column().dataSrc() to get the data property for that column. Updated example:
    http://live.datatables.net/datedave/3/edit

    So the total displayed is total for whole column not for the rowGroup data

    Looks like the calculation for the age column works in your original test case. I guess you should follow that pattern for all the columns you want to sum. Instead of creating individual variables for each column create an object where the key is the column index. Then, in the loop above, sum each column data and place in the table.

    Kevin

Sign In or Register to comment.