Page 1 of 1

Need some help with a table-dv.js file

Posted: 2019-03-23 19:13
by kbarrett
Hello to all the coding experts out there,

I am still working on the same project but have hit another conundrum. I have a detail view with some simple equations that I need to group together for one final total. I have a MyTable-dv.js file that I have been working on. There are some catches though.

1. The first part is fairly easy and with the current help files posted on the AppGini site I was able to solve. It is basic math with 3 pulldown fields needing to be summed in a subtotal field. Here is the code I have for that:
$j(function(){
/* Set SubTotal1 field as read-only to avoid user editing */
$j('# SubTotal1').prop('readonly', true);

/* recalculate SubTotal1on updating Field1, Field2 or Field3*/
$j('#Frequency, #Severity, #Exposure').change(function(){
var Frequency = parseFloat($j('#Field1).val()) || 0;
var Severity = parseFloat($j('#Field2').val()) || 0;
var Exposure = parseFloat($j('#Field3').val()) || 0;

var SubTotal1 = Math.round (Field1 + Field2 + Field3) ;
$j('#SubTotal1').val(SubTotal1);
});

/* Calculate Subtotal on opening the detail view form */
$j('#Field1').change();
$j('#Field2').change();
$j('#Field3').change();


})

2. I have 3 additional fields that are multiple choice list boxes. I have been trying to have it so when they are not populated, they would be zero and the moment that each is populated (regardless of how many choices are selected) each would be given a distinct value. The once summed they would become SubTotal2 . The fields are:
• ControlField1 – Would like to see this as a total of 3 when populated and zero if empty (null)
• ControlField2 – Similar to above but 2 if populated and zero otherwise
• ControlField3 – Finally, 1 if populated and zero if empty

So the equation would be SubTotal2 = ControlField1(value of 3) + ControlField2(value of 2) + ControlField3(value of 1). I have nothing to show here as so far I have been unsuccessful.

3. Finally I would like to Have a final total that would be GrandTotal = Subtotal1 – SubTotal2
I tried this in the same MyTable-dv.js file but it hasn’t worked:
/* Set GrandTotal field as read-only to avoid user editing */
$j('#GrandTotal').prop('readonly', true);

/* recalculate GrandTotal on updating SubTotal1 and SubTotal2*/
$j('#SubTotal2').change(function(){
var SubTotal = parseFloat($j('#SubTotal2').val()) || 0;


var Adjustment = Math.round (SubTotal1 – SubTotal2) ;
$j('#GrandTotal').val(GrandTotal);
});
/* Calculate SubTotal2 on opening the detail view form */
$j('#SubTotal2').change();

I wanted to see if I could just put a number in the SubTotal2 field and make the equation work. All I achieved was a NaN total in the GrandTotal field.

Thanks in advance.

If I need to pay someone for the assistance, I am willing to work out something. However, it all depends on how much. I am not working at the moment and building this on sweat equity.

Re: Need some help with a table-dv.js file

Posted: 2019-03-23 19:26
by kbarrett
I tried to edit but was unsuccessful. The first part should read:
$j(function(){
/* Set SubTotal1 field as read-only to avoid user editing */
$j('# SubTotal1').prop('readonly', true);

/* recalculate SubTotal1on updating Field1, Field2 or Field3*/
$j('#Field1, #Field2, #Field3').change(function(){
var Field1= parseFloat($j('#Field1).val()) || 0;
var Field2 = parseFloat($j('#Field2').val()) || 0;
var Field3 = parseFloat($j('#Field3').val()) || 0;

var SubTotal1 = Math.round (Field1 + Field2 + Field3) ;
$j('#SubTotal1').val(SubTotal1);
});

/* Calculate Subtotal on opening the detail view form */
$j('#Field1').change();
$j('#Field2').change();
$j('#Field3').change();


})

Sorry for the confusion

Re: Need some help with a table-dv.js file

Posted: 2019-03-24 11:59
by pbottcher
Hi,

maybe you can it like this:

Code: Select all

$j(function () {
	$j('#subtotal_1').prop('readonly', true);
	$j('#subtotal_2').prop('readonly', true);
	$j('#grandtotal').prop('readonly', true);

	var calc_grandtotal = function () {
		var SubTotal1 = parseFloat($j('#subtotal_1').val()) || 0;
		var SubTotal2 = parseFloat($j('#subtotal_2').val()) || 0;
		$j('#grandtotal').val(SubTotal1+SubTotal2)
	
	}
/* recalculate SubTotal1on updating Field1, Field2 or Field3*/
	$j('#field_1, #field_2, #field_3').on('change',function(){
		var Field1= parseFloat($j('#field_1').val()) || 0;
		var Field2 = parseFloat($j('#field_2').val()) || 0;
		var Field3 = parseFloat($j('#field_3').val()) || 0;

		var SubTotal1 = Math.round (Field1 + Field2 + Field3) ;
		$j('#subtotal_1').val(SubTotal1);
		calc_grandtotal();
	});

	$j('#multi_1, #multi_2, #multi_3').on('change',function(){
		var Multi1= $j('#multi_1').val() || 0;
		var Multi2 = $j('#multi_2').val() || 0;
		var Multi3 = $j('#multi_3').val() || 0;

		if (Multi1 != 0) Multi1=3;
		if (Multi2 != 0) Multi2=2;
		if (Multi3 != 0) Multi3=1;
		var SubTotal2 = Math.round (Multi1 + Multi2 + Multi3) ;
		$j('#subtotal_2').val(SubTotal2);
		calc_grandtotal();
	});

});
Replace the #field_1, #field_2, #field_3, #multi_1, #multi_2, #multi_3, #subtotal_1, #subtotal_2, #grandtotal with your fieldnames.

Re: Need some help with a table-dv.js file

Posted: 2019-03-25 18:55
by kbarrett
Hey!

You are the best! Worked like a charm. I was frustrated at first as it appeared it did not work. On a hunch I launched another browser (was using Chrome) and tried, worked! I had to go back and clear all of my viewing cache for the previous hour but it worked. I have this appearing in the SubTotal1 field on the Detail View prior to plugging in the numbers and values but not too worried if I have to live with it:

[object HTMLInputElement]

The next step is my evaluation field. I am going to try to get it so that depending on the totals in the grandtotal field it will read Low, Medium or High based on the values. Something like
Value is < 5 > 0 = Low
Value is < 9 > 5 = Medium
Value is < 13 > 9 = High

Thanks again for all your help.

Re: Need some help with a table-dv.js file

Posted: 2019-03-25 19:29
by pbottcher
Hi,

not sure what you mean by
------------
I have this appearing in the SubTotal1 field on the Detail View prior to plugging in the numbers and values but not too worried if I have to live with it:

[object HTMLInputElement]
--------------

For the second part you could change the calc_grandtotal function to:

Code: Select all

	var calc_grandtotal = function () {
		var SubTotal1 = parseFloat($j('#subtotal_1').val()) || 0;
		var SubTotal2 = parseFloat($j('#subtotal_2').val()) || 0;
		var GrandTotal = SubTotal1+SubTotal2;
		$j('#grandtotal').val(GrandTotal);
		var Value1 = "";   // or set a default value 
		if (GrandTotal < 13 && GrandTotal >= 9) { Value1 = "High"  }
		else if  (GrandTotal < 9 && GrandTotal > 5) { Value1 = "Medium"  }
		else if  (GrandTotal < 5 && GrandTotal > 0) { Value1 = "Low"  }
		else { Value1 = ""  }
		$j('#FIELDNAME_FOR_VALUE').val(Value1);
	}

Re: Need some help with a table-dv.js file

Posted: 2019-03-25 20:03
by kbarrett
I am attaching a screen grab of the SubTotal1 field and the value that is in it prior to inputting any values in the other fields. I hope this better explains what I was trying to say.

Cheers,

Kevin