前台使用EasyUI實現 . EasyUI向後台傳遞一個id參數 .
第一次加載 , 向後台傳遞的id為null .
之後每次將樹節點展開 , 會向後台傳遞一個當前節點的 id .
Control層 :
代碼如下:
/**
* tree
*/
@RequestMapping(value = "/tree.do")
public void mytree(HttpServletResponse response, String id) {
this.writeJson(response, bookService.getChildrenTree(id));
}
Service層 :
代碼如下:
@Transactional
@Override
public List<Tree> getChildrenTree(String pid) {
try {
List<Tree> result = new ArrayList<Tree>();
//獲得兒子節點的列表
List<TBookType> childrenList = this.getChildrenType(pid);
if (childrenList != null && childrenList.size() > 0) {
for (TBookType child : childrenList) {
// 獲取孫子的個數
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");
//將兒子列表childrenList數據逐個存到樹當中
result.add(node);
}
}
return result;
} catch (Exception e) {
throw new BusinessException("獲取圖書類型數據出現錯誤!", e);
}
}
Dao層 :
代碼如下:
@Override
public List<TBookType> getChildrenType(String pid) {
//這個的pid就是當前展開節點的id , 通過父節點的 id 來獲得子節點
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) {
//這個的pid就是當前展開節點的id , 通過父節點的 id 來獲得子節點的個數
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());
}
以上所述就是本文關於EasyUI實現異步樹的全部代碼了,希望對大家能有所幫助