最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change icon on click jstree - Stack Overflow

programmeradmin2浏览0评论

I have this code using jstree plugin.

$(".gems-tree").on('changed.jstree' , function( event , data ){
  console.log("folder clicked");
});

And it works, but now i want to change the icon from the folder to close to open, is there a way to achive this?

NOTE

Already try with data.node.state.opened = true just to see if the folder icon change but not.

I have this code using jstree plugin.

$(".gems-tree").on('changed.jstree' , function( event , data ){
  console.log("folder clicked");
});

And it works, but now i want to change the icon from the folder to close to open, is there a way to achive this?

NOTE

Already try with data.node.state.opened = true just to see if the folder icon change but not.

Share Improve this question edited May 23, 2015 at 6:05 carloss medranoo asked May 23, 2015 at 5:57 carloss medranoocarloss medranoo 1,4893 gold badges15 silver badges21 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 7

If you need to change the icon of each selected node, the answer by Adnan Y will work (just make sure data.action is "select_node"):

$("#jstree2").on('changed.jstree', function(evt, data) {
  if(data.action === "select_node") {
    data.instance.set_icon(data.node, 'http://jstree.com/tree-icon.png');
  }
});

If you need to react on nodes opening and closing, use similar code:

$("#jstree2")
  .on('open_node.jstree', function(evt, data) {
    data.instance.set_icon(data.node, 'http://jstree.com/tree-icon.png');
  })
  .on('close_node.jstree', function(evt, data) {
    data.instance.set_icon(data.node, true);
  });

In the second example the icon is set to true - this will restore it to the default icon (if this is what you need).

This can be done using jstree's types plugin.

Here's an example, using font-awesome for the folder icons.

<div id="jstree_menu"></div>
<script>
/* Load menu tree data */
$('#jstree_menu').jstree(
    {
        'core' : {
            'data' : {
                'url' : '/jstree-menu-data/index.html',
            }
        },
        'plugins' : [ "types" ],
        'types' : {
            'default' : {
                'icon' : 'fa fa-angle-right fa-fw'
            },
            'f-open' : {
                'icon' : 'fa fa-folder-open fa-fw'
            },
            'f-closed' : {
                'icon' : 'fa fa-folder fa-fw'
            }
        }
    }
);

/* Toggle between folder open and folder closed */
$("#jstree_menu").on('open_node.jstree', function (event, data) {
    data.instance.set_type(data.node,'f-open');
});
$("#jstree_menu").on('close_node.jstree', function (event, data) {
    data.instance.set_type(data.node,'f-closed');
});
</script>
$("#jstree2").on('changed.jstree', function(evt, data){
  $("#jstree2").jstree(true).set_icon(data.node, 'http://jstree.com/tree-icon.png')
})
发布评论

评论列表(0)

  1. 暂无评论