Set combobox to display first item

Questions and answers for designing and implementing forms in Servoy

Set combobox to display first item

Postby jd2p » Fri Aug 10, 2012 7:30 pm

Does anyone know how to configure comboboxes to show by default the first element of the value list associated with it?

Thanks in advance
jd2p
 
Posts: 95
Joined: Thu Dec 08, 2011 1:08 am

Re: Set combobox to display first item

Postby jd2p » Sat Aug 11, 2012 1:12 am

For example, many of our forms show a combobox that's populated from a valuelist based on table Currency. Now, we want all of those objects to show, by default the first element without the user having to select it.
jd2p
 
Posts: 95
Joined: Thu Dec 08, 2011 1:08 am

Re: Set combobox to display first item

Postby matthewbrice » Sat Aug 11, 2012 6:13 am

I am not sure how your code is laid out, but I did the following to achieve what you want. My 2 valuelists have form variables as dataproviders, so I just set their values. One is from an array, the other is from a dataset.

Code: Select all
var _record          = foundset.getSelectedRecord() ;
var _formName          = "win_" + _record.query_id ;
var _returnValues       = forms[_formName].getExistingColumnsRealNames();
var _displayValues      = forms[_formName].getExistingColumnFriendlyNames() ;
application.setValueListItems('blank',_displayValues,_returnValues) ;

//below is what you are looking to do:
_f_dataprovider = _returnValues[0] ;
var _ds          = application.getValueListItems('filterChoices') ;
_f_filterChoices    = _ds.getValue(1,2) ;


If your valuelist comes from a table, perhaps try the following if you put in the 'onShow' event for the form:

Code: Select all
var _ds = application.getValueListItems('name') ;  //returns 2 column dataset with (displayValues, returnValue) columns
foundset.dataprovider = _ds.getValue(1,2) ; 
Per the wiki though:    NOTE: this doesn't return a value for a valuelist that depends on a database relation or is a global method valuelist.


You should not have too many items in the lists, so performance shouldn't be too bad...let me know if this doesn't make sense, or even if you find a better way.

regards,
Matt Brice
matthewbrice
 
Posts: 36
Joined: Wed Jun 13, 2012 6:42 pm

Re: Set combobox to display first item

Postby jcarlos » Sat Aug 11, 2012 7:37 am

jd2p wrote:For example, many of our forms show a combobox that's populated from a valuelist based on table Currency. Now, we want all of those objects to show, by default the first element without the user having to select it.


Do you want to store in the database the 'first element' (value) of the value list or just show it?

I suspect that you want to store that first element without user input? Correct? Is so, just write a method on a record select event (for example) where you set the attribute with the first value (currency) of the value list.

function setDefaultCurrency(){
currency_type = '$';
}

Assuming that the default currency is the dollar.

I hope this help.

Carlos

PS: I wrote this from my iPhone. Check for typos.
jcarlos
 
Posts: 578
Joined: Thu May 04, 2006 8:55 pm
Location: Palo Alto, California USA

Re: Set combobox to display first item

Postby matthewbrice » Sat Aug 11, 2012 6:21 pm

I guess I presumed the valuelists are not know at design time, or the valuelists may change and you don't want to have to recode to display the first value.

But if you really want to hard code an initial return value, the simplest way may be to use the table -> auto enter -> Custom Value for the field and put in "$" or whatever you want to be first. On new record this would in effect set the valuelist to that initial value. If you subsequently change to a different item, that new selection will get saved in the db.

hope this helps,
Matt Brice
matthewbrice
 
Posts: 36
Joined: Wed Jun 13, 2012 6:42 pm

Re: Set combobox to display first item

Postby jd2p » Sat Aug 11, 2012 6:52 pm

Thanks for the replies.

Hey Carlos, I don't want to store a a default value to the DB I just want the combobox to display the first element of the value list. Since the valuelist is populated from a table, I can't hard-code it.

Mathew, the code you provided, I think we could use:

Code: Select all
var _ds = application.getValueListItems('name') ;  //returns 2 column dataset with (displayValues, returnValue) columns
foundset.dataprovider = _ds.getValue(1,2) ; 
Per the wiki though:    NOTE: this doesn't return a value for a valuelist that depends on a database relation or is a global method valuelist.


However, is there a way to do this in a generic way, without having to code it on each form? I have 60 reports in which the users can specify various filters by selecting values from comboboxes and I want that same behaviour. I think I could create a procedure that loops through the form elements and once I find a combobox I use your code above. I'll give that a shot and post back.

Regards,
jd2p
 
Posts: 95
Joined: Thu Dec 08, 2011 1:08 am

Re: Set combobox to display first item

Postby matthewbrice » Sun Aug 12, 2012 4:03 am

You could create a global method that you pass in the form name and have it set the combo boxes for you. In any event I think you are going to have to scan the form for the combo boxes you need to set. Something like this should work (though I am free typing and didn't test it)

In your form's onShow, call a global method - something like
Code: Select all
scopes.globals.globalMethod(controller.getname())


then the global method can:

Code: Select all
   _form = solutionModel.getForm(formName) ;
   _fields = _form.getFields() ;
   
   for ( var i in _fields ){
      if (_fields[i].displayType == JSField.COMBOBOX ){
         _dataprovider = _fields[i].dataProviderID ;
         _valuelist = _fields[i].valuelist ;
         _ds = application.getValueListItems( _valuelist.name ) ;
         forms[formName][_dataprovider] = _ds.getValue(1,2) ;
      }
   }


And on a side note, the two items:
I don't want to store a a default value to the DB
and
I just want the combobox to display the first element of the value list
I think are one and the same. (with two caveats that I can think of : 1. if combobox dataprovider is a variable 2. databaseManager.setAutoSave(autoSave) is false). By setting the combobox/valuelist to the first item, that value IS saved to the DB. So Juan's suggestion and mine do the same thing, but differ on how it is done.

Again , I hope this helps...
Matt Brice
matthewbrice
 
Posts: 36
Joined: Wed Jun 13, 2012 6:42 pm

Re: Set combobox to display first item

Postby jd2p » Sun Aug 12, 2012 6:08 am

Hey Matt, the code you provided seems to be on right on track and it's what I had in mind of how to make it work. Thanks a lot!

About the side note, keep in mind that these forms are meant for the user to select conditions before displaying a report and so they are not meant to update any data, so we keep the autosave feature to false.

I'll use the code you provided and post back. Thanks again.
jd2p
 
Posts: 95
Joined: Thu Dec 08, 2011 1:08 am

Re: Set combobox to display first item

Postby jd2p » Wed Sep 19, 2012 9:38 pm

I said I would post back... so the code Matt provided worked quite well, I use a generic method that gets called when a form loads, it ended up looking like this (pretty much the same thing):
Code: Select all
                         var oForm = solutionModel.getForm(pFormName);   // pformname contains the name of current form being displayed
            var sFields = oForm.getFields();
            var sDataProvider;
            var sValuelist;
            var dsLstName;
            
            for ( var i in sFields ){
               if (sFields[i].displayType == JSField.COMBOBOX ){
                  sDataProvider = sFields[i].dataProviderID ;
                  sValuelist = sFields[i].valuelist ;
                  dsLstName = application.getValueListItems(sValuelist.name ) ;
                  forms[pFormName][sDataProvider] = dsLstName.getValue(1,2) ;
               }
            }
jd2p
 
Posts: 95
Joined: Thu Dec 08, 2011 1:08 am


Return to Forms

Who is online

Users browsing this forum: No registered users and 6 guests