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

Custom Results Filter Resets Selected Options #305

Open
cskowron opened this issue Oct 13, 2016 · 0 comments
Open

Custom Results Filter Resets Selected Options #305

cskowron opened this issue Oct 13, 2016 · 0 comments

Comments

@cskowron
Copy link

cskowron commented Oct 13, 2016

I am using GwtChosen to select from a list of ~10,000 options that are stored server-side and using a custom results filter to asynchronously load the available options. I am running into an issue where the list of selected options is getting cleared out when selecting more than one option. The following is my code for the ResultsFilter.

chosenOptions.setResultFilter(new ResultsFilter() {
    @Override
    public void filter(final String searchText, final  ChosenImpl chosen, boolean isShowing) {
         if ((searchText == null) || "".equals(searchText)) {
             return;
         }

        getUiHandlers().onGeneSearch(searchText, new AsyncCallback<GeneSearchResult>() {
            @Override
            public void onFailure(Throwable throwable) {
                // handle the error
            }
            @Override
            public void onSuccess(GeneSearchResult geneSearchResult) {
                toggleMessage(false);
                final ArrayList<GeneSearchResult.Suggestion> suggestions = geneSearchResult.getSuggestions();
                List<SelectParser.SelectItem> selectItems = chosen.getSelectItems();
                selectItems.clear();
                int arrayIndex = 0;
                for (GeneSearchResult.Suggestion geneSuggestion : suggestions) {
                    SelectParser.OptionItem optionItem = new SelectParser.OptionItem();

                    final StringBuilder sb = new StringBuilder()
                        .append(geneSuggestion.getSymbol());

                    optionItem.setHtml(sb.toString());
                    optionItem.setText(geneSuggestion.getSymbol());
                    optionItem.setValue(geneSuggestion.getSymbol());
                    optionItem.setArrayIndex(arrayIndex);
                    optionItem.setOptionsIndex(arrayIndex);
                    optionItem.setDomId("myOption_" + arrayIndex++);

                    selectItems.add(optionItem);
                }

                // Let chosen update the auto-suggest list
                chosen.rebuildResultItems();
            }
        });
    }
    });

From what I can tell, when someone selects an item, and then types something completely different and selects it, only the last select item is retained. It looks like this behavior is due to the way in which the rebuildResultItems() method clears out and then rebuilds the selectedValues array.

In my specific use case, I type something in, such as "BRCA" and after selecting the appropriate option, may type in "GALT" which will reach out to the server and return a list of options where "BRCA" does not exist. Then when the selectedValues list is rebuilt, the original "BRCA" will not be included since it is not in the newly returned list of options.

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

No branches or pull requests

1 participant