Adding Nodes

You can add nodes to a tree by calling the addChildNode method. The TreeView control and TreeNode object have an addChildNode method. This sample shows how to add nodes to a root node and to a child node.

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjNav from '@grapecity/wijmo.nav'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // // create the tree var theTree = new wjNav.TreeView('#theTree', { itemsSource: getData(), displayMemberPath: 'header', childItemsPath: 'items' }); theTree.selectedItem = theTree.itemsSource[0]; // // handle buttons document.getElementById('btnFirst').addEventListener('click', function () { var newItem = { header: document.getElementById('theInput').value }, node = theTree.selectedNode; if (node) { theTree.selectedNode = node.addChildNode(0, newItem); } else { theTree.selectedNode = theTree.addChildNode(0, newItem); } }); document.getElementById('btnLast').addEventListener('click', function () { var newItem = { header: document.getElementById('theInput').value }, node = theTree.selectedNode; if (node) { var index = node.nodes ? node.nodes.length : 0; theTree.selectedNode = node.addChildNode(index, newItem); } else { var index = theTree.nodes ? theTree.nodes.length : 0; theTree.selectedNode = theTree.addChildNode(index, newItem); } }); document.getElementById('btnNoSel').addEventListener('click', function () { theTree.selectedNode = null; }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo TreeView Adding Nodes</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-xs-5"> <div id="theTree"> </div> </div> <div class="col-xs-7"> <p> New Node Content: <input class="form-control" id="theInput" value="My New Node"> </p> <p> Add Node: <button id="btnFirst" class="btn btn-default">First Child</button> <button id="btnLast" class="btn btn-default">Last Child</button> </p> <p> <button id="btnNoSel" class="btn btn-default">Remove Selection</button> </p> </div> </div> </div> </body> </html> // create some data export function getData() { return [ { header: 'Parent 1', items: [ { header: 'Child 1.1' }, { header: 'Child 1.2' }, { header: 'Child 1.3' } ] }, { header: 'Parent 2', items: [ { header: 'Child 2.1' }, { header: 'Child 2.2' } ] }, { header: 'Parent 3', items: [ { header: 'Child 3.1' } ] } ]; } /* default trees on this sample */ .wj-treeview { font-size: 120%; margin-top: 8px; margin-bottom: 8px; padding: 6px; background: #f0f0f0; box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23); } body { margin-bottom: 24pt; } #theInput { font-weight: normal; display: inline-block; width: 12em; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import { Component, Inject, enableProdMode, NgModule, ViewChild, AfterViewInit } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjNavModule, WjTreeView } from '@grapecity/wijmo.angular2.nav'; import { DataService, TreeItem } from './app.data'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent implements AfterViewInit { @ViewChild('theTree') theTree: WjTreeView; data: TreeItem[]; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } // ngAfterViewInit() { this.theTree.selectedItem = this.theTree.itemsSource[0]; } // onFirstClick() { var theTree = this.theTree; var newItem = { header: (document.getElementById('theInput') as HTMLInputElement).value }, node = theTree.selectedNode; if (node) { theTree.selectedNode = node.addChildNode(0, newItem); } else { theTree.selectedNode = theTree.addChildNode(0, newItem); } } // onLastClick() { var theTree = this.theTree; var newItem = { header: (document.getElementById('theInput') as HTMLInputElement).value }, node = theTree.selectedNode; if (node) { var index = node.nodes ? node.nodes.length : 0; theTree.selectedNode = node.addChildNode(index, newItem); } else { var index = theTree.nodes ? theTree.nodes.length : 0; theTree.selectedNode = theTree.addChildNode(index, newItem); } } // onNoSelClick() { this.theTree.selectedNode = null; } } // @NgModule({ imports: [WjNavModule, BrowserModule], declarations: [AppComponent], providers: [DataService], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo TreeView Adding Nodes</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <div class="row"> <div class="col-xs-5"> <wj-tree-view #theTree [itemsSource]="data" [displayMemberPath]="'header'" [childItemsPath]="'items'"></wj-tree-view> </div> <div class="col-xs-7"> <p> New Node Content: <input class="form-control" id="theInput" value="My New Node"> </p> <p> Add Node: <button id="btnFirst" class="btn btn-default" (click)="onFirstClick()">First Child</button> <button id="btnLast" class="btn btn-default" (click)="onLastClick()">Last Child</button> </p> <p> <button id="btnNoSel" class="btn btn-default" (click)="onNoSelClick()">Remove Selection</button> </p> </div> </div> </div> import { Injectable } from '@angular/core'; export class TreeItem { header: string; items?: TreeItem[] } @Injectable() export class DataService { getData(): TreeItem[] { return [ { header: 'Parent 1', items: [ { header: 'Child 1.1' }, { header: 'Child 1.2' }, { header: 'Child 1.3' }] }, { header: 'Parent 2', items: [ { header: 'Child 2.1' }, { header: 'Child 2.2' }] }, { header: 'Parent 3', items: [ { header: 'Child 3.1' }] } ]; } } /* default trees on this sample */ .wj-treeview { font-size: 120%; margin-top: 8px; margin-bottom: 8px; padding: 6px; background: #f0f0f0; box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23); } body { margin-bottom: 24pt; } #theInput { font-weight: normal; display: inline-block; width: 12em; } <template> <div class="container-fluid"> <div class="row"> <div class="col-xs-5"> <wj-tree-view :itemsSource="data" :displayMemberPath="'header'" :childItemsPath="'items'" :initialized="initialized" ></wj-tree-view> </div> <div class="col-xs-7"> <p> New Node Content: <input class="form-control" id="theInput" value="My New Node"> </p> <p> Add Node: <button id="btnFirst" class="btn btn-default" @click="onFirstClick()">First Child</button> <button id="btnLast" class="btn btn-default" @click="onLastClick()">Last Child</button> </p> <p> <button id="btnNoSel" class="btn btn-default" @click="onNoSelClick()">Remove Selection</button> </p> </div> </div> </div> </template> <script> import "bootstrap.css"; import "@grapecity/wijmo.styles/wijmo.css"; import Vue from "vue"; import "@grapecity/wijmo.vue2.nav"; import * as wjCore from "@grapecity/wijmo"; import { getData } from "./data"; new Vue({ el: "#app", data: function() { return { data: getData(), }; }, methods: { onFirstClick: function() { var control = this.control; var newItem = { header: document.getElementById("theInput").value }, node = control.selectedNode; if (node) { control.selectedNode = node.addChildNode(0, newItem); } else { control.selectedNode = control.addChildNode(0, newItem); } }, onLastClick: function() { var control = this.control; var newItem = { header: document.getElementById("theInput").value }, node = control.selectedNode; if (node) { var index = node.nodes ? node.nodes.length : 0; control.selectedNode = node.addChildNode(index, newItem); } else { var index = control.nodes ? control.nodes.length : 0; control.selectedNode = control.addChildNode(index, newItem); } }, onNoSelClick: function() { this.control.selectedNode = null; }, initialized: function(control) { this.control = control; this.control.selectedItem = this.control.itemsSource[0]; } } }); </script> <style> .container-fluid .wj-treeview { font-size: 120%; margin-top: 8px; margin-bottom: 8px; padding: 6px; background: #f0f0f0; box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23); } body { margin-bottom: 24pt; } #theInput { font-weight: normal; display: inline-block; width: 12em; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo TreeView Adding Nodes</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"> </div> </body> </html> export function getData(){ return [ { header: 'Parent 1', items: [ { header: 'Child 1.1' }, { header: 'Child 1.2' }, { header: 'Child 1.3' }] }, { header: 'Parent 2', items: [ { header: 'Child 2.1' }, { header: 'Child 2.2' }] }, { header: 'Parent 3', items: [ { header: 'Child 3.1' }] } ]; } import './app.css'; import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as wjNav from '@grapecity/wijmo.react.nav'; import { getData } from './data'; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(), newNodeContent: "My New Node" }; } render() { return (<div className="container-fluid"> <div className="row"> <div className="col-xs-5"> <wjNav.TreeView itemsSource={this.state.data} displayMemberPath="header" childItemsPath="items" initialized={this.initialized.bind(this)}></wjNav.TreeView> </div> <div className="col-xs-7"> <p> New Node Content: <input className="form-control" id="theInput" value={this.state.newNodeContent} onChange={this.setNewNodeContent.bind(this)}/> </p> <p> Add Node: <button id="btnFirst" className="btn btn-default" onClick={this.onFirstClick.bind(this)}>First Child</button> <button id="btnLast" className="btn btn-default" onClick={this.onLastClick.bind(this)}>Last Child</button> </p> <p> <button id="btnNoSel" className="btn btn-default" onClick={this.onNoSelClick.bind(this)}>Remove Selection</button> </p> </div> </div> </div>); } onFirstClick() { var control = this._control; var newItem = { header: document.getElementById("theInput").value }, node = control.selectedNode; if (node) { control.selectedNode = node.addChildNode(0, newItem); } else { control.selectedNode = control.addChildNode(0, newItem); } } onLastClick() { var control = this._control; var newItem = { header: document.getElementById("theInput").value }, node = control.selectedNode; if (node) { var index = node.nodes ? node.nodes.length : 0; control.selectedNode = node.addChildNode(index, newItem); } else { var index = control.nodes ? control.nodes.length : 0; control.selectedNode = control.addChildNode(index, newItem); } } onNoSelClick() { this._control.selectedNode = null; } initialized(control) { this._control = control; this._control.selectedItem = this._control.itemsSource[0]; } setNewNodeContent(e) { this.setState({ newNodeContent: e.target.value }); } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AutoComplete</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> .container-fluid .wj-treeview { font-size: 120%; margin-top: 8px; margin-bottom: 8px; padding: 6px; background: #f0f0f0; box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23); } body { margin-bottom: 24pt; } #theInput { font-weight: normal; display: inline-block; width: 12em; } export function getData() { return [ { header: 'Parent 1', items: [ { header: 'Child 1.1' }, { header: 'Child 1.2' }, { header: 'Child 1.3' } ] }, { header: 'Parent 2', items: [ { header: 'Child 2.1' }, { header: 'Child 2.2' } ] }, { header: 'Parent 3', items: [ { header: 'Child 3.1' } ] } ]; }