Show/Hide fields depending on Lookup

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
User avatar
jsetzer
Veteran Member
Posts: 67
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Show/Hide fields depending on Lookup

Post by jsetzer » 2018-07-24 18:07

Hi,

I have an events-table with a couple of date/time fields:
AppGini_2018-07-24_19-30-52.png
AppGini_2018-07-24_19-30-52.png (5.48 KiB) Viewed 823 times

Depending on the value of eventtype_id, which is a dropdown, I wanted to hide certain fields, for example...

...when selecting eventtype "Antragstellung" (Date of application) I only need one date...
chrome_2018-07-24_19-32-37.png
chrome_2018-07-24_19-32-37.png (6.51 KiB) Viewed 823 times

...when selecting eventtype "Mehrtätiges Ereignis ohne Uhrzeiten" I need two dates but no times ...
chrome_2018-07-24_19-35-28.png
chrome_2018-07-24_19-35-28.png (8.97 KiB) Viewed 823 times

...when selecting eventtype "Mehrtätiges Ereignis mit Uhrzeiten" I need two dates with times ...
chrome_2018-07-24_19-36-20.png
chrome_2018-07-24_19-36-20.png (10.93 KiB) Viewed 823 times

As you can see the visibility of certain fields changes.

This is mainly done by listening to the change event of the dropdown, getting the id of the selected dropdown-item, pulling some JSON data from the server via ajax and finally hiding fields according to the JSON data.

I'm not only hiding fields, I'm also changing labels:

2nd screenshot "date_from"-label = "Datum"
3rd screenshot "date_from"-label = "Datum (von)"

This is the same field "date_from" in the database.


This is part of events-dv.js:

Code: Select all

    // on change of eventtype_id: update fields
    $j('#eventtype_id-container').change(function () {
        var id = $j(this).val();
        updateFields(id);
    });
    
    // ... there is more here
    
    // load eventtype by id using ajax with josn response
    $j.getJSON('hooks/eventtypes.api.php?id=' + id, function (data) {

        console.log(data);

        // ...
        
        // hide/show fields
        new AppGiniField('date_from')
            .visible(data.show_date_from)
            .label(data.show_date_from && data.show_date_till, 'Datum (von)', 'Datum')
            ;
        new AppGiniField('time_from')
            .visible(data.show_time_from)
            .label(data.show_time_from && data.show_time_till, 'Uhrzeit (von)', 'Uhrzeit')
            ;
        new AppGiniField('date_till').visible(data.show_date_till);
        new AppGiniField('time_till').visible(data.show_time_till);
        new AppGiniField('custom').visible(data.show_custom);

    });
I have a separate eventtypes table which allows customers to customize the diffent lookup values and the visibility of fields like so:
chrome_2018-07-24_20-06-12.png
chrome_2018-07-24_20-06-12.png (9.3 KiB) Viewed 823 times

User avatar
a.gneady
Site Admin
Posts: 938
Joined: 2012-09-27 14:46
Contact:

Re: Show/Hide fields depending on Lookup

Post by a.gneady » 2018-08-08 13:01

Awesome! Thanks for sharing :)
AppGini: Responsive, collaborative web db apps in minutes.
:idea: Learn all the tips and tricks of customizing AppGini apps through our online course.

User avatar
jsetzer
Veteran Member
Posts: 67
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Show/Hide fields depending on Lookup

Post by jsetzer » 2018-10-14 10:08

See also: Styling the default AppGini-Checkboxes
https://forums.appgini.com/phpbb/viewto ... f=4&t=2817

Alisson
Posts: 15
Joined: 2017-02-25 20:32

Re: Show/Hide fields depending on Lookup

Post by Alisson » 2018-11-12 18:11

This is what I been looking for, would you mind to share more info about this,like the content of the file "eventtypes.api.php"
and the code you use where is says ... there is more here.

Thank you.

xbox2007
Posts: 28
Joined: 2016-12-16 16:49

Re: Show/Hide fields depending on Lookup

Post by xbox2007 » 2018-11-16 22:38

Thanks for sharing

will add this code inside tablename-dv.js

about this file ( eventtypes.api.php ) we should create and what code inside .

User avatar
jsetzer
Veteran Member
Posts: 67
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Show/Hide fields depending on Lookup

Post by jsetzer » 2018-11-16 23:18

Recently I got a couple of requests for publishing code or for sending complete solutions. I'm sorry and I hope for your understanding that I cannot publish complete files due to complexity of includes (included libraries) and due to legal issues. To give you an example: my included JavaScript codefile contains more than 1600 lines of code providing dozens of UI-functions. And there are another 12 php files + included libraries full of helping functions for workflow, email processing, logging, html-generation and pdf creation.

The good thing is: To hide/show fields you do not need the content of eventtypes.api.php.

All you need is:
  • a server-side php file returning json for every field you need to hide/show.
  • Reading data from mysql using AppGini's sqlValue() or sql() function has been described in the forum and help files
  • To return database values as json: Search the php manual for "json_encode" function
  • Hiding fields has been described somewhere here in the forum. Try $j('#fieldname').closest('.form-group').hide();
Hope this will help you to solve the problem.

Regards,
Jan

Post Reply