Context Menus

The Menu control in this sample is used to create Context Menus for other Wijmo controls.

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as input from '@grapecity/wijmo.input'; import * as wijmo from '@grapecity/wijmo'; import * as chart from '@grapecity/wijmo.chart'; import * as grid from '@grapecity/wijmo.grid'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create the menu let menu = new input.Menu(document.createElement('div'), { displayMemberPath: 'header', selectedValuePath: 'cmd', dropDownCssClass: 'ctx-menu', itemsSource: [ { header: '<span class="glyphicon glyphicon-asterisk"></span>&nbsp;&nbsp;New', cmd: 'cmdNew' }, { header: '<span class="glyphicon glyphicon-folder-open"></span>&nbsp;&nbsp;Open', cmd: 'cmdOpen' }, { header: '<span class="glyphicon glyphicon-floppy-disk"></span>&nbsp;&nbsp;Save', cmd: 'cmdSave' }, { header: '<span class="wj-separator"></span>' }, { header: '<span class="glyphicon glyphicon-remove"></span>&nbsp;&nbsp;Exit', cmd: 'cmdExit' } ], itemClicked: () => { alert('Executing **' + menu.selectedValue + '** for element **' + menu.owner.id + '**'); } }); // // use it as a context menu for one or more elements let els = document.querySelectorAll('.has-ctx-menu'); for (let i = 0; i < els.length; i++) { els[i].addEventListener('contextmenu', e => { menu.owner = wijmo.closest(e.target, '.has-ctx-menu'); if (menu.owner) { e.preventDefault(); menu.show(e); } }, true); } // // create FlexChart and FlexGrid let theChart = new chart.FlexChart('#theChart', { itemsSource: getData(), bindingX: 'country', series: [ { binding: 'sales', name: 'Sales' }, { binding: 'expenses', name: 'Expenses' }, ] }); // let theGrid = new grid.FlexGrid('#theGrid', { itemsSource: getData() }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity 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"> <div class="owners"> <div id="first" class="has-ctx-menu" style="background:#f0a0a0"> I have a Context Menu. </div> <div id="second" class="has-ctx-menu" style="background:#a0f0a0"> I have the same Context Menu. </div> </div> <h4> The same approach works with all Wijmo controls: </h4> <p> FlexChart with ContextMenu </p> <div id="theChart" class="has-ctx-menu"></div> <p> FlexGrid with ContextMenu </p> <div id="theGrid" class="has-ctx-menu"></div> </div> </body> </html> // create some random data export function getData() { let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i % countries.length], sales: Math.random() * 100000, expenses: Math.random() * 50000 }); } // return data; } .owners:after { content: ""; clear: both; display: block; } .owners > div { float: left; margin: 20px; padding: 20px; } .wj-flexgrid { max-height: 250px; } .wj-flexchart { max-height: 250px; } .ctx-menu { padding: 3px; min-width: 120px; background: #FFFBDD; overflow: hidden; } .ctx-menu .wj-listbox-item { text-align: center; margin: 6px; } body { margin-bottom: 24pt; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as input from '@grapecity/wijmo.input'; // import { Component, enableProdMode, NgModule, Inject } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjInputModule } from '@grapecity/wijmo.angular2.input'; import { WjChartModule } from '@grapecity/wijmo.angular2.chart'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; import { DataService } from './app.data'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } // menuItemClicked(menu: input.Menu) { alert('Executing **' + menu.selectedValue + '** for element **' + menu.owner.id + '**'); } } // @NgModule({ imports: [WjInputModule, WjChartModule, WjGridModule, 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 Context Menus</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="owners"> <div id="first" [wjContextMenu]="ctxMenu" style="background:#f0a0a0"> I have a Context Menu. </div> <div id="second" [wjContextMenu]="ctxMenu" style="background:#a0f0a0"> I have the same Context Menu. </div> </div> <h4> The same approach works with all Wijmo controls: </h4> <p> FlexChart with ContextMenu </p> <wj-flex-chart id="theChart" [itemsSource]="data" [bindingX]="'country'" [wjContextMenu]="ctxMenu"> <wj-flex-chart-series [binding]="'sales'" [name]="'Sales'"></wj-flex-chart-series> <wj-flex-chart-series [binding]="'expenses'" [name]="'Expenses'"></wj-flex-chart-series> </wj-flex-chart> <p> FlexGrid with ContextMenu </p> <wj-flex-grid id="theGrid" [itemsSource]="data" [wjContextMenu]="ctxMenu"></wj-flex-grid> <!-- Menu --> <wj-menu #ctxMenu style="display:none" [header]="'Context Menu'" [selectedValuePath]="'cmd'" [dropDownCssClass]="'ctx-menu'" (itemClicked)="menuItemClicked(ctxMenu)"> <wj-menu-item cmd="cmdNew"> <span class="glyphicon glyphicon-asterisk"></span>&nbsp;&nbsp;New </wj-menu-item> <wj-menu-item cmd="cmdOpen"> <span class="glyphicon glyphicon-folder-open"></span>&nbsp;&nbsp;Open </wj-menu-item> <wj-menu-item cmd="cmdSave"> <span class="glyphicon glyphicon-floppy-disk"></span>&nbsp;&nbsp;Save </wj-menu-item> <wj-menu-separator></wj-menu-separator> <wj-menu-item cmd="cmdExit"> <span class="glyphicon glyphicon-remove"></span>&nbsp;&nbsp;Exit </wj-menu-item> </wj-menu> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { getData() { let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i % countries.length], sales: Math.random() * 100000, expenses: Math.random() * 50000 }); } // return data; } } .owners:after { content: ""; clear: both; display: block; } .owners > div { float: left; margin: 20px; padding: 20px; } .wj-flexgrid { max-height: 250px; } .wj-flexchart { max-height: 250px; } .ctx-menu { padding: 3px; min-width: 120px; background: #FFFBDD; overflow: hidden; } .ctx-menu .wj-listbox-item { text-align: center; margin: 6px; } body { margin-bottom: 24pt; } <template> <div class="container-fluid"> <div class="owners"> <div id="first" class="has-ctx-menu" style="background:#f0a0a0"> I have a Context Menu. </div> <div id="second" class="has-ctx-menu" style="background:#a0f0a0"> I have the same Context Menu. </div> </div> <h4> The same approach works with all Wijmo controls: </h4> <p> FlexChart with ContextMenu </p> <wj-flex-chart id="theChart" class="has-ctx-menu" :itemsSource="data" :bindingX="'country'"> <wj-flex-chart-series :binding="'sales'" :name="'Sales'"></wj-flex-chart-series> <wj-flex-chart-series :binding="'expenses'" :name="'Expenses'"></wj-flex-chart-series> </wj-flex-chart> <p> FlexGrid with ContextMenu </p> <wj-flex-grid id="theGrid" class="has-ctx-menu" :itemsSource="data"></wj-flex-grid> <!-- Menu --> <wj-menu style="display:none" :header="'Context Menu'" :selectedValuePath="'cmd'" :initialized="initMenu" :dropDownCssClass="'ctx-menu'" :itemClicked="menuItemClicked" :itemsSource="itemsSource" :displayMemberPath="'header'"> </wj-menu> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import * as wijmo from '@grapecity/wijmo'; import '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.input'; import '@grapecity/wijmo.vue2.chart'; import '@grapecity/wijmo.vue2.grid'; import { getData } from './data'; let App = Vue.extend({ name: 'app', data: function() { return { data: getData(), itemsSource: [ { header: '<span class="glyphicon glyphicon-asterisk"></span>&nbsp;&nbsp;New', cmd: 'cmdNew' }, { header: '<span class="glyphicon glyphicon-folder-open"></span>&nbsp;&nbsp;Open', cmd: 'cmdOpen' }, { header: '<span class="glyphicon glyphicon-floppy-disk"></span>&nbsp;&nbsp;Save', cmd: 'cmdSave' }, { header: '<span class="wj-separator"></span>' }, { header: '<span class="glyphicon glyphicon-remove"></span>&nbsp;&nbsp;Exit', cmd: 'cmdExit' } ], menu: {} } }, methods: { initMenu: function(menu){ this.menu = menu; }, itemClicked: function(menu) { alert('Running ' + menu.selectedValue); }, menuItemClicked: function(menu) { alert('Executing **' + menu.selectedValue + '** for element **' + menu.owner.id + '**'); } }, mounted: function(){ // use it as a context menu for one or more elements let els = document.querySelectorAll('.has-ctx-menu'); for (let i = 0; i < els.length; i++) { els[i].addEventListener('contextmenu', e => { this.menu.owner = wijmo.closest(e.target, '.has-ctx-menu'); if (this.menu.owner) { e.preventDefault(); this.menu.show(e); } }, true); } } }) let vm = new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> .owners:after { content: ""; clear: both; display: block; } .owners > div { float: left; margin: 20px; padding: 20px; } .container-fluid .wj-flexgrid { max-height: 250px; } .container-fluid .wj-flexchart { max-height: 250px; } .wj-content.ctx-menu { padding: 3px; min-width: 120px; background: #FFFBDD; overflow: hidden; } .ctx-menu .wj-listbox-item { text-align: center; margin: 6px; } 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 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> // create some random data export function getData() { let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i % countries.length], sales: Math.random() * 100000, expenses: Math.random() * 50000 }); } // return data; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './app.css'; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; // import * as wijmo from '@grapecity/wijmo'; import * as wjInput from '@grapecity/wijmo.react.input'; import * as wjChart from '@grapecity/wijmo.react.chart'; import * as wjGrid from '@grapecity/wijmo.react.grid'; import { getData } from './data'; // class App extends React.Component { constructor() { super(...arguments); this._data = getData(); this._menuSource = [ { header: '<span class="glyphicon glyphicon-asterisk"></span>&nbsp;&nbsp;New', cmd: 'cmdNew' }, { header: '<span class="glyphicon glyphicon-folder-open"></span>&nbsp;&nbsp;Open', cmd: 'cmdOpen' }, { header: '<span class="glyphicon glyphicon-floppy-disk"></span>&nbsp;&nbsp;Save', cmd: 'cmdSave' }, { header: '<span class="wj-separator"></span>' }, { header: '<span class="glyphicon glyphicon-remove"></span>&nbsp;&nbsp;Exit', cmd: 'cmdExit' } ]; } componentDidMount() { // use it as a context menu for one or more elements let els = document.querySelectorAll('.has-ctx-menu'); for (let i = 0; i < els.length; i++) { els[i].addEventListener('contextmenu', e => { this._menu.owner = wijmo.closest(e.target, '.has-ctx-menu'); if (this._menu.owner) { e.preventDefault(); this._menu.show(e); } }, true); } } render() { return <div className="container-fluid"> <div className="owners"> <div id="first" className="has-ctx-menu" style={{ background: '#f0a0a0' }}> I have a Context Menu. </div> <div id="second" className="has-ctx-menu" style={{ background: '#a0f0a0' }}> I have the same Context Menu. </div> </div> <h4> The same approach works with all Wijmo controls: </h4> <p> FlexChart with ContextMenu </p> <wjChart.FlexChart id="theChart" className="has-ctx-menu" itemsSource={this._data} bindingX="country"> <wjChart.FlexChartSeries binding="sales" name="Sales"> </wjChart.FlexChartSeries> <wjChart.FlexChartSeries binding="expenses" name="Expenses"> </wjChart.FlexChartSeries> </wjChart.FlexChart> <p> FlexGrid with ContextMenu </p> <wjGrid.FlexGrid id="theGrid" className="has-ctx-menu" itemsSource={this._data}> </wjGrid.FlexGrid> <wjInput.Menu style={{ display: 'none' }} header="Context Menu" selectedValuePath="cmd" initialized={this._initMenu.bind(this)} dropDownCssClass="ctx-menu" itemClicked={this._menuItemClicked.bind(this)} itemsSource={this._menuSource} displayMemberPath="header"> </wjInput.Menu> </div>; } _initMenu(sender) { this._menu = sender; } _menuItemClicked(sender) { alert('Executing **' + sender.selectedValue + '** for element **' + sender.owner.id + '**'); } } 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> .owners:after { content: ""; clear: both; display: block; } .owners > div { float: left; margin: 20px; padding: 20px; } .wj-flexgrid { max-height: 250px; } .wj-flexchart { max-height: 250px; } .ctx-menu { padding: 3px; min-width: 120px; background: #FFFBDD; overflow: hidden; } .ctx-menu .wj-listbox-item { text-align: center; margin: 6px; } body { margin-bottom: 24pt; } export function getData() { let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i % countries.length], sales: Math.random() * 100000, expenses: Math.random() * 50000 }); } // return data; }