jquery中EasyUI实现异步树
前台使用EasyUI实现.EasyUI向后台传递一个id参数.
第一次加载,向后台传递的id为null.
之后每次将树节点展开,会向后台传递一个当前节点的id.
Control层:
/** *tree */ @RequestMapping(value="/tree.do") publicvoidmytree(HttpServletResponseresponse,Stringid){ this.writeJson(response,bookService.getChildrenTree(id)); }
Service层:
@Transactional @Override publicList<Tree>getChildrenTree(Stringpid){ try{ List<Tree>result=newArrayList<Tree>(); //获得儿子节点的列表 List<TBookType>childrenList=this.getChildrenType(pid); if(childrenList!=null&&childrenList.size()>0){ for(TBookTypechild:childrenList){ //获取孙子的个数 longcount=bookDao.getChildrenCount(String.valueOf(child.getId())); Treenode=newTree(); 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); } } returnresult; }catch(Exceptione){ thrownewBusinessException("获取图书类型数据出现错误!",e); } }
Dao层:
@Override publicList<TBookType>getChildrenType(Stringpid){ //这个的pid就是当前展开节点的id,通过父节点的id来获得子节点 StringBuildersqlstr=newStringBuilder(); if(StringUtils.isBlank(pid)) sqlstr.append("select*frombooktypebtwherebt.pid=0"); else sqlstr.append("select*frombooktypebtwherebt.pid="+pid); returnthis.search2(TBookType.class,sqlstr.toString()); }
@Override publiclonggetChildrenCount(Stringpid){ //这个的pid就是当前展开节点的id,通过父节点的id来获得子节点的个数 StringBuildersqlstr=newStringBuilder(); if(StringUtils.isBlank(pid)) sqlstr.append("selectcount(*)frombooktypetbwheretb.pid='0'"); else sqlstr.append("selectcount(*)frombooktypetbwheretb.pid='"+pid+"'"); returnthis.count(sqlstr.toString()); }
以上所述就是本文关于EasyUI实现异步树的全部代码了,希望对大家能有所帮助