Tôi đang sử dụng CellBrowser của GWT 2.1 với tùy chỉnh TreeViewModel
. TreeViewModel lần lượt sử dụng AsyncDataProvider
để tìm nạp dữ liệu động. Điều này tất cả hoạt động rất đẹp - khi người dùng nhấp vào một nút mà AsyncDataProvider của tôi tìm nạp các kết quả thông qua RPC và trình duyệt CellBrowser hiển thị chúng một cách thận trọng.GWT: Làm thế nào để tải lại CellBrowser theo chương trình?
Tôi cảm thấy ngớ ngẩn vì không thể hình dung ra điều này, nhưng làm cách nào tôi có thể nói cho trình duyệt CellBrowser lập trình để tải lại (và hiển thị) dữ liệu? Tôi đoán rằng tôi cần bằng cách nào đó có được một xử lý cho AsyncDataProvider cho nút gốc của tôi và sau đó gọi updateRowData() & updateRowCount() trên đó, nhưng tôi không thấy một cách rõ ràng để truy vấn trình duyệt (hoặc mô hình của nó) cho DataProvider gốc.
Tôi đoán tôi có thể thêm mã vào hàm tạo AsyncDataProvider tìm kiếm đối số rỗng và điều đó có nghĩa là nhận ra "hey, tôi là root" và lưu trữ một tham chiếu ở đâu đó, nhưng điều đó có vẻ bị tấn công. Chắc chắn có một cách tốt hơn để làm điều này.
Xin lỗi vì đã đổ quá nhiều mã vào đây, nhưng tôi không biết cách đun sôi điều này xuống bất kỳ thứ gì đơn giản và vẫn cung cấp đủ ngữ cảnh.
AsyncDataProvider của tôi:
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();
}
}
mô hình của tôi:
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();
}
}
Và cuối cùng, đây là cách tôi đang sử dụng chúng:
CategoryTreeModel model = new CategoryTreeModel(selectionModel);
CellBrowser cellBrowser = new CellBrowser(model, null);
lỗi đánh máy trên "HashRefresh" nhưng nếu không +1! :) – HaveAGuess
Tại sao mọi người bỏ phiếu cho câu trả lời này? Cái dưới đây là tốt hơn nhiều - một thay đổi dòng đơn mà làm tất cả những điều này. –
updateRowData (start, response.getEntities()) cập nhật tất cả các hiển thị cho nhà cung cấp, bạn có thể sử dụng updateRowData (display, start, response.getEntities()) để thay thế. Nhưng display.setVisibleRangeAndClearData (display.getVisibleRange(), true) vẫn tốt hơn :) – Quiz