递归树,异步加载树

递归树,异步加载树

Jsp:

$("#maintree").ligerTree( {

        url : 'busiticket!getTree.action?' + $.param( {

           filter_EQL_parentid : '0',

           id : '0',

           root : '票务信息',[d1] 

           rooticon : '',

           iconroot : '../'

        }),

        checkbox : false,

        slide : false,

        isleaf : false,

        nodeWidth : 120,

        onBeforeExpand : onBeforeExpand,

        onExpand : onExpand,

        attribute : [ 'nodename', 'url' ],[d2] 

        onClick : function(node) {

            //$('#status').val(-1);

            // $('#company').val(-1);

           nodeID = node.data.classid;

           if (nodeID != 0) {

              gridManager.setOptions( {

                 parms : [{

                    name : 'filter_EQL_classid',

                    value : node.data.classid

                 } ]

              });

           } else {

              gridManager.setOptions( {

                 parms : [ {

                   

                 }  ]

              });

           }

           gridManager.loadData(true);

        }

      });

function onBeforeExpand(node) {

      if (node.data.children && node.data.children.length == 0) {

        manager.loadData(node.target,

              'busiticket!getTree.action?' + $.param( {

                 filter_EQL_parentid : node.data.classid

[d3]             }));

      }

   }

   function onExpand(note) {

   }

Action:

public String getTree() {

      try {

        List<PropertyFilter> filters = PropertyFilter

              .buildFromHttpRequest(Struts2Utils.getRequest());

        HttpServletRequest request = Struts2Utils.getRequest();

        String root = request.getParameter("root");

        if (root != null)

           root = new String(root.getBytes("iso8859-1"), "UTF-8");

        String rootIcon = request.getParameter("rooticon");

        List<BusiGoodsClassify> list = busiGoodsClassifyManager

              .getTicketTree(filters);

        JSONObject jo = new JSONObject();

        String treeJSON = "";

        for (BusiGoodsClassify entity : list) {

           jo.put("text", entity.getName());

           jo.put("classid", entity.getClassid());

           jo.put("pid", entity.getParentid());

           jo.put("isexpand", "false");

           if (entity.getChildcnt() > 0) {

              jo.put("isleaf", false);

           } else {

              jo.put("isleaf", true);

           }

           jo.put("children", "[]");

           treeJSON += jo.toString() + ",";

        }

        if (!treeJSON.equals(""))

           treeJSON = treeJSON.substring(0, treeJSON.lastIndexOf(","));

        if (root != null && !root.equals("")) {

           treeJSON = "[{"text":"" + root

                 + "","classid":"0","pid":"-1","children":["

                 + treeJSON + "]";

           if (rootIcon != null && !rootIcon.equals(""))

              treeJSON += ""icon":"" + rootIcon + """;

           treeJSON += "}]";

 

        } else {

           treeJSON = "[" + treeJSON + "]";

        }

        // 解决叶子节点问题,临时办法。API中有isleaf函数,没查到用法!!!

        if (treeJSON.equals("[{}]"))

           treeJSON = "[]";

        // System.out.println("treeJSON=" + treeJSON);

        // Struts2Utils.getResponse().setContentType("text/plain");

        Struts2Utils.renderText(treeJSON);

         return NONE;

      } catch (Exception ex) {

        logger.error(ex.getMessage());

      }

      return NONE;

   }

Service:

-- 商品分类表

树的表结构如下:

 

private static final String hql1 = "select r.name,r.classid,r.parentid , (select count(bc.classid) from BusiGoodsClassify bc where bc.parentid = r.classid ) childcnt from BusiGoodsClassify r where r.classid = (:parentid)  order by r.orders";

private static final String hql2 = "select r.name,r.classid,r.parentid , (select count(bc.classid) from BusiGoodsClassify bc where bc.parentid = r.classid ) childcnt from BusiGoodsClassify r where r.parentid = (:classid)  order by r.orders";

public List<BusiGoodsClassify> getTicketTree(final List<PropertyFilter> filters)

   { 

      Query query[d4]  = null;

      for(PropertyFilter filter : filters){

        String fieldname = filter.getPropertyName();

        String filedValue = filter.getMatchValue().toString();[d5] [d6] 

        String firstValue = "1000000019[d7] [d8] ";

        if (filedValue.equals("0")){

           query = busiGoodDao.getSession().createSQLQuery(hql1);[d9] 

           query.setParameter("parentid", Long.parseLong(firstValue[d10] [d11] ));

        }else{

           query = busiGoodDao.getSession().createSQLQuery(hql2);[d12] 

           query.setParameter("classid", Long.parseLong(filedValue[d13] [d14] ));

        }

      }

     

      List<BusiGoodsClassify> result = new ArrayList<BusiGoodsClassify>();

      List tmpList = query.list();[d15] 

      for (Object tmp : tmpList) {

        Object[] obj = (Object[]) tmp;

        BusiGoodsClassify classify = new BusiGoodsClassify();

        classify.setName(obj[0].toString());

        classify.setClassid(StringValueUtils.getLong(obj[1].toString()));

        classify.setParentid(StringValueUtils.getLong(obj[2].toString()));

         classify.setChildcnt(StringValueUtils.getInt(obj[3].toString[d16] ()));

        result.add(classify);

      }

      return result;

   }

Dao:

 


 [d1]最原始的根节点

 [d2]获取行数据时很有作用

 [d3]把子节点又作为孙节点的父节点

 [d4]申明一个Query变量

 [d5]

 [d6]在这里获取选中的子节点的值,action里并没有做任何操作

 [d7]

 [d8]

 [d9]递归写sql语句

 [d10]

 [d11]如果是根节点就传根节点参数。是个定值

 [d12]

 [d13]

 [d14]如果是某个子节点,就传可变的一个值,作为孙节点的父节点

 [d15]

 [d16]拼一课新的树

原文地址:https://www.cnblogs.com/pujiajia/p/3287342.html