Including row subtotals in custom print function.

Including row subtotals in custom print function.

bbrindzabbrindza Posts: 136Questions: 29Answers: 1

I have a bit of code I found in the forum that will add row subtotals to custom print option. It works fine for printing all rows in the table, however when rows are selected it will still print all subtotals in the table.

I believe my only option would be to remove the function that adds the subtotals in the table at time of printing and change the script to print subtotals on the fly in the customize: function(win) of my print button.

I have been looking for some examples out this out on the internet but could not find any. Has anyone have a working example of this?


  • bbrindzabbrindza Posts: 136Questions: 29Answers: 1

    Here is the custom print function and the includeSubtotals function.

    Custom Print

        extend: 'print', 
        init: function(api, node, config) {
                        $(node).removeClass('dt-button buttons-print')
        className: 'btn btn-primary',
        exportOptions: { columns: [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 19, 20]  },
            title: 'Week Ending Report '+  $('#weekEndingDate').val() + " for Manager: " + managerName + '  (printed by ' + employeeNameFirstLast + ')' ,
        customizeData: includeSubtotals,
        customize: function(win) {
                    $(win.document.body).find('h1').css('font-size', '15pt');   
                    $(win.document.body).css( 'font-size', '8pt' );
                    $(win.document.body).find( 'table' )
                                .addClass( 'compact' )
                                .css( 'font-size', 'inherit' );
                        var last = null;
                        var current = null;
                        var bod = [];
                       var css = '@page { size: landscape; font-size: 8px}',
                        head = win.document.head || 
                         style = win.document.createElement('style');
                        style.type = 'text/css';
               = 'print';
                        if (style.styleSheet)
                          style.styleSheet.cssText = css;

    includeSubtotals function

    function includeSubtotals( data, button, exportObject){
      var classList = button.className.split(' ');
      var subtotals = [];
      $('#timeLogTable_WeekEnding tr.subTotalHours').each(function(){
        var $row = $(this);
        row_index = $row.index(); 
        row = $row.find('td,, th').map(function(){return $(this).text();}).get();
                subtotals[ subtotals.length ] = {rowNum: row_index, data: row };
      var firstGroup = 'Y';
      var currentRow = 0;
      var diff = 0;
      for (var i=0, n=subtotals.length; i<n; i++){
           var subtotal = subtotals[i];
           if(firstGroup == 'Y'){  
                subtotal.rowNum = subtotal.rowNum ;
                currentRow =  subtotal.rowNum - 2;
                diff = 3;
               subtotal.rowNum = subtotal.rowNum ;
               currentRow =  subtotal.rowNum - diff;
              diff = diff + 1;
           data.body.splice(currentRow , 0,;
           firstGroup = 'N';  
    return data;
  • colincolin Posts: 9,856Questions: 0Answers: 1,635
    edited September 3

    $('#timeLogTable_WeekEnding tr.subTotalHours').each(function(){

    It's probably due to this - as this will get all the rows in the table. You could also check for the class selected, or just use the DataTables API - rows(), with selector-modifier set to selected: true,


  • bbrindzabbrindza Posts: 136Questions: 29Answers: 1

    Hi Colin,

    Thank you for answering.

    Prior to my post I was fiddling around the edges with that idea, however I found that if a user selects only one row in the group, the subtotal for that group is still brought in from the table with the totals of all rows for the group.(see images below).

    So I was considering still using the includeSubtotals function but creating the totaling based on row selection on the fly and not using the subtotals from the table.

    Or do I force selection of all rows under a group when one is select in that group and only use the subtotals for that group from the table?

    I am a little unclear on how the customizeData: option works and if what I am suggesting can be achieve in this manner.

    I would like to hear your thoughts and or suggestions.

  • colincolin Posts: 9,856Questions: 0Answers: 1,635

    There's a lot going on there - I think it would be clearer if we could see it running. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.



  • bbrindzabbrindza Posts: 136Questions: 29Answers: 1


    This application is behind our corporate firewall so public access is not possible.

    Moreover my application uses ssp that returns JSON from our back end DB2 tables.

    I can not see how I can copy and use this JSON data in the .

    I can remove any script that is not relevant to my issues , however I am stumped on the data piece, which is need for the example.

    Please advise.

  • allanallan Posts: 52,724Questions: 1Answers: 8,070 Site admin

    I'd use the approach Colin suggested with the API - that is what it is there for after all. You'd need to check if there are any rows selected first, and if so, then limit to just the selected rows, otherwise get all.

    Then your grouping code would need to be updated. It looks like the algorithm used for includeSubtotals needs to be updated to handle the empty rows as well.


  • bbrindzabbrindza Posts: 136Questions: 29Answers: 1


    That was my initial thought. I will still use the includeSubtotals function in my customizeData: option but instead of using the existing subtotals from the table I will use the API to check for select row and create the subtotals on the fly.

    My thought would be to replace the $('#timeLogTable_WeekEnding tr.subTotalHours').each(function() with a ,.each function for selected rows in a group.

Sign In or Register to comment.