gwtchosen
gwtchosen copied to clipboard
when server-side filtering, selecting always takes the first item !
I have this server-side filter setup :
public class ServerSideResultFilter implements ResultsFilter {
public ServerSideResultFilter() {
}
@Override
public void filter(final String searchText, final ChosenImpl chosen,
boolean isShowing) {
if (!isShowing) {
return;
}
ContactsSearchCriteria criteria = new ContactsSearchCriteria();
criteria.setSearchString(searchText);
criteria.setOrderBy(Contact.ORDER_FULLNAME_DOWN);
RestClientFactory.getContactClient().search(criteria,
new Result<SearchResult<Contact>>() {
@Override
public void onFailure(Throwable caught) {
GWT.log("error querying for contacts : "
+ caught.getMessage());
}
@Override
public void onSuccess(SearchResult<Contact> result) {
final List<SelectItem> selectItems = chosen
.getSelectItems();
selectItems.clear();
int index = 0;
for (Contact contact : result.getList()) {
OptionItem optionItem = new OptionItem();
optionItem.setText(new ContactNameDecorator(
I18nHelper.getI18nConstants())
.decorate(contact));
optionItem.setValue(contact.getId().toString());
optionItem.setOptionsIndex(index);
selectItems.add(optionItem);
}
chosen.rebuildResultItems();
chozen.update(); /// CRITICAL ! This is the widget
}
});
}
}
This is almost a copy of the sample code. But notice the call to update that has been added near the end of the callback implementation.
- Now without the call chozen.update(), the id of the container of each select item is set to "chozen_container__0_chzn_o_0". This results in always the first item being selected (index=0!)
- When the call to chozen.update() is made, the selected items have the right index in the id of their container : "chozen_container__0_chzn_o_0", "chozen_container__0_chzn_o_1", ""chozen_container__0_chzn_o_2" for the subsequent elements.
So either the documentation is wrong and this call must be added there (and save somebody else's day) or this is unexpected behaviour ? I would expect that the call to chosen.rebuildResultItems
would take proper care of initializing the id's for the new items.
You can fix this isue by setting ids. You need to start with the id 0, otherwise he will take the next item. Example: optionItem.setArrayIndex(i);
I have this server-side filter setup :
public class ServerSideResultFilter implements ResultsFilter { public ServerSideResultFilter() { } @Override public void filter(final String searchText, final ChosenImpl chosen, boolean isShowing) { if (!isShowing) { return; } ContactsSearchCriteria criteria = new ContactsSearchCriteria(); criteria.setSearchString(searchText); criteria.setOrderBy(Contact.ORDER_FULLNAME_DOWN); RestClientFactory.getContactClient().search(criteria, new Result<SearchResult<Contact>>() { @Override public void onFailure(Throwable caught) { GWT.log("error querying for contacts : " + caught.getMessage()); } @Override public void onSuccess(SearchResult<Contact> result) { final List<SelectItem> selectItems = chosen .getSelectItems(); selectItems.clear(); int index = 0; for (Contact contact : result.getList()) { OptionItem optionItem = new OptionItem(); optionItem.setText(new ContactNameDecorator( I18nHelper.getI18nConstants()) .decorate(contact)); optionItem.setValue(contact.getId().toString()); optionItem.setOptionsIndex(index); selectItems.add(optionItem); } chosen.rebuildResultItems(); chozen.update(); /// CRITICAL ! This is the widget } }); } }
This is almost a copy of the sample code. But notice the call to update that has been added near the end of the callback implementation.
- Now without the call chozen.update(), the id of the container of each select item is set to "chozen_container__0_chzn_o_0". This results in always the first item being selected (index=0!)
- When the call to chozen.update() is made, the selected items have the right index in the id of their container : "chozen_container__0_chzn_o_0", "chozen_container__0_chzn_o_1", ""chozen_container__0_chzn_o_2" for the subsequent elements.
So either the documentation is wrong and this call must be added there (and save somebody else's day) or this is unexpected behaviour ? I would expect that the call to
chosen.rebuildResultItems
would take proper care of initializing the id's for the new items.