Show/Hide field based on lookup

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
kbarrett
Veteran Member
Posts: 50
Joined: 2019-02-24 16:35
Location: Calgary Alberta

Show/Hide field based on lookup

Post by kbarrett » 2019-04-17 18:48

Hi all,

I likely am way of with this but I am trying to show hide a field or two based on one value from a lookup field. I thought I would create a tablename-dv.js file with the following code in it. I have tried both text and a numerical value of the text selection. Neither works:

$j(document).ready(function(){
$j('#My_Field').change(function() {
if($j('#My_Field').text()=="This Value")
$j('#ScID').show();
else
$j('#ScID').hide();

})

})


I have used the code from Ahmed's tutorials but it does the opposite and hides the field when the lookup is selected:

$j(function(){
$j('#My_Field').on('change', function(){
var IR = parseFloat($j(this).val());

if(IR == 'This Value'){
$j('#ScID').parents('.form-group').show();
}else{
$j('#ScID').parents('.form-group').hide();
}
});
});

I am not sure what I have to do, I did try and use the value of the selection (7) as well.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Show/Hide field based on lookup

Post by pbottcher » 2019-04-17 19:31

Hi Kevin,

if you need only to check for the change you can use:

Code: Select all

$j(function(){
$j('#My_Field').on('change', function(){
var IR = $j('#s2id_My_Field-container .select2-chosen').text());

if(IR == 'MYVALUE'){
$j('#ScID').parents('.form-group').show();
}else{
$j('#ScID').parents('.form-group').hide();
}
});
});
replace My_Field by your fieldname and MYVALUE by the selected item from the lookup.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

kbarrett
Veteran Member
Posts: 50
Joined: 2019-02-24 16:35
Location: Calgary Alberta

Re: Show/Hide field based on lookup

Post by kbarrett » 2019-04-17 19:55

Hi,

I substituted my values and it works in reverse, every selection hides the ScID field. Ideally the field stays hidden and selecting 'Safety Concern' shows the ScID field:

$j(function(){
$j('#Inspection_Reason').on('change', function(){
var IR = $j('#s2id_Inspection_Reason-container .select2-chosen').text();

if(IR == 'Safety Concern'){
$j('#ScID').parents('.form-group').show();
}else{
$j('#ScID').parents('.form-group').hide();
}
});
});

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Show/Hide field based on lookup

Post by pbottcher » 2019-04-18 13:04

Hi,
not sure what the issue is. Every selection (apart "Safety Concern") will hide the ScID. Only if you select the Safety Concern, it will be shown.

This does of course not work for the initial load of the page, as you react with the "on('change'..) only on changes to the fieldvalue.

If you want to hide field once the page gets loaded, you need to check for the value at the time the page is loaded and set the field status accordingly.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

kbarrett
Veteran Member
Posts: 50
Joined: 2019-02-24 16:35
Location: Calgary Alberta

Re: Show/Hide field based on lookup

Post by kbarrett » 2019-04-18 21:00

Yes,

Once I click on the drop down the field disappears and doesn't come back. LOL.

Post Reply