本文介绍了GWT:如何以编程方式重新加载CellBrowser?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用的是GWT 2.1的 CellBrowser 与自定义 TreeViewModel 。 TreeViewModel反过来使用 AsyncDataProvider 来动态获取数据。这一切都很精彩 - 当用户点击一个节点时,我的AsyncDataProvider通过RPC获取结果,CellBrowser忠实地显示它们。 我觉得无法解决这个问题,但我怎样才能以编程方式告诉CellBrowser重新加载(并显示)数据?我猜我需要以某种方式获取我的根节点的AsyncDataProvider句柄,然后调用updateRowData()& updateRowCount(),但我没有看到明显的方式来查询根DataProvider的浏览器(或其模型)。 我想我可以添加代码我的AsyncDataProvider构造函数寻找一个空参数,通过这种方式意识到嘿,我是根,并在某处存储引用,但这似乎是骇人听闻。当然,还有更好的方法可以做到这一点。 在这里倾销这么多代码的道歉,但我不知道如何简化它,并且仍然提供足够的 $ b 我的AsyncDataProvider: private static class CategoryDataProvider extends AsyncDataProvider< Category> { private Category selectedCategory; private CategoryDataProvider(Category selectedCategory) { this.selectedCategory = selectedCategory; $ b @Override protected void onRangeChanged(HasData< Category> display) { new AsyncCall< List< Category>>() { @Override protected void callService(AsyncCallback< List< Category>> cb) { //默认为root String categoryId = -1\" ; if(selectedCategory!= null) { categoryId = selectedCategory.getCategoryId(); } //当一个类别被点击时,获取它的子类别 service.getCategoriesForParent(categoryId,cb); $ b @Override public void onSuccess(List< Category> result) { //更新显示 updateRowCount(result .size(),true); updateRowData(0,result); } } .go(); $ b 我的模特: private static class CategoryTreeModel实现TreeViewModel { private SingleSelectionModel< Category> selectionModel设置; public CategoryTreeModel(SingleSelectionModel< Category> selectionModel) { this.selectionModel = selectionModel; } / ** * @返回提供指定类别的子元素的NodeInfo * / public< T>的nodeinfo<?> getNodeInfo(T值) { CategoryDataProvider dataProvider = new CategoryDataProvider((Category)value); //返回一个将数据与单元格配对的节点信息。 返回新的TreeViewModel.DefaultNodeInfo< Category>(dataProvider,new CategoryCell(),selectionModel,null); $ b $ ** b $ b * @return如果指定的类别表示叶节点,则返回true * / public boolean isLeaf(Object value) {返回值!= null&& ((Category)value).isLeafCategory(); $ / code $ / pre 最后,这是我的方式使用它们: CategoryTreeModel model = new CategoryTreeModel(selectionModel); CellBrowser cellBrowser = new CellBrowser(model,null); 解决方案看起来很多人都有这个同样的问题。首先,创建一个接口,通过包装AsyncDataProvider的受保护的onRangeChanged方法来增加刷新数据提供者的能力。下面是我如何处理刷新AsyncDataProvider中的数据的方法。例如... HasRefresh.java pre $ public interface HasRefresh<< / p> ; HasData< T>> { / ** *当显示器想要刷新时调用 * * @param显示刷新 * / void refresh(HasData< T> display); } 然后,需要刷新的CellTable可以调用它通过一个Activity或者你的控制器逻辑被设置。 / ** *自定义{@link AsyncDataProvider}。 * / public class MyAsyncDataProvider extends AsyncDataProvider< Entity>实现HasRefresh< Entity> { public void refresh(实体显示){ onRangeChanged(显示); } / ** * {@link #onRangeChanged(HasData)}在表请求 *新数据范围时被调用。您可以使用 * {@link #updateRowData(int,List)}将数据推送回显示器。 * / @Override protected void onRangeChanged( HasData< Entity> display){ currentRange = display.getVisibleRange(); final int start = currentRange.getStart(); dataServiceQuery = context.getEntities(); $ b $ dataServiceQuery .execute(new DataServiceQueryHandler< Entity>(){ $ b $ @Override public void onQueryResponse( DataServiceQueryResponse< Entity>响应){ updateRowCount(response.getCount(),true); updateRowData(start,response.getEntities()); } }); } // end onRangeChanged() } //结束MyAsyncDataProvider类 I'm using GWT 2.1's CellBrowser with a custom TreeViewModel. The TreeViewModel in turn uses an AsyncDataProvider to fetch data dynamically. This all works beautifully- when the user clicks on a node my AsyncDataProvider fetches the results via RPC, and the CellBrowser dutifully displays them.I feel silly for not being able to figure this out, but how can I programmatically tell the CellBrowser to reload (and display) the data? I'm guessing that I need to somehow get a handle to the AsyncDataProvider for my root node and then call updateRowData() & updateRowCount() on it, but I don't see an obvious way to query the browser (or its model) for the root DataProvider.I guess I could add code to my AsyncDataProvider constructor that looks for a null argument, and by that means recognize "hey, I'm the root" and store a reference somewhere, but that seems hackish. Surely there's a better way to do this.Apologies for dumping so much code here, but I don't know how to boil this down to anything simpler and still provide enough context.My AsyncDataProvider:private static class CategoryDataProvider extends AsyncDataProvider<Category>{ private Category selectedCategory; private CategoryDataProvider(Category selectedCategory) { this.selectedCategory = selectedCategory; } @Override protected void onRangeChanged(HasData<Category> display) { new AsyncCall<List<Category>>() { @Override protected void callService(AsyncCallback<List<Category>> cb) { // default to root String categoryId = "-1"; if (selectedCategory != null) { categoryId = selectedCategory.getCategoryId(); } // when a category is clicked, fetch its child categories service.getCategoriesForParent(categoryId, cb); } @Override public void onSuccess(List<Category> result) { // update the display updateRowCount(result.size(), true); updateRowData(0, result); } }.go(); }}My model:private static class CategoryTreeModel implements TreeViewModel{ private SingleSelectionModel<Category> selectionModel; public CategoryTreeModel(SingleSelectionModel<Category> selectionModel) { this.selectionModel = selectionModel; } /** * @return the NodeInfo that provides the children of the specified category */ public <T> NodeInfo<?> getNodeInfo(T value) { CategoryDataProvider dataProvider = new CategoryDataProvider((Category) value); // Return a node info that pairs the data with a cell. return new TreeViewModel.DefaultNodeInfo<Category>(dataProvider, new CategoryCell(), selectionModel, null); } /** * @return true if the specified category represents a leaf node */ public boolean isLeaf(Object value) { return value != null && ((Category) value).isLeafCategory(); }}And finally, here's how I'm using them: CategoryTreeModel model = new CategoryTreeModel(selectionModel); CellBrowser cellBrowser = new CellBrowser(model, null); 解决方案 It looks like a lot of people are having this same issue. Here's how I handled refreshing the data from an AsyncDataProvider.First, create an interface that adds the ability to refresh data provider by wrapping the AsyncDataProvider's protected onRangeChanged method. For example...HasRefresh.javapublic interface HasRefresh<HasData<T>>{ /** * Called when a display wants to refresh * * @param display the display to refresh */ void refresh(HasData<T> display);}Then the CellTable needing to be refreshed can then call into it through an Activity or however your controller logic is setup. /*** A custom {@link AsyncDataProvider}.*/public class MyAsyncDataProvider extends AsyncDataProvider<Entity> implements HasRefresh<Entity> { public void refresh(Entity display){ onRangeChanged(display); } /** * {@link #onRangeChanged(HasData)} is called when the table requests a * new range of data. You can push data back to the displays using * {@link #updateRowData(int, List)}. */ @Override protected void onRangeChanged( HasData<Entity> display) { currentRange = display.getVisibleRange(); final int start = currentRange.getStart(); dataServiceQuery = context.getEntities(); dataServiceQuery .execute(new DataServiceQueryHandler<Entity>() { @Override public void onQueryResponse( DataServiceQueryResponse<Entity> response) { updateRowCount(response.getCount(), true); updateRowData(start, response.getEntities()); } }); }// end onRangeChanged() }// end MyAsyncDataProvider class 这篇关于GWT:如何以编程方式重新加载CellBrowser?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 18:02