This topic is locked

JavaScript code to convert Date failing in popup

5/8/2022 6:17:04 PM
PHPRunner General questions
P
ppradhan@live.com author

Hi, This code works great in the normal Add Page, but when I open in the Popup window it fails. Looks like I'm not using the right selector or wrong pattern. Please help me fix this issue.

What is this about?
This is about a JS plugin called Nepali Date Picker that I've included in the project and works great in the normal ADD page, but fails during popup window.
This plugin will show the Nepali Date Calendar that has ability to convert date between English Date (AD) and Nepali Date (BS).
There are two Text Box: one for English Date 'date_of_birth' and another for Nepali Date 'dob_bs'. Both are for Date of Birth.
User can either select date in English or Nepali. Either way the date is calculated/converted and sets automatilcally in the other text box.

I've attached image, that may make easier to understand.

Code in Before Display of Add Page:

// LOAD RESOURCES FOR NEPALI DATE PICKER
$pageObject->addJSFile("nepali.datepicker.v3.7/js/nepali.datepicker.v3.7.min.js");
$pageObject->addCSSFile("nepali.datepicker.v3.7/css/nepali.datepicker.v3.7.min.css");

Code looks like this in the JavaScript Onload event of Add Page:

var ctrlDob_ad = Runner.getControl(pageid,"date_of_birth");
var ctrlDob_bs = Runner.getControl(pageid,"dob_bs");
ctrlDob_bs.addClass("nepali-datepicker");

$(document).ready(function(){
var englishDateFormat = "YYYY/MM/DD";
var englishDateInputId = "value_date_of_birth_1";
var nepaliDateInputId = "value_dob_bs_1";

$('#value_dob_bs_1').nepaliDatePicker({ //this is to initialize the calender, ndpEnglishInput: will write the converted English date to the date_of_birth textbox
ndpYear: true,
ndpMonth: true,
ndpYearCount: 80,
ndpEnglishInput: 'value_date_of_birth_1'
});
});

ctrlDob_ad.on('change', function(){

var currentValue = document.getElementById("value_date_of_birth_1").value;
var convertedNepaliDate = NepaliFunctions.ConvertDateFormat(NepaliFunctions.AD2BS(NepaliFunctions.ConvertToDateObject(currentValue, "YYYY-MM-DD")), "YYYY-MM-DD");

$('#value_dob_bs_1').val(convertedNepaliDate); // UPDATE DOB_BS

});

img alt

img alt

Admin 5/9/2022

It sounds a bit strange that the the same code works diferently in a popup and on a regular page. My guess is that you are not using recommended Javascript APIs and relying on some tags/selectors that work differently in a popup.

Check PHPRunner's Javascript API:
https://xlinesoft.com/phprunner/docs/ctrl_getvalue.htm
https://xlinesoft.com/phprunner/docs/ctrl_setvalue.htm

Also, if you are interested in troubleshooting your Javascript code and finding errors there, we recommend checking this excellent video:
https://www.youtube.com/watch?v=o5o18eawWpE

fhumanes 5/17/2022

Hello @ppradhan@live.com,

I think, it is only an assumption, that your problem occurs because you think the field is called "value_dob_bs_1".

I do not know the reason, but when it is a Popup screen, the names of the fields are formed with a different number sequence and it may at one time be "value_dob_bs_23" and in another "value_dob_bs_31". You should not presuppose in the field identifier.

In the construction of plugin, the name of the field is provided by the interface, but without the construction of plugin I do not know how to obtain this value.

Greetings,
fernando

P
ppradhan@live.com author 5/17/2022

@fhumanes, thank you sir. You are correct, the fields would appear with different name while at popup window.
I did a little research and found a way to half fix my current issue. I used below code:

var id_ad = $("[id^=value_date_ofbirth]")[0].id ;
var id_bs = $("[id^=value_dobbs]")[0].id;

this would get me the id of the date fields correctly in the popup window too. And good news is that when I input english date, it does calculate and auto-populate nepali date in the next field.
But when I select the Nepali date, the converted date is not populated in the english date field. Meaning there is still something missing that I cannot help myself.

fhumanes 5/17/2022

Hello,

In the case of a phprunner field, of date, this field is not treating it as a "string" field, it is handled as a "datetime" type and I think that your problems can come from.

Greetings,
fernando