简体   繁体   English

使用fancytree.js更改节点图标

[英]change node icon using fancytree.js

I am using fancytree.js for treeview and I have a callback on the tree: 我正在使用fancytree.js进行树视图,并且在树上有一个回调:

$("#tree").fancytree({ 
    source: {url: "/" + appPath + "/pathosadmin?treepages=true"},
    postProcess: function (event, data) {
        data.result = convertData(data.response, true);
    },
    init: function (event, data) {
        var root = $("#tree").fancytree("getRootNode");
        root.sortChildren(function (a, b) {
            return treeSort(a, b);
        }, true);
    },
    icon: function (event, data) {

        switch (data.node.data.NODE_TYPE) {
            case 1: //page
                if (data.node.data.STARTPAGE == 0) {
                    return "fancytree_page_icon";
                } else if (data.node.data.STARTPAGE == 1) {
                    _this.startPageNode = data.node;
                    return "fancytree_startpage_icon";
                }
            case 2: //group
                return "fancytree_group_icon";
            case 3: //level
                if (data.node.data.LEVELID) {
                    switch (data.node.data.LEVELID) {
                        case 1:
                            return "fancytree_level_1_icon";
                        case 2:
                            return "fancytree_level_2_icon";
                        case 3:
                            return "fancytree_level_3_icon";
                        case 4:
                            return "fancytree_level_4_icon";
                        case 5:
                            return "fancytree_level_5_icon";
                    }
                } else {
                    return "fancytree_location_icon";
                }
        }
    },
    extensions:

Now I want to also change the icons on runtime. 现在,我还想在运行时更改图标。 Sadly 可悲的是

if (_this.startPageNode) {
     _this.startPageNode.icon = "fancytree_page_icon";
     _this.startPageNode.renderTitle();
 }
 activeNode.icon = "fancytree_startpage_icon";
 activeNode.render();
 _this.startPageNode = activeNode;

doesnt work. 不起作用。 Any hints on how to tackle that problem. 关于如何解决该问题的任何提示。 The node.icon attribute is always undefined and even if i set it (+render the node) it doesnt show. node.icon属性始终是未定义的,即使我设置它(+渲染该节点)也不会显示。

renderTitle() calls icon function, so only way I found to change icon dynamically is to put some attribute on node renderTitle()调用了图标函数,所以我发现动态更改图标的唯一方法是在节点上放置一些属性

activeNode.state = 'clicked';
activeNode.renderTitle();

and then put extra handling login in icon function on tree: 然后在树上的图标功能中添加额外的处理登录名:

var state = data.node.state;
if (state) {
    switch (state) {
        case "clicked": return "glyphicon glyphicon-ban-circle"
            return;
        case "completed": return "glyphicon glyphicon-ok-circle"
            return;
        case "removed": return "glyphicon glyphicon-remove-circle"
            return;
        default:
            return "glyphicon glyphicon-remove-circle"
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM