Forum

Hide field dependin...
 
Notifications
Retirer tout

Hide field depending on another field's value

6 Posts
2 Utilisateurs
0 Reactions
1,361 Vu
Louis Heredero
(@baryhobal)
Posts: 49
Trusted Member
Début du sujet
 
[#7593]

Hello,

I'm making another custom element and I have certain buttons in the creation form that I would like to be hidden unless the field "idMailable" has a certain value.
I can't do that with the screenCustomization plugin since it only handles type, status and profile conditions. Is there a specific function that is called when a field is changed and if so, how can I hide/show buttons without refreshing the page ?

I hope someone can help me


 
Posté : 22/06/2021 2:14 am
(@babynus)
Posts: 14952
Membre Admin
 

What you try and do is not possible.
You would need to change the core code.


 
Posté : 22/06/2021 1:00 pm
Louis Heredero
(@baryhobal)
Posts: 49
Trusted Member
Début du sujet
 

Even if this is a new custom class ?
I was thinking about using the validation script to do something when the field changes, but I'm not sure what is the best way to hide field with js, but I think I've got something that could work.

In the onChange script of the idMailable field, I added this

function hideButton(id) {
  dojo.byId(id).parentNode.parentNode.parentNode.style.display="none";
}
function showButton(id) {
  dojo.byId(id).parentNode.parentNode.parentNode.style.display="";
}
dojo.xhrGet({
  url : "../tool/telegramDisplayTemplateVisibleButtons.php?idMailable=" + dijit.byId("idMailable").get("value"),
  handleAs : "json",
  load : function(buttons) {
    buttons["hidden"].forEach(but => hideButton(but));
    buttons["visible"].forEach(but => showButton(but));
  },
  error : function() {}
});

And if I also call this part of the script when the field loads, it should work just fine


 
Posté : 22/06/2021 3:34 pm
Louis Heredero
(@baryhobal)
Posts: 49
Trusted Member
Début du sujet
 

Okay so it works. The problem left is how to run the script when the field loads. I tried using the


but it doesn't work (it's not triggered). I'm not an expert with dojo so I don't know how to run some js when an element is loaded


 
Posté : 22/06/2021 4:09 pm
Louis Heredero
(@baryhobal)
Posts: 49
Trusted Member
Début du sujet
 

I got it to work. I just set the hidden fields' attributes to "invisible" and the script now looks like this:

function hideButton(id) {
  dojo.byId(id).parentNode.parentNode.parentNode.style.display="none";
  dojo.byId(id).parentNode.style.display="none";
  dojo.byId(id).parentNode.parentNode.parentNode.firstChild.firstChild.style.display="none";
}
function showButton(id) {
  dojo.byId(id).parentNode.parentNode.parentNode.style.display="";
  dojo.byId(id).parentNode.style.display="";
  dojo.byId(id).parentNode.parentNode.parentNode.firstChild.firstChild.style.display="";
}
dojo.xhrGet({
  url : "../tool/telegramDisplayTemplateVisibleButtons.php?idMailable=" + dijit.byId("idMailable").get("value"),
  handleAs : "json",
  load : function(buttons) {
    buttons["hidden"].forEach(but => hideButton(but));
    buttons["visible"].forEach(but => showButton(but));
  },
  error : function() {}
});

 
Posté : 22/06/2021 4:57 pm
(@babynus)
Posts: 14952
Membre Admin
 

I think you should have used the existing hideWidget() function
or showWidget(), or disableWidget() or enableWidget()


 
Posté : 24/06/2021 2:49 pm
Share:
Retour en haut