【问题标题】:GWT AutoComplete Or Suggest BoxGWT 自动完成或建议框
【发布时间】:2015-05-15 14:31:25
【问题描述】:

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

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

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

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

那么有没有其他技术可以在 gwt 中使用自动完成功能? 提前致谢

【问题讨论】:

    标签: java gwt suggestbox


    【解决方案1】:

    不是在加载表单时加载所有客户记录,而是根据用户在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);
            }
        }
    
    }
    

    【讨论】:

    • 感谢您的回复。
    猜你喜欢
    • 2020-05-17
    • 1970-01-01
    • 2014-08-15
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多