Version

Adding and Removing Nodes Overview and Examples (igTree)

Topic Overview

Purpose

This topic explains, with code examples, how to add and remove nodes of igTree™ control programmatically.

Required background

The following topics are prerequisites to understanding this topic:

  • igTree Overview: This topic provides conceptual information for the igTree control including information regarding: features, binding to data sources, requirements, and templates.

In this topic

This topic contains the following sections:

Introduction

Adding and removing nodes summary

The igTree™ control supports adding and removing of tree nodes.

Adding Nodes

To a node, you can add the following:

  • A node object
  • An array of nodes
  • A hierarchical node

Adding a node is done by addNode method.

Removing Nodes

You can remove a a node by either of the following:

Examples

The following table lists the code examples included in this topic.

Example Description
Code Example: Creating a New Node in a Tree This example demonstrates how to add a new node to the tree of the igTree control.
Code Example: Adding Multiple Nodes to a Tree This example demonstrates get-ting the selected node and adding an array of nodes to it.
Code Example: Removing a selected node from a Tree This example demonstrates get-ting selected node and removing it from an igTree.
Code Example: Removing a node by value from a Tree This example demonstrates removing nodes from an igTree by value

Code Example: Adding a New Node to a Tree

Description

This example demonstrates how to add a new node to the tree of the igTree control. In the example, you have to take the node Pepsi and add from an HTML place under the selected node. To do this, you get the reference to selected node in the igTree and then add the HTML list item to the selected node from the HTML list.

Preview

Code

In JavaScript:

var selectedNode = $("#tree").igTree("selectedNode").element;
// This returns a JSON object with the following structure:
// var newNode = {
//                  Text: "Pepsi",
//                  Value: 5
//               };
var newNode = clickedElement();
if (selectedNode != null) {
    // Adding the node to the tree
    $("#tree").igTree("addNode", newNode, selectedNode);
}

Code Example: Adding Multiple Nodes to a Tree

Description

This example demonstrates how add multiple nodes to the tree of the igTree control. For instance, you have to take a whole set of nodes nodes from an HTML list and that set under the selected node. To do this,, you get the reference to selected node in the igTree and then add the set of nodes from the HTML list. In the example, a randomizer us used to generate values for the new nodes (representing, for instance, quantities).

Preview

Code

In JavaScript:

var selectedNode = $("#tree").igTree("selectedNode").element;
if (selectedNode != null) {
    // Creating an array of new nodes
    var newNodes = [];
    // Converting the HTML list to the array of nodes
        var list = $("ul#items li").each(function () {
            var item = $(this);
            // Pushing new items with random values representing quantities
            newNodes.push({
                Text: item.html(),
                Value: Math.floor(Math.random() * 1001)
            });
        });
    // Adding the array of nodes to the tree
    $("#tree").igTree("addNode", newNodes, selectedNode);
}

Code Example: Removing a Selected Node from a Tree

Description

This example demonstrates how to remove a selected node from the tree of the igTree control. You first get the reference to selected node in igTree and then remove the node by referencing it by its path.

Preview

Code

In JavaScript:

var selectedPath = $("#tree").igTree("selectedNode").path;
if (selectedPath != null) {
    // Removing the selected node by path
    $("#tree").igTree("removeAt", selectedPath);
}

Code Example: Removing a Node by Value from a Tree

Description

This example demonstrates how remove a node from an igTree by value. In the example, you need to remove all nodes whose value matches the value the user has entered through an HTML input field. You get the value first and then remove the node by referencing it by that value.

Preview

Code

In JavaScript:

var nodeValue = $("#nodeValue").val();
if (nodeValue) {
    // Removing all nodes with the provided value
    $("#tree").igTree("removeNodesByValue", nodeValue);                   
 }

Related Content

Topics

The following topics provide additional information related to this topic.

Samples

The following samples provide additional information related to this topic.

View on GitHub