Hidding field depending of a drop-down list

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
Authen-TIC
Posts: 13
Joined: 2015-12-16 12:54

Hidding field depending of a drop-down list

Post by Authen-TIC » 2015-12-29 18:30

Hi, is it possible to hide or change field in the detail view. example I want to see all my field if the drop-down list is select to "option 1" and hide or change field if the drop-down list is select to "option 2". Thanks!

MsMetaP
Posts: 9
Joined: 2013-05-05 00:30

Re: Hidding field depending of a drop-down list

Post by MsMetaP » 2015-12-30 22:17

Wow. I came here to ask the same question. Thanks for doing it first! Only difference is I have three choices in my drop down and I need one of the specific fields to display based on the choice. Fingers crossed for an answer. I think this is called a dynamic form?

samrainbow
Posts: 11
Joined: 2015-09-24 14:06

Re: Hidding field depending of a drop-down list

Post by samrainbow » 2016-01-05 13:58

That code snippet works :
in that example, we suppose we've got a dropdown called (id=) 'Cities' and a text input called (id=) 'Manager', and then another dropdown called 'Departments'.

Put the following code after changing it to make it fit your needs in the file tablename_templateDV.html. Add this snippet at the end of the file.

Now let's look at what the code does :


<script>
// when the document is properly loaded
$j(document).ready(function()
{

// when the select2 dropdown's option changes

$j("#Cities").change(function()
{

// if NY is selected in the dropdown

if($j("#Cities :selected").text()=="New-York")
{
// Let's hide the field 'Offices', there isn't any manager in NY.

$j("#Manager").hide();

}
// if London is selected in the dropdown
if($j("#Cities :selected").text()=="London")
{
// We reshow the field Manager, there is one in London.
$j("#Manager").show();

}
// if Paris is selected
if($j("#Cities :selected").text()=="Paris")
{
// We remove the choice "Pre-sales", supposing there isn't any Pre-sales department in Paris.


$j("#Departments option[value='Pre-sales']").remove();

// Note : you can dynamically use a lookup rather than removing the options, but it's easier to put it 'in strong' in your html code when you want to build a dynamic form in detail view. You can use a hook to check (server-side) that the user didn't try to bypass the javascript dynamic code (a determined user could indeed try to modify the javascript option in order to make the hidden options shown. Look further)

}

});

});
</script>


in the file tablename.php (in the hooks folder) :

function tablename_before_insert(&$data, $memberInfo, &$args)
{
// if the form returned Paris as the selected choice for the field 'Cities'
if($data['Cities'] == 'Paris)
{
// Then we check whether the selected choice (here, a department) for the 'Departments' is allowed or not.
// it shouldn't be possible to check pre-sales depts (removed dynamically when the option 'Paris' was selected), but perhaps the user tried to hack the javascript code during runtime (with for instance Firefox inspector).
if($data['Departments'] == 'Pre-sales')
{
// No, 'pre-sales' is not allowed for Paris, the data sent to the database will be 'None'.
$data['Departments'] = 'None';
}
}

return TRUE;
}


Good luck to set in for your dynamic form !

hbcornea
Posts: 3
Joined: 2017-01-12 07:54

Re: Hidding field depending of a drop-down list

Post by hbcornea » 2017-01-16 08:33

Hi Samrainbow,

I am sorry but I can't understand it that much. Can you elaborate it more or maybe you can do an example for the first problem here?

Thanks,

Hector

Post Reply