You are viewing limited content. For full access, please sign in.

Question

Question

Set Drop down Default from Database lookup

asked on July 3, 2019

How can I set the drop down default on a field with a database lookup? I have multiple places where I would like to do this, but I will just give the simplest case. I have a Yes/No question where the answer is stored in the database. When I do the database lookup the value stored in the database i shown in the drop down. However, it is the only value stored in the drop down. When I add the vales Yes and No to the Choices of the drop down, and select Append choices to lookup results, I get both choices back. However, neither appear in the drop down automatically. I have to click the drop down arrow. Then I see the value stored in the database first with the other option second. Is there a way I can set the vale from the database lookup to be the default so it is automatically shown, and the remaining value an been seen when you click the drop down? I want my users to be required to have a value, but not have to select one from the drop down if they want to leave the value the same as what is already in the database.

0 0

Replies

replied on July 4, 2019 Show version history

So with the way things currently work, the only way a lookup will auto-populate a dropdown is if the result is the only value in the field.

However, the way around this is custom JavaScript and a second hidden field that actually holds the lookup value from the database.

What I do is this:

  1. Create a Single Line field that actually retrieves the lookup value from the database
  2. Under Field Rules, set the Single Line to always hidden
  3. Set Yes/No as the options for the Dropdown field (with no lookup)
  4. Set an "on change" event for the Single Line that changes the value of the Dropdown

 

For example,

I could give a CSS class of "lookupField" to the Single Line and a "lookupTarget" class to the dropdown, then use the following code.

$(document).ready(function(){
    $('.lookupField input').on('change',function(){
        $('.lookupTarget select').val($(this).val()).change();
    });
});

Now, when the lookup completes it will trigger a change event on your single line field, which in turn updates the value of your drop down to whatever was in the data source.

It is somewhat roundabout, but it'll get the job done.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.