Issue with dynamic fields in detail view of custom module

Hello !
I have created several custom modules, in which some fields are dynamic (depend on the value of a dropdown). I managed to make those fields appear/disappear as I want in edit View and quickcreate View, with a js function Added on the onchange parameter of the dropdown field.
I then adapted my functions to make them appear/disapear in detail view. I changed my javascript file so that the function runs when the file is loaded, and I Added the file like that :
$viewdefs [“crm20_Test”][“DetailView”][“templateMeta”][‘includes’] = array ( array (‘file’ => ‘custom/include/showhide_test_detail.js’));

For my first module, it works perfectly, and the fields are displayed as I wish.

The issue is with the second one. I do exactly the same thing, but it doesn’t work. In my js file, I used a getElementById to get the value of my dropdown field (on which the others depend), but it always return “null”. I checked my html page, and the id I am using is the right one.
I feel like in my two modules, the included js files don’t run at the same step of the page loading, is that possible ? Do you know what defines that and how to change it ?

I really don’t understant where is the problem, I created my two custom modules exactly the same, and proceeded the same way with everything.

Please I really need help with that, so if anyone has a solution for me, or an intell on how to locate the issue :pleading_face:
Or even another way that I could hide fields dynamicly on detail-view…

1 Like

Ok , so I let it rest for a few hours, and now suddently it works…
I don’t know if the issue solved itself or if there never was one from the beginning.

Anyway, I gess I don’t have an issue anymore :smile:

Hi, so I figured out what the issue was and how to solve it, so I thought I would share it, in case anyone wants to do the same thing.
So as I said before, I was trying to run a script during the page loading. I did that by including a js file in the detailviewdef.php of the module.
The issue is that, depending on how you get to the detailView(from List View or from Edit View), the js file is sometimes loaded before the HTML, so the functions Don’t work properly.

I managed to solve that by adding one line at the beginning of my js file :

document.addEventListener(“DOMContentLoaded”, myfunction, false);

Then I define the function called , containing the instructions needed.

function myfunction(e) {
// here I have my indications to hide fields depending on other values displayed in the page

This also works for EditView, so that the fields that you hide with an “onchange” function are also hidden before changing the value, when there is a default value at the page loading.

I don’t know if I’m being clear, or if my post will help anyone :slight_smile:

1 Like