Fancytree - Init from object array

This sample includes all options (with their default values) to allow playing with them.

by Michael Baas

HTML

<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/mar10/fancytree@2/dist/skin-bootstrap/ui.fancytree.min.css">
<script src="https://cdn.jsdelivr.net/gh/mar10/fancytree@2/dist/jquery.fancytree-all-deps.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<div id="tree"></div>
<div id="status">Ready.</div>
<div id="version"></div>
<p>
    <button id="button1">Select item2</button>
</p>

CSS

#version { color: #888; font-size: 70%; }

JavaScript

$(function(){
    $("#tree").fancytree({
        checkbox: true,
        source: [
            {title: "Node 1"},
            {title: "Node 2", key: "id2"},
            {title: "Folder 3", folder: true, children: [
                {title: "Node 3.1"},
                {title: "Node 3.2"}
            ]},
            {title: "Folder 2", folder: true}
        ],
        activate: function(event, data){
            $("#status").text("Activate: " + data.node);
        }
    });
    
    // Activate a node, on button click
    $("#button1").click(function(){
        var tree = $("#tree").fancytree("getTree"),
            node2 = tree.getNodeByKey("id2");
        node2.toggleSelected();
    });
    $("#version").text("Fancytree " + $.ui.fancytree.version
    		+ " / jQuery " + $.fn.jquery);
});