TreeView节点

在大多数情况下,您可以仅使用其数据绑定和选择属性以及绑定到树的原始数据项来使用TreeView。

在某些情况下,您可能需要访问构成实际树的 TreeNode 对象的属性。

您可以通过调用TreeView的 getNode 方法获取与给定数据项对应的 TreeNode,或使用 nodes 属性获取TreeView上的根节点集合。

拥有节点后,可以使用其属性和方法来检查或修改树。这些属性和方法包括:

  • dataItem:获取绑定到此节点的数据源中的项。
  • element:获取表示此节点的HTML元素。
  • ensureVisible:扩展父节点并根据需要滚动TreeView以确保节点当前可见。
  • select:选择节点并确保其可见。
  • parentNode:获取对节点父节点的引用。
  • index:获取包含它的节点集合中此节点的索引。
  • level:获取大纲结构中节点的级别。顶级节点的级别为零。
  • nodes:获取包含节点子节点的数组。
  • previous,next,previousSibling,nextSibling:获取对树上相邻节点的引用,可选择排除折叠或非兄弟节点。
  • isChecked,isCollapsed,isDisabled:获取或设置更改节点状态的值。

例如,单击下面的按钮以扫描TreeView上的visible节点。单击按钮时,尝试展开和折叠某些节点以查看差异:

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wjNav from '@grapecity/wijmo.nav'; import * as wjCore from '@grapecity/wijmo'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create the tree let theTree = new wjNav.TreeView('#theTree', { itemsSource: getData(), displayMemberPath: 'header', childItemsPath: 'items' }); // // scan visible nodes now and when the user clicks the button scanNodes(true); document.getElementById('scan').addEventListener('click', () => scanNodes(true)); // // scan nodes and show their information function scanNodes(visible) { let result = ''; // for (let node = theTree.nodes[0]; node; node = node.next(visible)) { result += wjCore.format('{header}', node.dataItem) + wjCore.format(' <span class="node-info">(level: {level}, index: {index}, isCollapsed: {isCollapsed})</span><br/>', node); } // document.getElementById('result').innerHTML = result; } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo TreeView Nodes Overview</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"> <div id="scan" class="btn btn-primary"> Scan Visible Nodes </div> <h4> Visible Nodes: </h4> <div id="result"></div> </div> </div> </div> </body> </html> // get the tree data export function getData() { return [ { header: 'Electronics', img: 'resources/electronics.png', items: [ { header: 'Trimmers/Shavers' }, { header: 'Tablets' }, { header: 'Phones', img: 'resources/phones.png', items: [ { header: 'Apple' }, { header: 'Motorola', newItem: true }, { header: 'Nokia' }, { header: 'Samsung' } ] }, { header: 'Speakers', newItem: true }, { header: 'Monitors' } ] }, { header: 'Toys', img: 'resources/toys.png', items: [ { header: 'Shopkins' }, { header: 'Train Sets' }, { header: 'Science Kit', newItem: true }, { header: 'Play-Doh' }, { header: 'Crayola' } ] }, { header: 'Home', img: 'resources/home.png', items: [ { header: 'Coffeee Maker' }, { header: 'Breadmaker', newItem: true }, { header: 'Solar Panel', newItem: true }, { header: 'Work Table' }, { header: 'Propane Grill' } ] } ]; } .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; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjCore from '@grapecity/wijmo'; // import { Component, Inject, enableProdMode, NgModule, ViewChild } 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 { @ViewChild('theTree') theTree: WjTreeView; data: TreeItem[]; scanResult: string; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } onLoadedItems() { this._scan(true); } // scanNode(s: WjTreeView) { this._scan(true); } // _scan(visible: boolean) { let result = ''; for (let node = this.theTree.nodes[0]; node; node = node.next(visible)) { result += wjCore.format('{header}', node.dataItem) + wjCore.format(' <span class="node-info">(level: {level}, index: {index}, isCollapsed: {isCollapsed})</span><br/>', node); } this.scanResult = result; } } // @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 Nodes Overview</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'" (loadedItems)="onLoadedItems()"> </wj-tree-view> </div> <div class="col-xs-7"> <div id="scan" class="btn btn-primary" (click)="scanNode()"> Scan Visible Nodes </div> <h4> Visible Nodes: </h4> <div id="result" [innerHtml]="scanResult"></div> </div> </div> </div> import { Injectable } from '@angular/core'; // export class TreeItem { header: string; newItem?: boolean; img?: string; items?: TreeItem[] } // @Injectable() export class DataService { getData(): TreeItem[] { return [ { header: 'Electronics', img: 'resources/electronics.png', items: [ { header: 'Trimmers/Shavers' }, { header: 'Tablets' }, { header: 'Phones', img: 'resources/phones.png', items: [ { header: 'Apple' }, { header: 'Motorola', newItem: true }, { header: 'Nokia' }, { header: 'Samsung' } ] }, { header: 'Speakers', newItem: true }, { header: 'Monitors' } ] }, { header: 'Toys', img: 'resources/toys.png', items: [ { header: 'Shopkins' }, { header: 'Train Sets' }, { header: 'Science Kit', newItem: true }, { header: 'Play-Doh' }, { header: 'Crayola' } ] }, { header: 'Home', img: 'resources/home.png', items: [ { header: 'Coffeee Maker' }, { header: 'Breadmaker', newItem: true }, { header: 'Solar Panel', newItem: true }, { header: 'Work Table' }, { header: 'Propane Grill' } ] } ]; } } .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; } <template> <div class="container-fluid"> <div class="row"> <div class="col-xs-5"> <wj-tree-view :items-source="data" displayMemberPath="header" childItemsPath="items" :initialized="initTreeView" :loadedItems="itemsLoaded"> </wj-tree-view> </div> <div class="col-xs-7"> <div id="scan" class="btn btn-primary" @click="scanNode"> Scan Visible Nodes </div> <h4> Visible Nodes: </h4> <div id="result" v-html="scanResult"></div> </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(), scanResult: null } }, methods:{ initTreeView: function(ctl){ this.wjTreeViewControl = ctl; }, scanNode(s) { this.$_scan(true); }, $_scan(visible) { let result = ''; for (let node = this.wjTreeViewControl.nodes[0]; node; node = node.next(visible)) { result += wjCore.format('{header}', node.dataItem) + wjCore.format(' <span class="node-info">(level: {level}, index: {index}, isCollapsed: {isCollapsed})</span><br/>', node); } this.scanResult = result; }, itemsLoaded(){ this.$_scan(true); } } }) </script> <style> .container-fluid .wj-treeview { display:block; font-size: 120%; 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; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo TreeView Nodes Overview</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: 'Electronics', img: 'resources/electronics.png', items: [ { header: 'Trimmers/Shavers' }, { header: 'Tablets' }, { header: 'Phones', img: 'resources/phones.png', items: [ { header: 'Apple' }, { header: 'Motorola', newItem: true }, { header: 'Nokia' }, { header: 'Samsung' } ] }, { header: 'Speakers', newItem: true }, { header: 'Monitors' } ] }, { header: 'Toys', img: 'resources/toys.png', items: [ { header: 'Shopkins' }, { header: 'Train Sets' }, { header: 'Science Kit', newItem: true }, { header: 'Play-Doh' }, { header: 'Crayola' } ] }, { header: 'Home', img: 'resources/home.png', items: [ { header: 'Coffeee Maker' }, { header: 'Breadmaker', newItem: true }, { header: 'Solar Panel', newItem: true }, { header: 'Work Table' }, { header: 'Propane Grill' } ] } ]; } 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(), scanResult: [] }; } initTreeView(ctl) { this._wjTreeViewControl = ctl; } scanNode(s) { this.scan(true); } scan(visible) { let result = [], cnt = 0; for (let node = this._wjTreeViewControl.nodes[0]; node; node = node.next(visible)) { cnt++; result.push(<p></p>); result.push(<span className="node-info">{node.dataItem.header} (level: {node.level}, index: {node.index}, isCollapsed: {node.isCollapsed})</span>); result.push(<br />); } this.setState({ scanResult: result }); } itemsLoaded() { this.scan(true); } 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.initTreeView.bind(this)} loadedItems={this.itemsLoaded.bind(this)}></wjNav.TreeView> </div> <div className="col-xs-7"> <div id="scan" className="btn btn-primary" onClick={this.scanNode.bind(this)}> Scan Visible Nodes </div> <h4> Visible Nodes: </h4> <div id="result">{this.state.scanResult}</div> </div> </div> </div>); } } 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 { display:block; font-size: 120%; 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; } export function getData() { return [ { header: 'Electronics', img: 'resources/electronics.png', items: [ { header: 'Trimmers/Shavers' }, { header: 'Tablets' }, { header: 'Phones', img: 'resources/phones.png', items: [ { header: 'Apple' }, { header: 'Motorola', newItem: true }, { header: 'Nokia' }, { header: 'Samsung' } ] }, { header: 'Speakers', newItem: true }, { header: 'Monitors' } ] }, { header: 'Toys', img: 'resources/toys.png', items: [ { header: 'Shopkins' }, { header: 'Train Sets' }, { header: 'Science Kit', newItem: true }, { header: 'Play-Doh' }, { header: 'Crayola' } ] }, { header: 'Home', img: 'resources/home.png', items: [ { header: 'Coffeee Maker' }, { header: 'Breadmaker', newItem: true }, { header: 'Solar Panel', newItem: true }, { header: 'Work Table' }, { header: 'Propane Grill' } ] } ]; }