I would like to add x-years to a Start Date selection and have it automatically update the End Date field based on an Interval with a call-back function.
To further explain:
I have four columns.
Skill ID, Start Date, Interval and End Date.
When adding a new record, I select the Skill ID and it runs the call-back function and populates the Interval-years. This works perfectly. I then select the “Start Date” from a date-picker. The next-thing I would like is to be able to update the “End Date” field, based the “Start Date” plus the number of years in the Interval column – with an update or callback function.
I tried the callback function – but it does not appear to work.
Any thoughts?
Thanks
Mike
Hi,
You need to adjust end_date value in callback, e.g.
// set 3rd param to true – means don’t stop after callback and continue grid insert/update.
$e[“on_update”] = array(“update_client”, null, true);
$g->set_events($e);
function update_client($data)
{
$interval = $data[“params”][“interval”]; // assuming interval is numeric and in dayss
$data[“params”][“end_date”] = date(“Y-m-d”, strtotime(“+$interval days”, $data[“params”][“start_date”]));
}
This will make update query with end_date field as well. Code is just to give idea.
If it does not work as expected, you can share grid code for review.
Thanks for the feedback. The problem I have is I need the “ADD RECORD” form to display the END DATE so the person entering the data sees the date change. I tried the direct update first and that works – but we need feedback for the person entering the data.
Thanks
Mike
Ok, thats doable as well. You can connect a JS function with onblur event on start_date and interval fields.
And inside that callback, fetch values of both source fields and set value in end_date. For help refer this code demo in faq: https://www.gridphp.com/faqs/#80
Some JS date calculation would be required, you can check https://stackoverflow.com/questions/563406/add-days-to-javascript-date