Calculated field in form application

Hello,

I have a somewhat tricky question. Hopefully someone can help…
Please take a look at the attached screenshot.

There are (among others) two fields containing integers (“Punkte” and “Bonus”), and a third field (“Gesamt”). This third field shall dynamically show the sum of “Punkte” and “Bonus” (just for user information, not to be stored in the DB).

I tried to bind a JS to the input fields onChange event, but since I show the form application as an editable grid, there may be more than one row with the same three fields…
How can I do this, that the field “Gesamt” always show the sum of the two fields in the same row?

Thanks for your help!

sum.png

Re: Calculated field in form application

Hi,

Try to use custom field (“New Field” from fields), also you can only show information’s.

regards,

Re: Calculated field in form application

Thanks for your answer.

As you can see in my screenshot, I already created a new custom field.
That’s okay.

What I new help with, is the Javascript logic to sum up the two other fields.
I can bind an event handler to the two fields onChange event, but within this handler, I need to know: Which row is the one currently edited (as I have an editable grid, there are usually more than one rows shown at a time)?

Re: Calculated field in form application

Hi,

When you create on Javascritp Event like OnChange , what instruction’s you put there are running just for current changed field in Current Row.
Here is some example :

in OnChange Event put something like these :
{Gesamt} = {Punkte}+{Bonus}

And this is all.

Enjoy.

Re: Calculated field in form application

Thanks,

I tried that but all I get is

scCssBlur is not defined
(3554 out of range 2)

on the JS console…

I also tried a simple

alert({Punkte});

but no change…

Any hints?

Re: Calculated field in form application

Have you tried the following?:

var opunkte = document.F1.punkte.value;
var obonus = document.F1.bonus.value;

document.F1.gesamt.value = obonus + opunkte;

N.B. in Scriptcase javascript code, field names are all lowercase.

Re: Calculated field in form application

Sorry, this doesn’t work, since I have an editable grid with several rows, in which the fields have names “punkte1”, “punkte2”, “punkte3”, …, “bonus1”, “bonus2”, “bonus3”, …

Meanwhile I use the following code:


// form onLoad event:
document.F1.focusedFormElement = null;

var n = function() {
			document.F1.focusedFormElement = this.form.focusedElement = this;
		},
	p = function() {
			document.F1.focusedFormElement = this.form.focusedElement = null;
		};

	document.F1.focusedElement = null;
	for(var j = 0, e = document.F1.elements; j < e.length; ++j) {
		e[j].onfocus = n;
		e[j].onblur = p;
	}

document.F1.sumPunkte = function(row) {
	var punkteSelector = 'input[name=punkte'+row+']';
	var bonusSelector = 'input[name=bonus'+row+']';
	var gesamtSelector = 'input[name=gesamt'+row+']';
	jQuery(gesamtSelector).val(parseInt(jQuery(punkteSelector).val().replace(/./g, "")) + parseInt(jQuery(bonusSelector).val().replace(/./g, "")));
}

// inputs field's onChange event:
var row = document.F1.focusedFormElement.id.substr(document.F1.focusedFormElement.id.length-1,1);
document.F1.sumPunkte(row);

This code is working fine, if I update existing records.

But if I click the “New” button to insert a record, a new row in the form is created, where the above code is not bound to the newly created input fields.
I need some event to be fired, when the “New” button is clicked, to bind the JS code at that moment to the newly created input fields.

How can I do this???

Re: Calculated field in form application

I had a similar issue but wanted my field calculated onkeyup event on the entire form.
Here is my solution
I put this in “Javascript” - “form” - “onload”


function calcTotal(e) {
Total = F1.elements["total"].value;
lFee = F1.elements["latefee"].value;
gTotal = F1.elements["grandtotal"];
Total = Number(Total);
lFee = Number(lFee);
gTotal.value = (Total + lFee);
gTotal.innerHTML = (Total + lFee);
}

document.onkeyup=calcTotal; 

If you have the result field set as read only you will have to change the innerHTML line to something like this
document.getElementById(‘id_read_on_grandtotal’).innerHTML = (Total + lFee);

Doing it via javascript limits functionality such as searching. I think SC should really add PHP event OnRecord such as for the grids.
I find it really strange it is not there yet.