View Full Version : Selected item in ComboBox
Hello everybody!
ComboBox<BeanModel> combo = new ComboBox<BeanModel>();
combo.setWidth(580);
combo.setDisplayField("title");
combo.setItemSelector("div.search-item");
combo.setTemplate(getTemplate());
combo.setLoadingText("Поиск");
combo.setStore(store);
combo.setHideTrigger(true);
combo.setPageSize(10);
combo.addSelectionChangedListener(new SelectionChangedListener<BeanModel>() {
@Override
public void selectionChanged(SelectionChangedEvent<BeanModel> se) {
Info.display("Info", se.getSelectedItem().toString()); //NullPointerException
}
});
How can i get a selected Bean in ComboBox(advanced) (http://www.sencha.com/examples/explorer.html#advancedcombobox)?
---
Regards from Russia, Kirill
I just added the SelectionChangedListener to that example and it works without any problems for me.
Keep in mind that your code could throw an exception if you set null as value. But when selecting something from the list, this is not the case.
Ok, thanks. But listed code:
@Override
public void selectionChanged(SelectionChangedEvent<BeanModel> se) {
BeanModel selected = se.getSelectedItem(); //first item from store...
Some selectedSome = selected.getBean();
if (selectedSome != null) {
Info.display(selectedSome.getNum(), selectedSome.getName());
}
}
returns the first item from the store, but it doesn't return selected item. Check this.
returns the first item from the store, but it doesn't return selected item. Check this. It is not doing that for me.
It is not doing that for me.
I'm using GXT-2.2.1 what version are you using?
It looks like listed code:
public class SelectionChangedEvent<M extends ModelData> extends BaseEvent {
...
/**
* Returns the first selected item.
*
* @return the selected item
*/
public M getSelectedItem() {
if (selection.size() > 0) {
return selection.get(0);
}
return null;
}
...
}
returns first element in the store and causes unexpectable widget's behavior.
That code returns the first element of the selection. As a ComboBox can only have one selected item, it retuns that item or null.
The problem is solved. I filled out the data at server incorrectly. Thank you for your participation.
Great. So this is working now as intended?
So this is working now as intended?
Yes it works.
OZKA
10 Dec 2010, 12:54 AM
I have a new question: What is the best way to remove query delay or query task? I want to run a search on button click.
upd: My version of this task is in the listed code, but i'm not sure that this solution has not some side effects.
combo = new ComboBox<BeanModel>(){
@Override
protected void onKeyUp(FieldEvent fe) {
//nothing...
}
public void expand() {
hasFocus = true;
super.expand();
}
};
combo.setWidth(200);
combo.setDisplayField("title");
combo.setItemSelector("div.search-item");
combo.setTemplate(getTemplate());
combo.setLoadingText("Поиск");
combo.setDisplayField("num");
combo.setUseQueryCache(false);
combo.setStore(store);
combo.setHideTrigger(true);
combo.setFieldLabel("Поиск");
combo.addKeyListener(new KeyListener() {
public void componentKeyPress(ComponentEvent event) {
if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
searchBtn.focus();
}
}
});
combo.addSelectionChangedListener(new SelectionChangedListener<BeanModel>() {
@Override
public void selectionChanged(SelectionChangedEvent<BeanModel> se) {
BeanModel selected = se.getSelectedItem();
applySelection(selected);
}
});
searchBtn = new Button("Найти");
searchBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
combo.doQuery(combo.getRawValue(), true);
}
});
Powered by vBulletin® Version 4.1.5 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.