Page 1 of 1

Detail view field calculation update issue

Posted: 2021-06-14 19:45
by bwalker
Hello group, me again,
I have this js code from the udemy course that updates the age field of the employee table.
The code works if I do a save but will not update the 'age' field after selecting a new BirthDate .
I believe it is supposed to update the form on a new selection then the DB on the save.
here is the code in the 'employee-dv.js' file
$j(function(){
$j('#Age').prop('readonly',true);

$j('#BirthDate-dd,#BirthDate-mm,#BirthDate').change(function(){
var dob = get_date('BirthDate');
var today = new Date();
var age = Math.floor((today-dob) / 1000/60/60/24/365.25);

$j('#Age').val(age);
});
$j('#BirthDate').change();
});
Thanks for any direction you can offer,
Bill Walker

Re: Detail view field calculation update issue

Posted: 2021-06-14 21:47
by pbottcher
Hi,

I think you are missing the get_date function (at least in your post)
Also please use the code formating feature to put your code in the post. This makes reading easier.

Re: Detail view field calculation update issue

Posted: 2021-06-14 23:27
by bwalker
The get_date function is in the footer-extras.php file as it is used in multiple places.
Here is it's code:
<script>
function get_date(date_field){
var y = $j('#'+ date_field).val();
var m = $j('#'+ date_field + '-mm').val();
var d = $j('#'+ date_field + '-dd').val();

var date_object = new Date(y, m - 1, d);

if(!y) return false;

return date_object;
}
</script>
I will have to investigate the code formatting feature.
Thank you.

Re: Detail view field calculation update issue

Posted: 2021-06-15 13:13
by bwalker
OK, so this is getting strange. None of this code is even being called. I have commented out the whole function and yet the AGE is still getting calculated on SAVE only. So where is this coming from? That is what I need to figure out.
I will report back any results found.
Thanks

Re: Detail view field calculation update issue

Posted: 2021-06-15 20:16
by pfrumkin
Hi

Does the line
var dob = get_date('BirthDate');
need to be
var dob = get_date('#BirthDate');

~Paul

Re: Detail view field calculation update issue

Posted: 2021-06-16 19:12
by pbottcher
The code is fine. The error must be somewhere else. Check for other error like upper/Lowecase mismatch, Errors in the console.