You are viewing limited content. For full access, please sign in.

Question

Question

sum 5 columns

asked on October 7, 2015

I need to get sub totals on 5 columns in a table. I have looked at the examples but how do I get each column to have a separate total at its bottom (not on the row), then I also need to total the subtotals of columns 1-3, then Total all of them for a grand total. I an new to javascript and css but trying to learn.

0 0

Answer

SELECTED ANSWER
replied on October 7, 2015

Here's an example:

Form fields and CSS class names

The form uses one table with five currency columns. The CSS class name for each column is col1, col2, etc. This table allows the user to add additional rows. There is a second table with five currency columns used to store the subtotals of each of the five columns from the first table. This second table only allows for one row. The columns in this second table use the CSS class names, subtotalcol1, subtotalcol2, etc. Below that is a single currency field that stores the total of the first three columns. It has a CSS class name, col123total. Finally, there's one last currency column that stores the grand totay. It uses the CSS class name, grandtotal.

The Javascript used is

$(document).ready(function () {
  
  $('.cf-table-block').on('blur', 'input', sumtotal); // run the sumtotal function whenever a table input loses focus
  $(document).on('click', '.form-del-field', sumtotal); // run the sumtotal function whenever a row in the table is deleted
  $('.readonly input').attr('readonly', true); // make subtotal and total fields read only

  function sumtotal() {
    var sum1 = 0;  // variables to store the subtotal of each column
    var sum2 = 0;
    var sum3 = 0;
    var sum4 = 0;
    var sum5 = 0;
    $('.cf-table-block tbody tr').each(function () { //for each row in the table
      var s1 = 0;  // variables to store the individual column value in the current row
      var s2 = 0;
      var s3 = 0;
      var s4 = 0;
      var s5 = 0;
      s1 = parseNumber($(this).find('.col1 input').val()); // getting the value to store in the variable
      s2 = parseNumber($(this).find('.col2 input').val());
      s3 = parseNumber($(this).find('.col3 input').val());
      s4 = parseNumber($(this).find('.col4 input').val());
      s5 = parseNumber($(this).find('.col5 input').val());
      sum1 += s1;  // adding the current row value of a column to the subtotal variable for that column
      sum2 += s2;
      sum3 += s3;
      sum4 += s4;
      sum5 += s5;
    });
    $('.subtotalcol1 input').val(sum1);  // setting the subtotal value into the subtotal field
    $('.subtotalcol2 input').val(sum2);
    $('.subtotalcol3 input').val(sum3);
    $('.subtotalcol4 input').val(sum4);
    $('.subtotalcol5 input').val(sum5);
    $('.col123total input').val(sum1 + sum2 + sum3);
    $('.grandtotal input').val(sum1 + sum2 + sum3 + sum4 + sum5);
  }
  
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
  
});

As an option, you may want to give the various subtotal and total fields a second CSS class name, i.e. readonly. This way you can add another line into the JS to make those read-only so the subtotal and total calculations will strictly be based off of the values entered into the first table.

Here's what a submitted form looks like

1 0

Replies

replied on October 7, 2015
The example code below will do a column total calculation. This should do it for the first column. What you would need to do is add other variables for the other columns as well as matching classes when you are doing the calculations and inserting the totals into the fields.

​$('.cf-table-block').on('blur', 'input', tableTotal);
    function tableTotal() {
        var FirstColumn = 0;
        var FirstColumnTotal = 0;
     
        $('.cf-table-block tbody tr').each(function () {
            FirstColumn =  parseNumber($(this).find('.FirstColumn input').val()); 
                       
            FirstColumnTotal += FirstColumn;
    	    $(this).find('.FirstColumnTotal input').val(total);
            
        });//need to total the total of the columns here​
}

You would then need to total

1 0
replied on October 8, 2015

Wow, Thank you both! I will give these a try and see how it goes! 

0 0
replied on October 8, 2015

Thank you very very much. Worked like a charm and I was even able to tweak it a little. Kristina

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.