我正在使用GWT 2.1的CellBrowser和自定义TreeViewModel.TreeViewModel又使用a AsyncDataProvider来动态获取数据.这一切都很好用 - 当用户点击节点时,我的AsyncDataProvider通过RPC获取结果,并且CellBrowser尽职尽责地显示它们.
我觉得无法解决这个问题很愚蠢,但我怎么能以编程方式告诉CellBrowser重新加载(和显示)数据呢?我猜我需要以某种方式为我的根节点获取AsyncDataProvider的句柄,然后在其上调用updateRowData()和updateRowCount(),但我没有看到查询浏览器(或其模型)的明显方法对于根DataProvider.
我想我可以在我的AsyncDataProvider构造函数中添加代码来查找null参数,并通过这种方式识别"嘿,我是根"并在某处存储引用,但这似乎是hackish.当然有更好的方法来做到这一点.
在这里抛弃这么多代码的道歉,但我不知道如何将其简化为更简单的东西并仍然提供足够的上下文.
我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的模特:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
最后,这是我如何使用它们:
CategoryTreeModel model = new CategoryTreeModel(selectionModel);
CellBrowser cellBrowser = new CellBrowser(model, null);
Run Code Online (Sandbox Code Playgroud)
aff*_*oke 10
看起来很多人都有同样的问题.以下是我处理从AsyncDataProvider刷新数据的方法.
首先,创建一个接口,通过包装AsyncDataProvider的受保护onRangeChanged方法来添加刷新数据提供程序的功能.例如...
HasRefresh.java
public interface HasRefresh<HasData<T>>{
/**
* Called when a display wants to refresh
*
* @param display the display to refresh
*/
void refresh(HasData<T> display);
}
Run Code Online (Sandbox Code Playgroud)
然后需要刷新的CellTable然后可以通过Activity调用它,或者你的控制器逻辑被设置.
/**
* 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
Run Code Online (Sandbox Code Playgroud)
小智 7
现在发现,以下代码基本相同,但是来自HasData接口,而不是使用AsyncDataProvider.
HasData<T> display;
...
display.setVisibleRangeAndClearData(display.getVisibleRange(), true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6640 次 |
| 最近记录: |