This topic is locked
[SOLVED]

 Javascript event with datepicker

7/2/2011 4:05:58 AM
PHPRunner General questions
G
giwi_dev author

I have a dynamic calculation on my page that uses a difference between two dates to calculate a total storage fee.

It works great except not with a datepicker field.
I tried using the change event like

ctrlStartDate.on('change', setTotal);

ctrlEndDate.on('change', setTotal);
and it doesn't work if i choose a date in the datepicker calendar popup
this works

ctrlStartDate.on('click', setTotal);

ctrlEndDate.on('click', setTotal);
but i don't want the user to first choose a date without anything 'happening' only after they they click in the date edit box

I'm probably missing something obvious
Thanks for any advice you can provide

D
Dale 7/2/2011

Just a quick stab, should it not be onchange instead of just change ?????

G
giwi_dev author 7/2/2011

Thanks, but that did not work.
I'm a newbie to this tool and assumed the events are set like
control.on('eventname', functioncall);
The on is already in front of the input parameters. I already searched the forum but couldn't find an answer to this particular question.

C
cgphp 7/2/2011



this works

ctrlStartDate.on('click', setTotal);

ctrlEndDate.on('click', setTotal);
but i don't want the user to first choose a date without anything 'happening' only after they they click in the date edit box

I'm probably missing something obvious
Thanks for any advice you can provide


After you have computed the difference, try to trigger the click event on a different field .

$('#second_field_id').trigger('click');
G
giwi_dev author 7/3/2011

>>After you have computed the difference, try to trigger the click event on a different field
That would be an option but i first need to recalculate the total after changing the date in the calendar popup, before being able to trigger any other event.
What i did for now is putting a recalculate button on the form so its clear for the user they need to manually recalculate the balance after changing the other parameters. I'm still interested in achieving this on the events of a date field tied to a calendar popup though.

C
cgphp 7/3/2011

Please, post the js code.

G
giwi_dev author 7/4/2011

i use this code in the onpageload call
//set vars

var ctrlTariff = Runner.getControl(pageid, 'StorageTariffRates');

var ctrlTotal = Runner.getControl(pageid, 'TotalElevationCharges');

var ctrlPaid = Runner.getControl(pageid, 'PrepaidElevationCharges');

var ctrlBalance = Runner.getControl(pageid, 'CreditDebit');

var ctrlStartDate = Runner.getControl(pageid, 'StorageStartDate');

var ctrlEndDate = Runner.getControl(pageid, 'DateStoragePaidThrough');

var differenceInDays = DateDiff(ctrlEndDate.getValue(),ctrlStartDate.getValue())/1000/60/60/24;
//set default value in total field first

setTotal();
function setTotal() {

differenceInDays = DateDiff(ctrlEndDate.getValue(),ctrlStartDate.getValue())/1000/60/60/24;

ctrlTotal.setValue(differenceInDays*parseFloat(ctrlTariff.getValue()));

ctrlBalance.setValue(ctrlTotal.getValue()-ctrlPaid.getValue());

};
function setBalance() {

ctrlBalance.setValue(ctrlTotal.getValue()-ctrlPaid.getValue());

};
function DateDiff(date1,date2) {

return date1.getTime() - date2.getTime();

}
//add events for boxes on form

ctrlTariff.on('change', setTotal);

ctrlStartDate.on('click', setTotal);

ctrlEndDate.on('click', setTotal);

ctrlPaid.on('change', setBalance);