JSTree font awesome check Box

2.9k views Asked by At

I have a tree structure using jsTree, like this:

<div id="SampleContainer">
<ul> 
  <li>
    Top 1
    <ul>
      <li>
        Sub 1
        <ul>
          <li>
            A
          </li>
          <li>
            B
          </li>

        </ul>
      </li>
      <li>
        Sub 2
        <ul>
          <li>
            A
          </li>
          <li>
            B
          </li>

        </ul>
      </li>
    </ul>
  </li>

</ul>
</div>

I would like to put icons of font-awesome for the check-box:

I tried by doing this:

$(document).ready(function(){
   $("#SampleContainer").jstree({
    "plugins": ["checkbox"],
    core: {
      "themes": {
        "icons": false,
        "responsive": true
      }
    }
  });
    $('#SampleContainer').on('ready.jstree click', function (e, data) {     
        $('a > i.jstree-checkbox').removeClass('jstree-icon jstree-checkbox').addClass('fa fa-square-o');

    $('a > i.jstree-clicked').removeClass('jstree-icon jstree-checkbox').addClass('fa fa-check-square');
      });     
       });

and this icon 'fa fa-square-o' is coming. not able see the onclick 'fa fa-check-square'.!

sample Design: enter image description here

Developed Screen: enter image description here

Does anyone have an idea about this issue?

2

There are 2 answers

1
Guillaume Georges On BEST ANSWER

I added a new toggleCheckClasses function that adds / removes the right fa classes depending on the state of the checkbox. And I attached this function to the tree nodes in your treejs-ready / click handler.

EDIT : I added the toggling of children elements classes

$(document).ready(function() {
    $("#SampleContainer").jstree({
        "plugins": ["checkbox"],
        core: {
            "themes": {
                "icons": false,
                "responsive": true
            }
        }
    });
    $('#SampleContainer').on('ready.jstree click', function(e, data) {
        $('a > i.jstree-checkbox')
            .removeClass('jstree-icon jstree-checkbox') // removing jstree classes
            .addClass('fa fa-square-o') // adding the fa non-checked checkbox class
            .on('click', function() { // attaching the handler that toggles the checked / unchecked class
                toggleCheckClasses($(this), $(this).hasClass('fa-square-o'));
            });
    });

    function toggleCheckClasses(element, show) {
        if (show) {
            element.removeClass('fa-square-o');
            element.addClass('fa-check-square-o');
        } else {
            element.removeClass('fa-check-square-o');
            element.addClass('fa-square-o');
        }

        var children = element.parent().siblings(".jstree-children").find(".jstree-anchor .fa");

        children.each(function() {
            toggleCheckClasses($(this), show);
        });

    }

});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.5/themes/default/style.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.5/jstree.min.js"></script>
<div id="SampleContainer">
<ul> 
  <li>
    Top 1
    <ul>
      <li>
        Sub 1
        <ul>
          <li>
            A
          </li>
          <li>
            B
          </li>

        </ul>
      </li>
      <li>
        Sub 2
        <ul>
          <li>
            A
          </li>
          <li>
            B
          </li>

        </ul>
      </li>
    </ul>
  </li>

</ul>
</div>

0
Ruwen On

The interactions in the tree are not shown clean with the jquery css classes manipulation.

I created a touch friendly jsTree with high-resolution icons

I wrote an additional CSS File that overrides jsTree's style for checkboxes and carets with scalable flat font-awesome Icons (FontAwesome version: 4.7.0 FontAwesome v4).

You can use the following CSS included after jsTree's CSS file and FontAwesome v4.

Preview:

enter image description here

CSS:

.jstree-icon.jstree-checkbox, .jstree-no-dots .jstree-closed>.jstree-ocl, .jstree-no-dots .jstree-open>.jstree-ocl {
  background-image: none;
  background-position: initial;
  color: #767676;

  display: inline-block;
  font: normal normal normal 14px/1 FontAwesome;
  font-size: inherit;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.jstree-icon.jstree-checkbox:before {
  content: "\f096";
  font-size: 17px;
}

.jstree-default-large.jstree-checkbox-selection .jstree-clicked>.jstree-checkbox:before, .jstree-default-large .jstree-checked>.jstree-checkbox:before {
  content: "\f046";
  font-size: 17px;
  margin-left: 2px; /* checkedSquare icon is wider -> margin simulates same square position as unchecked */
}

.jstree-default-large .jstree-anchor>.jstree-undetermined:before{
  content: "\f147";
  font-size: 17px;
}

.jstree-default-large>.jstree-no-dots .jstree-closed>.jstree-ocl:before{
  content: "\f0da";
  line-height: 32px;
}

.jstree-default-large>.jstree-no-dots .jstree-open>.jstree-ocl:before{
  content: "\f0d7";
  line-height: 32px;
}

So there is no need to manipulate jsTree's DOM via javascript / jquery.

The folder icon is set via the jsTree() Initialisation (fontAwesome's fa-folder-open. Ecerything colored with CSS.

.jstree({
    "types":   {
        "default": {
            "icon": "fa fa-folder-open treeFolderIcon",
        }
    },
    "plugins": plugins,
    "core":    {
        "themes": {"variant": "large"},
        }
    }
});