GWT自动完成或建议框

时间:2015-05-14 10:35:12

标签: java gwt suggestbox

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

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

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

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

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

1 个答案:

答案 0 :(得分: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);
        }
    }

}