8

It's easy to specify what the icon should be for a closed folder using the "types" plugin. But can the types plugin also be used to specify what an open folder should look like, or can I only do this with CSS (like below) ?

li.jstree-open > a .jstree-icon 
{
    background:url("folder_open.png") 0px 0px no-repeat !important;
} 
Captain Sensible
  • 4,946
  • 4
  • 36
  • 46

5 Answers5

8

A possible solution is to have two types - one for an open folder, and one for a closed folder. Changing the node type is easy.

From another answer of mine:

<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>
Community
  • 1
  • 1
Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
6

@Seventh element:

Your code in the question itself is the answer.It works pretty fine. For open node use

li.jstree-open > a .jstree-icon { background:url("/images/favorites.png") 0px 0px no-repeat !important; }

For closed nodes use

li.jstree-closed > a .jstree-icon { background:url("/images/favorites.png") 0px 0px no-repeat !important; }

Cheers

jeev
  • 478
  • 10
  • 25
6

If you guys want to use jQuery and bootstrap icon, here is my solution.

[Note: I use glyphicon-folder-open bootstrap icon for folder open and glyphicon-folder-close bootstrap icon for folder close.]

// bind customize icon change function in jsTree open_node event. 
$('#your-jstree').on('open_node.jstree', function(e, data){
   $('#' + data.node.id).find('i.jstree-icon.jstree-themeicon').first()
        .removeClass('glyphicon-folder-close').addClass('glyphicon-folder-open');
});

// bind customize icon change function in jsTree close_node event. 
$('#your-jstree').on('close_node.jstree', function(e, data){
   $('#' + data.node.id).find('i.jstree-icon.jstree-themeicon').first()
        .removeClass('glyphicon-folder-open').addClass('glyphicon-folder-close');
});

or if you are using font-awesome:

// bind customize icon change function in jsTree open_node event. 
$('#jstree').on('open_node.jstree', function(e, data){
    var icon = $('#' + data.node.id).find('i.jstree-icon.jstree-themeicon').first();
    icon.removeClass('fa-folder').addClass('fa-folder-open');
});

// bind customize icon change function in jsTree close_node event. 
$('#jstree').on('close_node.jstree', function(e, data){
    var icon = $('#' + data.node.id).find('i.jstree-icon.jstree-themeicon').first();
    icon.removeClass('fa-folder-open').addClass('fa-folder');
});
Timothy
  • 4,198
  • 6
  • 49
  • 59
Zin Min
  • 3,898
  • 1
  • 20
  • 24
0

At today, there is a function called "set_icon" (see API), that allow you to set the desider icon, from a path string or a class name.

$.jstree.reference('#jstree').set_icon(node, "/assets/contextMenu_icons/folderOpened.png")
0

Looks like you need to use css

li.jstree-open[rel=TYPE]{  background:url("openimage.gif") 0px 0px no-repeat !important; }
li.jstree-closed[rel=TYPE]{  background:url("closedimage.gif") 0px 0px no-repeat !important; }
li.jstree-leaf[rel=TYPE]{  background:url("leafimage.gif") 0px 0px no-repeat !important; }

more info in the jsTree forum

Radek
  • 13,813
  • 52
  • 161
  • 255