Skip to content

Custom Results Filter Resets Selected Options #305

@cskowron

Description

@cskowron

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions