前台使用EasyUI實作 . EasyUI向後台傳遞一個id參數 .
第一次載入 , 向後台傳遞的id為null .
之後每次將樹節點展開 , 會向後台傳遞一個當前節點的 id .
Control層 :
/**
* 樹
*/
@RequestMapping(value = "/tree.do")
public void mytree(HttpServletResponse response, String id) {
this.writeJson(response, bookService.getChildrenTree(id));
}
Service層 :
@Transactional
@Override
public List getChildrenTree(String pid) {
try {
List result = new ArrayList();
//取得兒子節點的清單
List 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 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實作非同步樹的全部程式碼了,希望對大家能有所幫助