gpt4 book ai didi

GWT:如何以编程方式重新加载 CellBrowser?

转载 作者:行者123 更新时间:2023-12-02 10:22:23 25 4
gpt4 key购买 nike

我正在使用 GWT 2.1 的 CellBrowser使用自定义 TreeViewModel。 TreeViewModel 又使用 AsyncDataProvider 动态获取数据。这一切都工作得很好 - 当用户单击节点时,我的 AsyncDataProvider 通过 RPC 获取结果,并且 CellBrowser 尽职尽责地显示它们。

我因为无法弄清楚这一点而感到愚蠢,但是我如何以编程方式告诉 CellBrowser 重新加载(并显示)数据?我猜测我需要以某种方式获取根节点的 AsyncDataProvider 句柄,然后对其调用 updateRowData() 和 updateRowCount() ,但我没有看到查询浏览器(或其模型)的明显方法对于根 DataProvider。

我想我可以将代码添加到我的 AsyncDataProvider 构造函数中,以查找空参数,从而识别“嘿,我是根”并将引用存储在某处,但这看起来很黑客。当然有更好的方法来做到这一点。

很抱歉在这里转储了这么多代码,但我不知道如何将其归结为更简单的内容并仍然提供足够的上下文。

我的 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();

}
}

我的模型:

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();
}
}

最后,这是我如何使用它们:

        CategoryTreeModel model = new CategoryTreeModel(selectionModel);
CellBrowser cellBrowser = new CellBrowser(model, null);

最佳答案

看起来很多人都有这个same issue 。以下是我处理从 AsyncDataProvider 刷新数据的方法。

首先,创建一个接口(interface),通过包装 AsyncDataProvider 的 protected 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);

}

然后,需要刷新的 CellTable 可以通过 Activity 或 Controller 逻辑的设置来调用它。

/**
* 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?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4702102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com