0

我正在使用建议框在 GWT 中实现自动完成。为了从实体中检索数据,我使用了 objectify 并将数据映射到建议框,我使用了 MultiWordSuggestOracle。

在表单加载时,我正在触发一个查询以检索数据并将其传递给 MultiWordSuggestOracle。它工作正常。

例如,如果我在建议中加载客户数据,它正在工作

但是例如,如果我的实体中有 5000 - 50000 条客户记录,那么检索所有数据并在建议中显示它是不可能成功的。

那么在gwt中使用自动完成还有其他技术吗?提前致谢

4

1 回答 1

0

与其在表单加载时加载所有客户记录,不如根据用户在SuggestBox. 您可以通过实现自定义SuggestOracle(也许扩展MultiWordSuggestOracle)来做到这一点。

public class ServerSuggestOracle extends SuggestOracle{

    protected DataSource datasource;
    protected int startQueryLength;
    protected ArrayList<T> suggestions = new ArrayList<T>();
    protected boolean isMoreSuggestions = false;
    protected int previousQueryLength = 0;

    public ServerSuggestOracle(DataSource datasource,int startQueryLength) 
    {
        super();
        this.datasource = datasource;
        this.startQueryLength = startQueryLength;

    }

    @Override
    public void requestSuggestions(final Request request, final Callback callback) {
        // start the backend call only if the user types in more than startQueryLength characters.
        if (request.getQuery().length() < startQueryLength) 
            return;
        // if the user expands the search or a serach hasn't been carried out, call the backend. Otherwise filte the existing list
        if (isMoreSuggestions || previousQueryLength > request.getQuery().length() || suggestions.size() == 0) 
        {
            datasource.fetchDataFromBackend(request.getQuery(), new FetchDataFromBackendCallback() {

                @Override
                public void onFetchData(ArrayList<T> genes,Integer count,boolean isMore) {

                    suggestions.clear();
                    for (int i = 0;i<genes.size();i++) {
                        Suggestion suggestion = new Suggestion();
                        suggestions.add(suggestion);
                    }
                    SuggestOracle.Response response = new SuggestOracle.Response(suggestions);
                    isMoreSuggestions = isMore; 
                    if (count != null)
                        response.setMoreSuggestionsCount(count);
                    else
                        response.setMoreSuggestions(isMore);
                    previousQueryLength =  request.getQuery().length();
                    callback.onSuggestionsReady(request,response);
                }
            });
        }
        else
        {
            super.requestSuggestions(request,cabllack);
        }
    }

}
于 2015-05-15T06:31:25.093 回答