Just like the caption saying, I can’t find a way to add one node and remove the node without loading the whole tree data when I use the dataprovider of backend.
the following is my data provider, it will try to load data from DB.
When use add/remove one new node, I want to add/remove only the node newly added/removed, could I do that?
The reason why I want this is because every I load the data into the treegrid, it will chagne the collapse/expan state of the whole notes, if I remember all the states of the tree, it is really in a low performance.
I try to expand/collapse the code, it doesn’t work. I try to getProvider().refreshItem(), it also doesn’t work and won’t call fetch children at all.
Hope someone could give me some idea of how to resolve this.
public class ProductConfigDataProvider extends AbstractHierarchicalDataProvider<ProductConfigurationVO, SerializablePredicate<ProductConfigurationVO>> {
/**
*
*/
private static final long serialVersionUID = 1845894365954760179L;
private TreeData<ProductConfigurationVO> rootNode;
private List<ProductConfigurationVO> childs = new ArrayList<>();
public ProductConfigDataProvider(TreeData<ProductConfigurationVO> treeData) {
super();
rootNode = treeData;
}
@Override
public int getChildCount(HierarchicalQuery<ProductConfigurationVO, SerializablePredicate<ProductConfigurationVO>> query) {
childs.clear();
if (query.getParent() == null) {
childs.addAll(rootNode.getRootItems());
return (int) rootNode.getRootItems().size();
}
Optional<ProductConfigurationVO> parentOptional = query.getParentOptional();
if (parentOptional.isPresent()) {
ProductConfigurationVO productConfigurationVO = parentOptional.get();
if (productConfigurationVO.getProductConfig() != null) {
IProductConfigurationService productConfigService =
BeanManager.getService(IProductConfigurationService.class);
List<ProductConfiguration> productConfigurations =
productConfigService.listChildProductConfig(productConfigurationVO.getProductConfig().getId());
for (ProductConfiguration productConfiguration : productConfigurations) {
childs.add(new ProductConfigurationVO(productConfiguration));
}
IProductConfigurationRouteService service =
BeanManager.getService(IProductConfigurationRouteService.class);
List<ProductConfigurationRoute> productConfigurationRoutes =
service.listByProductConfig(productConfigurationVO.getProductConfig().getId());
for (ProductConfigurationRoute productConfigurationRoute : productConfigurationRoutes) {
childs.add(new ProductConfigurationVO(productConfigurationRoute));
}
}
}
return childs.size();
}
@Override
public Stream<ProductConfigurationVO>
fetchChildren(HierarchicalQuery<ProductConfigurationVO, SerializablePredicate<ProductConfigurationVO>> query) {
return childs.stream();
}
@Override
public boolean hasChildren(ProductConfigurationVO item) {
if (item != null) {
if (item.getProductConfig() != null) {
IProductConfigurationService productConfigService =
BeanManager.getService(IProductConfigurationService.class);
List<ProductConfiguration> productConfigurations =
productConfigService.listChildProductConfig(item.getProductConfig().getId());
IProductConfigurationRouteService service = BeanManager.getService(IProductConfigurationRouteService.class);
List<ProductConfigurationRoute> productConfigurationRoutes =
service.listByProductConfig(item.getProductConfig().getId());
return !productConfigurations.isEmpty() || !productConfigurationRoutes.isEmpty();
} else {
//route 下面不能再加节点
return false;
}
}
return !rootNode.getRootItems().isEmpty();
}
@Override
public boolean isInMemory() {
return false;
}