I think I figured it out, but that is with making an assumption.
So basically, it appears as though the subtotals for the type are actually 3 additional columns in the row for the table?
In that case, do the value definition at the end.
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
function sumtotal() {
var total1 = 0;
var total2 = 0;
var total3 = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
var timeS = 0;
var timeE = 0;
$(this).find('.timeSTART input').each(function () {
timeS = parseNumber($(this).val());
});
$(this).find('.timeEND input').each(function () {
timeE = parseNumber($(this).val());
});
s = timeE - timeS;
$(this).find('.subtotal input').val(s);
if ($(this).find('.timeTYPE option:selected').val() === "Type1"){
total1 += s;
}
else if ($(this).find('.timeTYPE option:selected').val() === "Type2"){
total2 += s;
}
else if ($(this).find('.timeTYPE option:selected').val() === "Type3"){
total3 += s;
}
$('.totalTYPE1 input').val(total1);
$('.totalTYPE2 input').val(total2);
$('.totalTYPE3 input').val(total3);
});
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});
This way, the if else statements just adjust the totals based on the changes to that row, but the row will always have the values reset for all the types since we may have had someone change the type.
BTW: If you want to actually have the rows add up as it goes along, instead of all having the same value, try this:
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
function sumtotal() {
var total1 = 0;
var total2 = 0;
var total3 = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
var timeS = 0;
var timeE = 0;
$(this).find('.timeSTART input').each(function () {
timeS = parseNumber($(this).val());
});
$(this).find('.timeEND input').each(function () {
timeE = parseNumber($(this).val());
});
s = timeE - timeS;
$(this).find('.subtotal input').val(s);
if ($(this).find('.timeTYPE option:selected').val() === "Type1"){
total1 += s;
}
else if ($(this).find('.timeTYPE option:selected').val() === "Type2"){
total2 += s;
}
else if ($(this).find('.timeTYPE option:selected').val() === "Type3"){
total3 += s;
}
$(this).find('.totalTYPE1 input').val(total1);
$(this).find('.totalTYPE2 input').val(total2);
$(this).find('.totalTYPE3 input').val(total3);
});
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});
Lastly, to speed things up slightly:
$(function () {
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
function sumtotal() {
var total1 = 0;
var total2 = 0;
var total3 = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
var timeS = 0;
var timeE = 0;
timeS = parseNumber($(this).find('.timeSTART input').val());
timeE = parseNumber($(this).find('.timeEND input').val());
s = timeE - timeS;
$(this).find('.subtotal input').val(s);
var testTYPE = $(this).find('.timeTYPE option:selected').val();
if (testTYPE === "Type1"){
total1 += s;
}
else if (testTYPE === "Type2"){
total2 += s;
}
else if (testTYPE === "Type3"){
total3 += s;
}
// Comment out the below three lines and uncomment the 3 lines following them to
// have the Type Totals appear as they originally did in the code you provided
$(this).find('.totalTYPE1 input').val(total1);
$(this).find('.totalTYPE2 input').val(total2);
$(this).find('.totalTYPE3 input').val(total3);
//$('.totalTYPE1 input').val(total1);
//$('.totalTYPE2 input').val(total2);
//$('.totalTYPE3 input').val(total3);
});
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});
});