This topic is locked
[SOLVED]

 calculate values on the fly

5/3/2012 9:23:23 PM
PHPRunner General questions
S
stiven author

Hello all,
I got the example from the manual to calculate values on the fly, but I have more tan 3 fields and the problem is that sometimes not all the fields are filled so in the result (total) I get NaN if not all the fields have a value. Is there a way to fix this? there are more than 5 fields. here is the code i have maybe there is a solution for this



var cs1 = Runner.getControl(pageid, 'funeral_director_and_stuff');

var cs2 = Runner.getControl(pageid, 'embalming');

var cs3 = Runner.getControl(pageid, 'reconstruction');

var cs4 = Runner.getControl(pageid, 'dressing');

var cstotal = Runner.getControl(pageid, 'total_services');
function func() {

cstotal.setValue(parseFloat(cs1.getValue()) + parseFloat(cs2.getValue()) + parseFloat(cs3.getValue()) + parseFloat(cs4.getValue()));

};
cs1.on('keyup', func);

cs2.on('keyup', func);

cs3.on('keyup', func);

cs4.on('keyup', func);


Thanks for your help

Sergey Kornilov admin 5/4/2012

Try the following:

var cs1 = Runner.getControl(pageid, 'funeral_director_and_stuff');

var cs2 = Runner.getControl(pageid, 'embalming');

var cs3 = Runner.getControl(pageid, 'reconstruction');

var cs4 = Runner.getControl(pageid, 'dressing');

var cstotal = Runner.getControl(pageid, 'total_services');
function func() {

cstotal.setValue(+cs1.getValue() + +cs2.getValue() + +cs3.getValue() + +cs4.getValue());

};
cs1.on('keyup', func);

cs2.on('keyup', func);

cs3.on('keyup', func);

cs4.on('keyup', func);


Single plus operator does the job similar to parseFloat converting textual representation to Number object. It smarter though and can handle empty values.
Alternative syntax:

cstotal.setValue(Number(cs1.getValue()) + Number(cs2.getValue()) + Number(cs3.getValue()) + Number(cs4.getValue()));
S
stiven author 5/7/2012

Thanks so much it works perfect!!