Gazillion Gazillion - 3 months ago 36
Javascript Question

How to change icon in jstree?

I have the following code:

$('.wpFolders.co_files').bind('select_node.jstree', function (event, data) {
getFileById(data.args[0].hash.replace('#', ''));
}).jstree({
'plugins' : ['html_data','themes','ui','types'],
'ui' : {
'select_limit' : 1
},
'core' : {
'animation' : 0
},
'types': {
'default' : {
'icon' : {
'image' : '/admin/views/images/file.png'
}
}
}
});


I have a basic unordered list I would like to have displayed as a list of files. I'm trying to use the "types" to change the icon but I can't for the life of me figure out how to do it. I checked their docs link and even when using almost identical code nothing seems to happen.

From my understanding of the code above the default type of my tree should have the icon I specified but nothing happens, all I get is the default folder icon.

Any ideas? Sorry if the question seems basic but I find the documentation hard to follow when trying to do basic things. :)

Answer

Two problems:

  1. I needed to add the "type" in my rel attribute of my list objects (I created a default and file type).
  2. I forgot one level in my array declaring the types, the code had to be like the following:

    $('.wpFolders.co_files').bind('select_node.jstree', function (event, data) {
        getFileById(data.args[0].hash.replace('#', ''));
    }).jstree({
        'plugins' : ['html_data','themes','ui','types'],
        'ui' : {
            'select_limit' : 1
        },
        'core' : {
            'animation' : 0
        },
        'types': {
            'types' : {
                'file' : {
                    'icon' : {
                        'image' : '/admin/views/images/file.png'
                    }
                },
                'default' : {
                    'icon' : {
                        'image' : '/admin/views/images/file.png'
                    },
                    'valid_children' : 'default'
                }
            }
    
        }
    });
    

I don't really understand why my code is breaking in the WYSIWYG, sorry if it's ugly. Anyway, I hope this can help someone.