I would appreciate some help with an issue.
This is the case: I want to hide/show input text field and corresponding label when a user chooses a certain values in drop down list.
Module is: Opportunities.
Mode: Edit
Drop down list is: sales_stage
Input Text Field 1: hideunhide_c
Text Field 1 lable: LBL_HIDEUNHIDE
When sales_stage selected value is: “Closed Won” text field and label should be shown otherwise it should be hidden.
I have managed to show/hide text field but for some reason I can not hide label.
This is what I did so far:
\custom\modules\Opportunities\views\view.edit.php file:
<?php
require_once('include/MVC/View/views/view.edit.php');
class OpportunitiesViewEdit extends ViewEdit{
function OpportunitiesViewEdit(){
parent::ViewEdit();
}
function display(){
echo '';
parent::display();
}
}
?>
And custom/include/javascript/showhide.js file:
$(function () {
$("#sales_stage").change(function () {
var selectedValue = $(this).val();
if (selectedValue == "Closed Won"){
$("#LBL_HIDEUNHIDE").show();
$("#hideunhide_c").show();
}else{
$("#LBL_HIDEUNHIDE").hide();
$("#hideunhide_c").hide();
}
});
});
For some reason toggling show/hide on a label is not working.
For the input field it works just fine.
I tried with closest “tr” and “td” but with no luck > $("#hideunhide_c").closest(“tr”).hide();
Where am I wrong?
Ok. I found one more piece related to this issue.
I just added conditional validation on this field to make it required.
And once again I am able to make what I want with the field but I am unable to do it with its label!
So I managed to make a field required when a user choose “Closed Won” in sales_stage ddl.
But somehow LBL_HIDEUNHIDE drives me crazy. I can not append red * to it.
Why?
Here is my code:
<?php
require_once('include/MVC/View/views/view.edit.php');
class OpportunitiesViewEdit extends ViewEdit {
public function __construct() {
parent::ViewEdit();
}
function display() {
global $mod_strings;
$jsscript = <<<EOQ
EOQ;
echo '';
parent::display();
echo $jsscript;
}
}
?>
OK. So I made a little work around and managed to toggle visibility of the label as well by hiding complete parents of the input field.
if (selectedValue == “Closed Won”){
$("#hideunhide_c").parent().parent().show();
}else{
$("#hideunhide_c").parent().parent().hide();
}
However I’m still not sure what to do about adding a red star at the end of the label when dynamically making field required.
if(status == ‘Closed Won’){
addToValidate(‘EditView’,‘hideunhide_c’,‘varchar’,true,’{$mod_strings[‘LBL_HIDEUNHIDE’]}’);
$(’#LBL_HIDEUNHIDE’).html(’{$mod_strings[‘LBL_HIDEUNHIDE’]}: *’);
}
else{
removeFromValidate(‘EditView’,‘hideunhide_c’);
$(’#LBL_HIDEUNHIDE’).html(’{$mod_strings[‘LBL_HIDEUNHIDE’]}: ');
}
This doesn’t work since #LBL_HIDEUNHIDE cannot be referenced by id (id=""). Referencing by class I haven’t managed to make it work yet.
Any tips?
Ok. Traversing the DOM is not something I do very often as you could imagine.
Find a solution. A bit ugly but it works.
The way I reference label is like this:
$(’#hideunhide_c’).parent().parent().children().first()
So now I have a red * when condition is met:
if(status == ‘Closed Won’){
addToValidate(‘EditView’,‘hideunhide_c’,‘varchar’,true,’{$mod_strings[‘LBL_HIDEUNHIDE’]}’);
$(’#hideunhide_c’).parent().parent().children().first().html(’{$mod_strings[‘LBL_HIDEUNHIDE’]}: *’);
}
else{
removeFromValidate(‘EditView’,‘hideunhide_c’);
$(’#hideunhide_c’).parent().parent().children().first().html(’{$mod_strings[‘LBL_HIDEUNHIDE’]}: ');
}
Referencing like: .parent().parent().children().first() is hardly the best way to do it but it works.
Would like to here from someone how to do it in, let’s say, proper way!