PDA

View Full Version : Selected item in ComboBox



OZKA
9 Dec 2010, 2:24 AM
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

sven
9 Dec 2010, 4:20 AM
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.

OZKA
9 Dec 2010, 4:44 AM
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.

sven
9 Dec 2010, 4:48 AM
returns the first item from the store, but it doesn't return selected item. Check this. It is not doing that for me.

OZKA
9 Dec 2010, 5:00 AM
It is not doing that for me.

I'm using GXT-2.2.1 what version are you using?

sven
9 Dec 2010, 5:01 AM
I am also using 2.2.1

OZKA
9 Dec 2010, 5:19 AM
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.

sven
9 Dec 2010, 5:23 AM
That code returns the first element of the selection. As a ComboBox can only have one selected item, it retuns that item or null.

OZKA
9 Dec 2010, 5:46 AM
The problem is solved. I filled out the data at server incorrectly. Thank you for your participation.

sven
9 Dec 2010, 6:12 AM
Great. So this is working now as intended?

OZKA
9 Dec 2010, 6:44 AM
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);
}
});