TreeView with Context Menu

You can use create a context menu for any Wijmo control using the Menu control. The TreeView below has a custom context menu with a few options. Right-click the tree to see it.

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as input from '@grapecity/wijmo.input'; import * as nav from '@grapecity/wijmo.nav'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create the context menu let ctxMenu = new input.Menu('#theMenu', { itemClicked: (sender) => alert('thanks for selecting ' + sender.selectedValue) }); // // create the tree let theTree = new nav.TreeView('#theTree', { itemsSource: getData(), displayMemberPath: 'header', childItemsPath: 'items' }); // // attach context menu to tree theTree.hostElement.addEventListener('contextmenu', e => { e.preventDefault(); ctxMenu.show(e); }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo TreeView Context Menus</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"> <!-- this is the tree --> <div id="theTree"></div> <!-- this is the context menu --> <select id="theMenu" style="display:none"> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option></option> <option value="option3">Option 3</option> <option value="option4">Option 4</option> </select> </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' } ] } ]; } /* context menu */ .wj-dropdown-panel.wj-listbox { padding: 6px 12px; } /* default trees on this sample */ .wj-treeview { 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; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import { Component, Inject, enableProdMode, NgModule, ViewChild } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjNavModule } from '@grapecity/wijmo.angular2.nav'; import { WjInputModule, WjMenu } from '@grapecity/wijmo.angular2.input'; import { DataService } from './app.data'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { @ViewChild('theMenu') theMenu: WjMenu; data: any[]; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } // showContextMenu(e: any) { e.preventDefault(); this.theMenu.show(e); } // menuItemClick(contextMenu: WjMenu) { alert('thanks for selecting ' + contextMenu.selectedValue); } } // @NgModule({ imports: [WjNavModule, WjInputModule, 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 Hosting GCControls</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"> <wj-tree-view [itemsSource]="data" [displayMemberPath]="'header'" [childItemsPath]="'items'" (contextmenu)="showContextMenu($event)"> </wj-tree-view> <wj-menu #theMenu (itemClicked)="menuItemClick(theMenu)" style="display: none"> <wj-menu-item value="option1">Option 1</wj-menu-item> <wj-menu-item value="option2">Option 2</wj-menu-item> <wj-menu-separator></wj-menu-separator> <wj-menu-item value="option3">Option 3</wj-menu-item> <wj-menu-item value="option4">Option 4</wj-menu-item> </wj-menu> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { 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' } ] } ]; } } /* context menu */ .wj-dropdown-panel.wj-listbox { padding: 6px 12px; } /* default trees on this sample */ .wj-treeview { 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; } <template> <div class="container-fluid"> <wj-tree-view :itemsSource="data" displayMemberPath="header" childItemsPath="items" :initialized="onTreeViewInitialized"> </wj-tree-view> <wj-menu :itemsSource="menuSource" :itemClicked="menuItemClick" :initialized="onMenuInitialized" style="display: none"> </wj-menu> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.input'; import '@grapecity/wijmo.vue2.nav'; import { getData } from './data'; new Vue({ el: '#app', data: { data: getData(), menuSource: ['Option 1', 'Option 2', '<span class="wj-separator"></span>', 'Option 3', 'Option 4'] }, methods: { onTreeViewInitialized(sender) { sender.hostElement.addEventListener('contextmenu', e => { e.preventDefault(); if (this.menu) { this.menu.show(e); } }); }, onMenuInitialized(sender) { this.menu = sender; }, menuItemClick() { alert('thanks for selecting ' + this.menu.selectedValue); } } }); </script> <style> /* context menu */ .wj-dropdown-panel.wj-listbox { padding: 6px 12px; } /* default trees on this sample */ .container-fluid .wj-treeview { 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 Context Menus</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 * as wjInput from '@grapecity/wijmo.react.input'; import { getData } from './data'; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(), menuSource: ['Option 1', 'Option 2', '<span class="wj-separator"></span>', 'Option 3', 'Option 4'] }; } render() { return (<div className="container-fluid"> <wjNav.TreeView itemsSource={this.state.data} displayMemberPath="header" childItemsPath="items" initialized={this.onTreeViewInitialized.bind(this)}> </wjNav.TreeView> <wjInput.Menu itemsSource={this.state.menuSource} itemClicked={this.menuItemClick.bind(this)} initialized={this.onMenuInitialized.bind(this)} style={{ display: "none" }}> </wjInput.Menu> </div>); } onTreeViewInitialized(sender) { sender.hostElement.addEventListener('contextmenu', e => { e.preventDefault(); if (this._menu) { this._menu.show(e); } }); } onMenuInitialized(sender) { this._menu = sender; } menuItemClick() { alert('thanks for selecting ' + this._menu.selectedValue); } } 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> /* context menu */ .wj-dropdown-panel.wj-listbox { padding: 6px 12px; } /* default trees on this sample */ .container-fluid .wj-treeview { 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: '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' } ] } ]; }