Warm tip: This article is reproduced from stackoverflow.com, please click
css javascript jquery magento jstree

Protecting jsTree Styles from Other jsTree Instances

发布于 2020-04-23 15:21:56

Is it possible to prefix the class names generated by the jsTree jQuery plugin/library? If not is there a known path forward to have a single page share two jsTree elements that are styled differently?

The Problem:

I have a plugin for (a PHP based) content management system. This plugin uses the jsTree jQuery plugin to create several tree views that are visible on all pages.

The CMS UI also uses jsTree on a few of its pages, and our styles are conflicting with one another.

The DOM generation is complex enough that simply prefixing all the default styles with an ID does not work.

/* my 2008 era CSS tricks are no help, this does not work */

#an-id-on-the-div-that-contains-mytrees .jstree-node,
#an-id-on-the-div-that-contains-mytrees .jstree-children,
#an-id-on-the-div-that-contains-mytrees .jstree-container-ul {
  display: block;
  margin: 0;
  padding: 0;
  list-style-type: none;
  list-style-image: none;
}

The default CSS file is over 1,000 lines, and the prospect of carefully and surgically figuring out which style class is used where and supplying a more specific CSS rule for every existing rule is more than a little daunting.

I'm asking if there's a known path forward for quickly/easily using two jsTrees with different styles on the same page.

Questioner
Alan Storm
Viewed
44
Stephen S 2020-02-11 22:06

jsTree has support for using themes with its setup configuration.

$('#jstree').jstree({
  "core" : {
    "themes": { "name": "mycustomtheme", "dots": true, "icons": true }
  }
});

You can use the default CSS here as a template to modify the default class .jstree-default with your theme class .jstree-mycustomtheme. You can have multiple CSS stylesheets and apply them with the theme configuration. Although it's not the quickest approach, this is the extent of what jsTree supports at the moment.