JAVA 操作树返回JSON数据格式

//获取所有显示的菜单

public List<Menu> getAllShowMenu(){
  StringBuffer hql = new StringBuffer();
  hql.append("from Menu m ");
  hql.append("where m.menuIsShow=? ");
  hql.append("order by m.menuIndex");
  Query query = getSession().createQuery(hql.toString()).setParameter(0, Menu.MENU_IS_SHOW_TRUE);
  return query.list();
 }

//获取所有显示的菜单

public String getAllShowMenuToTree(){
  List<Menu> menuList = menuDao.getAllShowMenu();
  StringBuffer html = new StringBuffer();
  html.append("[");
  for (Menu menu : menuList) {
   String menu_p_id = menu.getParent()==null?null:menu.getParent().getMenuId();
   html.append("{id:\""+menu.getMenuId()+"\",pid:\""+StringUtils.defaultString(menu_p_id)+"\",name:\""+StringUtils.defaultString(menu.getMenuName())+"\"},");
  }
  if(html.toString().lastIndexOf(",") > -1){
   html.replace(html.toString().lastIndexOf(","), html.toString().lastIndexOf(",")+1, "");
  }
  html.append("]");
  return html.toString();
 }

//action call

List<SystemDictCategory> sdcList = systemDictCategoryService.getAll();
  sdcTreeData = wrapDictCategoryTreeList(sdcList);
  return "tree";

//返回代码表树

 public String wrapDictCategoryTreeList(List<SystemDictCategory> sdcList){
  List<Map<String,String>> treeList = new ArrayList<Map<String,String>>();
  for(SystemDictCategory sdc : sdcList){
   Map<String,String> tree = new HashMap<String,String>();
   tree.put("id",sdc.getDictCodeCategoryId());
   tree.put("name",sdc.getDictCodeCategoryName());
   if(sdc.getParent()!=null){
    tree.put("pid",sdc.getParent().getDictCodeCategoryId());
   }else{
    tree.put("pid","0"); //最顶层节点父节点ID为0
   }
   treeList.add(tree);
  }
  JSONArray json = JSONArray.fromObject(treeList);
  return json.toString();
 }

前台main.ft

l <frameset  cols= "170,*"  frameborder= "1"  framespacing= "5" bordercolor= "#CADAE8"  >
 <frame src="${base}/sys/code/system_dict!tree.action"  id="lfrm" name="lfrm" scrolling= "no" frameborder= "no" bordercolor= "#CADAE8">
 <frame src="" id="rfrm" name="rfrm"  frameborder= "5"  bordercolor= "#CADAE8">
</frameset>

tree.ftl

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>代码表类别树</title>
  <#include "/WEB-INF/template/common/tree_include.ftl">
  <script type="text/javascript">
   function zTreeOnClick(event,treeId,treeNode){
    var url = "system_dict!list.action?id="+treeNode.id;
    parent.rfrm.location.href=url;
   }
  </script>
 </head>
 <body>
  <@statictree id="sdc" treeData=sdcTreeData checkable='false' callback='click:zTreeOnClick' />
 </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值