The frontend is implemented using EasyUI. EasyUI passes an id parameter to the backend.
When loading for the first time, the id passed to the background is null.
Every time the tree node is expanded, the id of the current node will be passed to the background.
Control layer:
/**
* tree
*/
@RequestMapping(value = "/tree.do")
public void mytree(HttpServletResponse response, String id) {
This.writeJson(response, bookService.getChildrenTree(id));
}
Service layer:
@Transactional
@Override
public List getChildrenTree(String pid) {
try {
List result = new ArrayList();
//Get the list of child nodes
List childrenList = this.getChildrenType(pid);
if (childrenList != null && childrenList.size() > 0) {
for (TBookType child : childrenList) {
// Get the number of grandchildren
Long count = bookDao.getChildrenCount(String.valueOf(child.getId()));
Tree node = new Tree();
Node.setId(String.valueOf(child.getId()));
Node.setPid(String.valueOf(child.getPid()));
Node.setText(child.getName());
Node.setChildren(null);
node.setState(count > 0 ? "closed" : "open");
//Save the childrenList data into the tree one by one
result.add(node);
}
}
Return result;
} catch (Exception e) {
throw new BusinessException("An error occurred while obtaining book type data!", e);
}
}
Dao layer:
@Override
public List getChildrenType(String pid) {
//The pid of this is the id of the currently expanded node, and the child node is obtained through the id of the parent node
StringBuilder sqlstr = new StringBuilder();
if (StringUtils.isBlank(pid))
sqlstr.append("select * from booktype bt where bt.pid=0");
else
sqlstr.append("select * from booktype bt where bt.pid=" pid );
Return this.search2(TBookType.class, sqlstr.toString());
}
@Override
public long getChildrenCount(String pid) {
//The pid of this is the id of the currently expanded node, and the number of child nodes is obtained through the id of the parent node
StringBuilder sqlstr = new StringBuilder();
if (StringUtils.isBlank(pid))
sqlstr.append("select count(*) from booktype tb where tb.pid='0'");
else
sqlstr.append("select count(*) from booktype tb where tb.pid='" pid "'");
Return this.count(sqlstr.toString());
}
The above is all the code of this article about EasyUI implementing asynchronous trees. I hope it will be helpful to everyone