1

set Combobox selected Item by Value

asked 2011-03-04 16:59:37 +0800

willisra gravatar image willisra
58 1 3

Hi,

I'm trying to programmatically select an item from a combobox but I only know the value (I have no way to know the index). I think it is a very common functionality for any combobox but I can't find a clean and natural way to do it.

I don't want to make a fullscan over the combo's items to find the one I want to select and then use the:

combobox.setSelectedItem(Comboitem)


method.

Am I too wrong for wanting to be able to select an item by value? is it possible without the fullscan?

thank you guys!

delete flag offensive retag edit

11 Replies

Sort by ยป oldest newest

answered 2011-03-04 17:43:18 +0800

xcom gravatar image xcom
564 3

updated 2011-03-04 17:44:39 +0800

maybe this can help, maybe

combobox

<combobox inplace="true" id="cbox" autocomplete="true"
			selectedItem="@{control.myObject}" autodrop="true"
			model="@{control.modelo">
		


list --->List<MyObject>

in control zul


private Combobox cbox;//autowire
private ListModelList modelo;
private MyObject myObject;

@Override
	public void afterCompose() {
		super.afterCompose();
                comp.setAttribute("control", this, false);
		comp.setAttribute("binder", true);
                cp = (Controlador) SpringUtil
				.getBean("controlPersistencia");
                 List<youObject> list=cp.getLista()
                 modelol = new ListModelList(list);
		 cbox.setModel(modelo);
                                 
                 cbox.addEventListener("onSelect",
				new EventListener() {

					@Override
					public void onEvent(Event event) throws Exception {
						alert(cbox.getSelectedItem().getValue().toString());
                                                alert(String.value.of(cbox.getModel().indexOf(youObject)));

					}
				});
                //binder
                  binder = new AnnotateDataBinder(comp);
		// cargos todos los atributos para este binder
		binder.loadAll(

          


	}

        //get y set form model and you object


selectedItem="@{control.myObject}" ----->is you object and you object have index you listModel ok..

ListModelList lista=cbox.getModel();

lista you model have many index from n-1 item element.

link publish delete flag offensive edit

answered 2011-03-05 22:00:37 +0800

xcom gravatar image xcom
564 3

rembember in seleted object you code

-......
event seleted you combobox --------------------------------->setMyObject(object)

becasue, you call other function in you code not have nulll example

For example:

event seleted you combobox----------------------------------------------> call you function (youObject)------>is ok.

but, you call other function ---------------------------------------->otherFunction{ //here is code ........youObject is NULLLLLLLLLLLLLL}

SOLVED:

EVENT SELETED YOU Combobox------------------------------------->eventseleted--------------->setMyObject(object)


you call other function ---------------------------------------->otherFunction{ //here is code ........getMyObject() ok ist no null}

ok.

link publish delete flag offensive edit

answered 2011-03-07 11:28:42 +0800

willisra gravatar image willisra
58 1 3

Thanx xcom, I think I didn't explain myself very well though.

I have a grid, and when the user clicks the "edit" button then I want to place the data of that particular record into input fields (texboxes, dateboxes, comboboxes, etc) so the user can edit anything on the texboxes and select any other item from the combos and then save (update) the record. I have no problem bringing the corresponding data to the textboxes but I can't make the combobox to show the corresponding item passing the "value" that comes from the record since the API only provides a method to select an item by index (which is useless to me)

so... I'm having to iterate on the combobox items till I locate the one that has the value and then pass that Comboitem to the combobox.setSelectedItem() method so it shows that item as selected.

I hope this is clearer now.
thank you!!!

link publish delete flag offensive edit

answered 2011-03-07 19:38:56 +0800

xcom gravatar image xcom
564 3

<combobox id="youCombo"
value="@{Controller.youObject.youVariable,save-when='btn_salve.onClick'}"
											model="@{Controller.youModel}" />

link publish delete flag offensive edit

answered 2012-11-22 12:59:56 +0800

selement gravatar image selement
12

Hi willisra,

I am trying to do the exact thing you have described and am failing. Would you mind posting all your code please. Really appreciated!

or email to [email protected]

Thanks

link publish delete flag offensive edit

answered 2012-11-24 17:29:45 +0800

willisra gravatar image willisra
58 1 3

I had to create this method... and I use it every time I need to select a como item by name... I know, Iknow, it's sad and dirty but... I had no choice... :)

	public static Comboitem getComboItem(Combobox combo, String value){
		
		Comboitem item = null;

		for(int i = 0; i < combo.getItems().size(); i++){
			if(combo.getItems().get(i) != null){
				item = (Comboitem)combo.getItems().get(i);
				if(value.equals(item.getValue().toString())){
					break;
				}
			}
		}
		return item;
	
	}


Then I just pass the resulting ComboItem to the Combobox.setSelectedItem()

Hope this helps

link publish delete flag offensive edit

answered 2012-12-10 13:17:21 +0800

pscheipl gravatar image pscheipl
3

Hi,

I'm not sure if this is still an issue, but I was working on the same problem and solved this a little "cleaner". Here's what I did:

public class ComboboxHelper<T> {

	public Comboitem getItemByValue(Combobox box, T value) throws IllegalArgumentException{
		
		for(Comboitem item : box.getItems()){
			if(item.getValue().equals(value))
				return item;
		}
		
		throw new IllegalArgumentException(value+" wasn't found in Combobox store");
	}
}

Maybe this helps.

link publish delete flag offensive edit

answered 2013-10-28 12:42:48 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Hi Any help on the same solution on MVVM pattern ?

link publish delete flag offensive edit

answered 2014-01-16 09:46:19 +0800

Tristan2583 gravatar image Tristan2583
13 3

it would be great if there was a solution on MVVM pattern! Any idea?

link publish delete flag offensive edit

answered 2014-01-16 12:34:58 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Here is MVVM Example

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2011-03-04 16:59:37 +0800

Seen: 3,794 times

Last updated: Jan 16 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More