Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Dynamic dropdown ?

Post Reply
MaxGQC
Posts: 2
Joined: Sun Nov 16, 2014 1:21 pm

Dynamic dropdown ?

Unread post by MaxGQC »

Hi

I want to add a dropdown on my form and make the content change dynamically based on the selection of another dropdown

I have 2 table:

maker
id | maker_name
1 | Lenovo
2 | HP

model
id | name | maker
1 | Thinkpad | 1
2 | Probook | 2
1 | Thinkcentre | 1

In the form, if I select Lenovo as the maker, the choices on the 2nd dropdown are Thinkpad and Thinkcentre

I've been able to do it but I had to save the record before the content of the 2nd dropdown changes

How can I do this ?

Thanks and sorry for my bad english
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Dynamic dropdown ?

Unread post by admin »

MaxGQC,

Run this populate_select() onchange of the_maker.

Code: Select all


function populate_select(){

    $("#the_dropdown option").remove();
    $("#the_dropdown").append("<option value=''></option>");

    if($('#the_maker').val() == '1'){
        $("#the_dropdown").append("<option value='thinkpad'>Thinkpad</option>");
        $("#the_dropdown").append("<option value='thinkcentre'>Thinkcentre</option>");
    }
    
    if($('#the_maker').val() == '2'){
        $("#the_dropdown").append("<option value='probook'>Probook</option>");
    }
    
}

Steven
MaxGQC
Posts: 2
Joined: Sun Nov 16, 2014 1:21 pm

Re: Dynamic dropdown ?

Unread post by MaxGQC »

Thanks for your answer but I need to get the value in the dropdown from a db table, not hard coded
Fike
Posts: 79
Joined: Thu Oct 20, 2011 9:13 pm

Re: Dynamic dropdown ?

Unread post by Fike »

Hi,

I have done that using a (hidden) subform that contains the records of the table from which you want to get the values.

You would need to adapt the JavaScript function provided by Steven in order to get the values from that subform, and execute the function when the 'Marker' is selected using an onchange event.

Hope this helps.



Regrads,

Fike
nekpap
Posts: 27
Joined: Wed Dec 03, 2014 7:41 am

Re: Dynamic dropdown ?

Unread post by nekpap »

Hi,

How do you refresh the subform?

Nektarios
jacky
Posts: 10
Joined: Wed Apr 22, 2015 9:50 am

Re: Dynamic dropdown ?

Unread post by jacky »

Hello,
Do you have a working example with mysql ? because I am very interested in this feature.

Thank you very much for your help.

Jacky
jacky
Posts: 10
Joined: Wed Apr 22, 2015 9:50 am

Re: Dynamic dropdown ?

Unread post by jacky »

Hello,
I managed to run the example above.
Unfortunately I am in subform, how to automatically determine the names of the marker and dropdown ?

Thanks,
Jacky
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Dynamic dropdown ?

Unread post by admin »

jacky,

A Lookup Object can be used to give a filtered list based on a Form Object that might or might not change before the Form is saved.

http://wiki.nubuilder.net/index.php/Add ... Lookup_Tab

Steven
Post Reply