ztree插件的使用

            //查询已存的分类值
            List<Integer> cateids = storeBrandCategoryService.queryStoreCateIds(storeId);
            //查询分类列表
            List<Category> cateList = categoryService.queryTopCategory(cate_type);
            cateLoop(cateList,cateids);
            //转成josn串
            JSONArray arr = JSONArray.fromObject(cateList);
            String json = arr.toString();
            json = json.replaceAll("cate_id", "id").replaceAll("cate_name", "name")
                .replaceAll("subCategory", "nodes").replaceAll("hasCate", "checked");
            mv.addObject("zTreeNodes", json);

            private void cateLoop(List<Category> cateList,List<Integer> cateids){
                if(cateList != null && cateList.size() > 0){
                    for(Category cate : cateList){
                        List<Category> subCate = categoryService.querysubCategory(cate);
                        cate.setSubCategory(subCate);
                        cate.setHasCate(cateids.contains(cate.getCate_id()));
                        cateLoop(subCate,cateids);
                    }
                }
            }

jsp:
<script type="text/javascript" src="../js/zTree/jquery.ztree-2.6.min.js"></script>

            <div class="content tc">
                    <ul id="tree" class="tree" style="overflow:auto;"></ul>
            </div>
js:
var zTree;
$(document).ready(function(){
        var setting = {
                        showLine: true,
                        checkable: true
                    };
        var zTreeNodes = eval('${zTreeNodes}');
        zTree = $("#tree").zTree(setting, zTreeNodes);
    });

//取值:仅取叶子节点
        var nodes= zTree.getCheckedNodes(true);
        var tmpNode;
        var ids = "";
        for(var i=0; i<nodes.length; i++){
            tmpNode = nodes[i];
            if(!tmpNode.isParent){
                if(i!=nodes.length-1){
                    ids += tmpNode.id+",";
                }else{
                    ids += tmpNode.id;
                }
            }
        }


你可能感兴趣的:(ztree插件的使用)