一:遞歸樹,異步加載樹

遞歸樹,異步加載樹

效果圖如下:

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);

         returnNONE;

      } catch (Exception ex) {

        logger.error(ex.getMessage());

      }

      returnNONE;

   }

Service

-- 商品分類表

樹的表結構如下:

privatestaticfinal 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";

privatestaticfinal 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]拼一課新的樹

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章