前面我們分享了使用jquery中EasyUI實(shí)現(xiàn)同步樹的代碼,本文我們就來看下使用EasyUI實(shí)現(xiàn)異步樹的方法和示例,希望小伙伴們能夠喜歡。
前臺(tái)使用EasyUI實(shí)現(xiàn) . EasyUI向后臺(tái)傳遞一個(gè)id參數(shù) .
第一次加載 , 向后臺(tái)傳遞的id為null .
之后每次將樹節(jié)點(diǎn)展開 , 會(huì)向后臺(tái)傳遞一個(gè)當(dāng)前節(jié)點(diǎn)的 id .
Control層 :
復(fù)制代碼代碼如下:
/**
* tree
*/
@RequestMapping(value = "/tree.do")
public void mytree(HttpServletResponse response, String id) {
this.writeJson(response, bookService.getChildrenTree(id));
}
Service層 :
復(fù)制代碼代碼如下:
@Transactional
@Override
public List<Tree> getChildrenTree(String pid) {
try {
List<Tree> result = new ArrayList<Tree>();
//獲得兒子節(jié)點(diǎn)的列表
List<TBookType> childrenList = this.getChildrenType(pid);
if (childrenList != null && childrenList.size() > 0) {
for (TBookType child : childrenList) {
// 獲取孫子的個(gè)數(shù)
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數(shù)據(jù)逐個(gè)存到樹當(dāng)中
result.add(node);
}
}
return result;
} catch (Exception e) {
throw new BusinessException("獲取圖書類型數(shù)據(jù)出現(xiàn)錯(cuò)誤!", e);
}
}
Dao層 :
復(fù)制代碼代碼如下:
@Override
public List<TBookType> getChildrenType(String pid) {
//這個(gè)的pid就是當(dāng)前展開節(jié)點(diǎn)的id , 通過父節(jié)點(diǎn)的 id 來獲得子節(jié)點(diǎn)
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());
}
復(fù)制代碼代碼如下:
@Override
public long getChildrenCount(String pid) {
//這個(gè)的pid就是當(dāng)前展開節(jié)點(diǎn)的id , 通過父節(jié)點(diǎn)的 id 來獲得子節(jié)點(diǎn)的個(gè)數(shù)
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());
}
以上所述就是本文關(guān)于EasyUI實(shí)現(xiàn)異步樹的全部代碼了,希望對(duì)大家能有所幫助