Skip to content
Daniel Gimenez edited this page Aug 10, 2013 · 17 revisions

This is another tree view widget for jQuery. It is differentiated from others by its flexibility creating a tree from a data source, and its intent on keeping the number of DOM objects to a minimum by removing invisible nodes. It is best for very large, data driven trees.

It is located at \Scripts\Plugins\dgBlocks-1.0.5.js

Members

Object Literals

  • node
  • id: optional. If not assigned, one will be created.
  • value: either a primitive type, or if it is an object keys will attempt to match elements in the data template that have an attribute with the key (replacing the attribute value), or an element with the attribute data-tree-key equaling the key (replacing the attribute's html).

Options

All options have defaults set.

  • dataTemplate: a single jQuery element to be reused when displaying a node's text portion.
  • handleTemplate: a signle jQuery element to be reused when displaying a parent node's handle.
  • nodeTransform: a function with one argument (function (data)). This function will use data and transform it into a node type expected by the tree.
  • childrenTransform: a function with one argument (function (data)). This function will return an array of child nodes derived from data.
  • nodes: nodes to add to the tree.

Methods

  • addNode Adds a single node to a tree.
  • usage: $(treeElement).simpleTree('addNode', node, [parentID], [startIndex]);
  • parameters:
    • node: data to add to the tree.
    • parentID: optional. If none is provided, then the node is added to the root.
    • startIndex: optional. What index of the parent should the nodes be inserted.
  • addNodes Adds multiple nodes to a tree.
  • usage: $(treeElement).simpleTree('addNodes', nodes, [parentID], [startIndex]);
  • parameters:
    • nodes: an array of nodes.
    • parentID: optional. If none is provided, then the node is added to the root.
    • startIndex: optional. What index of the parent should the nodes be inserted.
  • swapNode Replaces a node on a tree with a new node with the same id.
  • usage: $(treeElement).simpleTree('swapNode', node);
  • commentary: Tries to maintain the expanded state of matching nodes with the same id.
  • parameters:
    • node: a tree node
  • swapNodes Replaces all nodes on a tree with new nodes.
  • usage: $(treeElement).simpleTree('swapNodes', nodes);
  • commentary: Tries to maintain the expanded state of matching nodes with the same id.
  • parameters:
    • nodes: an array of nodes.
  • empty Removes all elements on the tree
  • usage: $(treeElement).simpleTree('empty');
  • removeNode Removes a node from the tree
  • usage: $(treeElement).simpleTree('removeNode', node);
  • parameters:
    • node: a tree node
  • getNode Get's a node for a given id.
  • usage: $(treeElement).simpleTree('getNode', nodeID);
  • parameters:
    • nodeID: an id for a node
  • returns: a node, or undefined if no matching node is found.
  • getElementNode Gets an the nearest node to an DOM or jQuery element
  • usage: var treeNode = $(treeElement).simpleTree(getElementNode, element);
  • parameters:
    • element: DOM or jQuery element.
  • commentary: Will search from the element passed upwards until the root of the tree is reached.
  • getNodeLi Gets the list item element for a node (does not necessarily have to be an <li> element)
  • usage: var elemDOM = $(treeElement).simpleTree('getNodeLi', node);
  • parameters:
    • node: a tree node
  • toggleNode Hides/shows a node's children
  • usage: $(treeElement).simpleTree(toggleNode, node, [animate]);
  • parameters:
    • node: a tree node
    • animate: (optional) animates the transition.
  • expandNode Expands a node if it is collapsed
  • usage: $(treeElement).simpleTree(expandNode, node, [animate]);
  • parameters:
    • node: a tree node
    • animate: (optional) animates the transition.
  • expandNodes Expands nodes from an array of node ids
  • usage: $(treeElement).simpleTree(expandNodes, nodeIDs, [animate]);
  • parameters:
    • nodeIDs: an array of node ids
    • animate: (optional) animates the transition.
  • collapseNode Collapses a node if it is expanded
  • usage: $(treeElement).simpleTree(collapseNode, node, [animate]);
  • parameters:
    • node: a tree node
    • animate: (optional) animates the transition.

Events

  • simpletreeclick, simpletreemouseleave, simpletreemouseover, simpletreemouseup, simpletreemousedown, simpletreetogglestart, simpletreetogglefail
  • usage:
$(treeElement).on('simpletreeclick', function(ev, data) {
    console.log('node with id ' + data.node.id + ' clicked.');
});

usage

$('#myTree').simpleTree({
    nodeTransform: function (data) {
        return {
            value: data.cookieTaste,
            id: data.cookieName,
            children: kids
        }
    },
    click: function (ev, data) {
        alert('This has lots of chips : ' + data.node.chipCount);
    }
}).one('simpletreemouseover', function(ev, data) {
    alert(data.id + " doesn't like it when you hover.");
)).addNodes([
    { 
        cookieTaste: 'good', 
        cookieName: 'chocolateChip', 
        kids: [
            { cookieTaste: 'bad', cookieName: 'raisin nut' }
        ]
    }
]);

Clone this wiki locally