-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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)
Am I too wrong for wanting to be able to select an item by value? is it possible without the fullscan?
thank you guys!
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.
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.
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!!!
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
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
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.
Hi Any help on the same solution on MVVM pattern ?
it would be great if there was a solution on MVVM pattern! Any idea?
Here is MVVM Example
Asked: 2011-03-04 16:59:37 +0800
Seen: 3,794 times
Last updated: Jan 16 '14