Dorado 9 : 预处理树节点(sample-center)

观察DataTree下BindingConfig节点的属性:

其中红框标出的属性都带有"Property"后缀,这表示对应的属性的值可以直接通过所绑定的实体对象的属性中直接获取,如实体对象中有一个tip属性,这个时候我们设置BindingConfig的tipProperty值为tip,则最终树节点的tip属性就会自动的采用所绑定实体对象的tip属性。

根据上面这个机制,我们同样可以设置children,label,checked,exapndedIcon,hasChild, icon等其它的属性。

如果一个实体对象本身就具有这些属性,那么非常简单,只要在BindingConfig配置好"xxxProperty"属性的值就可以了。但是如果实体对象本身没有这些属性,那么我们可以通过实体对象的虚拟属性技术解决这个问题,就如同本例一样,我们可以在DataProvider对应的代码中通过虚拟属性处理机制添加对应的虚拟属性,并完成赋值的工作:

@DataProvider
public Collection<ExampleCategory> getCategories(Long parentCategoryId)
		throws Exception {
	List<Object[]> rows = exampleCategoryDao
			.find("select c, "
					+ "(select count(*) from ExampleCategory child where child.categoryId = c.id) as childNum "
					+ "from ExampleCategory c " + "where c.categoryId = "
					+ parentCategoryId + " order by c.sortFlag");

	int i = 0;
	List<ExampleCategory> categories = new ArrayList<ExampleCategory>();
	for (Object[] row : rows) {
		ExampleCategory category = EntityUtils.toEntity(row[0]);
		EntityUtils.setValue(category, "hasChild", true/false);
		EntityUtils.setValue(category, "checked", true/false);
		categories.add(category);
	}
	return categories;
}

上面的代码给category对象注入了两个虚拟属性hasChild和checked。之后BindingConfig就可以这么设置了:

这样我们最终看到的页面就如下的效果:

可以看到有的节点没有叶子节点,而有的有,有的节点默认就处于选中状态,而有的节点默认不处于选中状态。