dndTree.js的树视图动态增加节点

来源:互联网 发布:游戏存档软件 编辑:程序博客网 时间:2024/05/16 08:11
用dndTree.js结合d3.js绘制如下图,通过点击节点动态绘制,增加相关的节点。
动态获取json数据,绘制代码: 
//得到树图json的数据
function get_json(json)
{
treeJSON = d3.json(json, function(error, treeData){
    $("#tree-container").empty();
    root = treeData;
    global_core_config.query = root.w;
    viewerWidth = $(document).width();
    viewerHeight = $(document).height();
    tree = d3.layout.tree()
       .size([viewerHeight, viewerWidth]);
    diagonal = d3.svg.diagonal()
       .projection(function (d) {
           return [d.y, d.x];
       });

    visit(treeData, function (d) {
        totalNodes++;
        if (d.w != undefined)
            maxLabelLength = Math.max(d.w.length, maxLabelLength);
    }, function (d) {
        return d.children && d.children.length > 0 ? d.children : null;
    });

    //sortTree();
    zoomListener = d3.behavior.zoom().scaleExtent([0.1, 3]).on("zoom", zoom);
    baseSvg = d3.select("#tree-container").append("svg")
        .attr("width", viewerWidth * 2)
        .attr("height", viewerHeight * 2)
        .attr("class", "overlay")
        .call(zoomListener);
    svgGroup = baseSvg.append("g");
    root = treeData;
    root.x0 = viewerHeight / 2;
    root.y0 = 0;
    update(root);
    vcenterNode(root);
}
  )
}


点击获取代码:
//点击节点,从后台获取数据填充
function click(d) {
    if (d.a == true)
    {
        var p = d.depth;
        if (p > 0)
        {
            ttid = (d._id == undefined ? d.id : d._id);
            search_core(ttid, 0, 10);
            var l = d.parent;
            global_core_config.query = "";
            for(p=d.depth;p>1;p--)
            {
                global_core_config.query = l.w + global_core_config.query;
                l = l.parent;
            }
            global_core_config.query = global_core_config.query +" "+d.w;
        }
       
    }
    {
        var jsonstr = convertToText(root);;
    }
   
    if (typeof d.children !== 'undefined' || typeof d._children !== 'undefined') {
        d = toggleChildren(d);
        update(d);
    } else {
       
        $.post("find_children.action", $.toJSON({ core: global_core_config.core, query: d._id, style: 0 }), function (result) {
            var findflag = d.a;
            obj = $.parseJSON(result);
            if (obj.children.length == 0)
            {
                return;
            }
            var count = obj.children.length;
            for (var i = 0; i < count; i++) {
                if (obj.children[i].w != obj.w)
                    obj.children[i].w = obj.w + obj.children[i].w;
            }
            var aobj = obj.children;
            aobj.sort(function (a, b) {
                return a.w.localeCompare(b.w);
            });
            var newnodes = tree.nodes(aobj).reverse();
            d.children = newnodes[0];
            update(d);
        });
    }
}
转换树为json字符串,这样绘制的图可以保存为json格式的数据:
function convertToText(obj) {
    var string = [];
    if (obj == undefined) {
        return String(obj);
    } else if (typeof (obj) == "object" && (obj.join == undefined)) {
        for (prop in obj) {
            if (prop == "children" || prop == "_id" || prop == "w" || prop == "a")
            {
                if (obj.hasOwnProperty(prop))
                    string.push("\"" + prop + "\": " + convertToText(obj[prop]));
            }
        };
        return "{" + string.join(",") + "}";
    } else if (typeof (obj) == "object" && !(obj.join == undefined)) {
        for (prop in obj) {
            if (prop == "id" || prop == "depth" || prop == "x" || prop == "x0" || prop == "y" || prop == "y0" || obj[prop]==0)
            {
                continue;
            }
            string.push(convertToText(obj[prop]));
        }
        return "[" + string.join(",") + "]";
    } else if (typeof (obj) == "function") {
        string.push(obj.toString())
    } else {
        string.push(JSON.stringify(obj))
    }
    return string.join(",");
}
代码:
2 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买的老鼠仓股票怎么办? 草莓苗叶子干了怎么办 赌钱输了很多钱怎么办 晨勃消失了怎么办戒色 赌时时彩一直输怎么办 车停小区被砸了怎么办 车停在小区被砸怎么办 地板拖过有腥味怎么办 网联成立支付宝怎么办 微信号站街不了怎么办 苹果照片全没了怎么办 没给员工交社保怎么办 淘宝图片超过3m怎么办 亚马逊被跟卖了怎么办 刚开饭店没生意怎么办 趣店诈骗注册了怎么办 乐动力步数不准怎么办 乐动力计步为零怎么办 净网大师ios下架怎么办 探探手机号换了怎么办 大象册被和谐后怎么办 试客联盟不退款怎么办 比邻号被禁用了怎么办 被手机分期骗了怎么办 分期乐登录不上怎么办 拼多多绑定微信怎么办 狗吃了鸭骨头怎么办 在趣购上当了怎么办 加不了别人微信怎么办 微信号加不上人怎么办 微信无法加好友怎么办 失眠的时候怎么办才好 贵宾狗不吃饭该怎么办 玩时时彩输钱了怎么办 白色衣物被染色了怎么办 白衬衣漂白变黄怎么办 白衬衣被染红了怎么办 染色了的白衣服怎么办 洗白衣服染色了怎么办 白衣服染上色了怎么办 白色衣服被染色了怎么办