Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when server-side filtering, selecting always takes the first item ! #227

Open
komasoftware opened this issue May 25, 2015 · 1 comment
Open

Comments

@komasoftware
Copy link

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.

@christiangoudreau christiangoudreau added this to the 2.2.0 milestone May 26, 2015
@olafleur olafleur added the bug label Sep 14, 2015
@olafleur olafleur removed this from the 2.2.0 milestone Sep 14, 2015
@olafleur olafleur added documentation and removed bug labels Jan 4, 2016
@anaether
Copy link

anaether commented Apr 27, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants