vakata / jstree

jquery tree plugin
http://jstree.com
MIT License
5.15k stars 1.38k forks source link

Using jstree in a library: TypeError: $element.jstree is not a function #2640

Closed tacman closed 2 years ago

tacman commented 2 years ago

How do I use jstree in a javascript library (it's a stimulus controller, but that shouldn't matter).

import 'jstree';
import $ from 'jquery'; 

    let treeElement = this.ajaxTarget; // a DOM element
    let $element = $(this.treeElement); // 
    let tree = $element
            .jstree({})

I can get this to work from within my application, using webpack, which handles jquery with a special command to make it global.

But when I create a library that I want to use in multiple project, I think I'm doing something wrong with importing jquery. I've been hacking away, require v. import, different order, setting a global.$ = $, etc.

Is there a way to invoke jstree on a DOM element, rather than turning the DOM object into a jQuery object and then calling the .jstree method on it? That "feels" like it'd be a better solution.

Thanks.

tacman commented 2 years ago

This appears to work for me now.

import jQuery from 'jquery';
import 'jstree';