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