Menu Commands

Menu commands are created using the command property, which is an object that expects two function properties: canExecuteCommand and executeCommand. This sample shows a Menu control with commands that controls the the incremental or decremental value of an InputNumber control.

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'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // current tax value let currentTax = new input.InputNumber('#currentTax', { format: 'p2', min: 0, max: 1, step: 0.025, value: 0.0825 }); // // create change tax menu let changeTax = createMenu('changeTax'); // // set command object for the tax menu changeTax.command = { // execute the command executeCommand: (arg) => { arg = wijmo.changeType(arg, wijmo.DataType.Number, null); if (wijmo.isNumber(arg)) { currentTax.value += arg; } }, // // check if a command can be executed canExecuteCommand: (arg) => { arg = wijmo.changeType(arg, wijmo.DataType.Number, null); if (wijmo.isNumber(arg)) { let val = currentTax.value + arg; return val >= 0 && val <= 1; } return false; } }; // function createMenu(elementId) { // get host element, header, items let host = document.getElementById(elementId), header = host.firstChild.textContent.trim(), items = host.querySelectorAll('div'), menuItems = []; // for (let i = 0; i < items.length; i++) { let item = items[i]; menuItems.push({ header: item.outerHTML, cmdParam: item.getAttribute('cmd-param') }); } // // clear host and instantiate menu host.innerHTML = ''; let menu = new input.Menu(host, { header: header, displayMemberPath: 'header', commandParameterPath: 'cmdParam', itemsSource: menuItems, }); // // done, return menu return menu; } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Menu Commands</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="form-group"> <label for="currentTax">Current Tax</label> <div id="currentTax"></div> </div> <div class="form-group"> <label for="changeTax">Change Tax</label> <div id="changeTax"> Tax Commands <div cmd-param=".50">Increment by 50%</div> <div cmd-param=".25">Increment by 25%</div> <div cmd-param=".05">Increment by 5%</div> <div class="wj-separator"></div> <div cmd-param="-.05">Decrement by 5%</div> <div cmd-param="-.25">Decrement by 25%</div> <div cmd-param="-.50">Decrement by 50%</div> </div> </div> </div> </body> </html> label { width: 120px; text-align: right; margin-right: 3px; } body { margin-bottom: 24pt; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wijmo from '@grapecity/wijmo'; // import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjInputModule } from '@grapecity/wijmo.angular2.input'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { tax = 0.0825; command = { // execute the command executeCommand: (arg: any) => { if (wijmo.isNumber(arg)) { this.tax += arg; } }, // // check if a command can be executed canExecuteCommand: (arg: any) => { if (wijmo.isNumber(arg)) { let val = this.tax + arg; return val >= 0 && val <= 1; } return false; } }; } // @NgModule({ imports: [WjInputModule, BrowserModule], declarations: [AppComponent], 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 Menu Commands</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="form-group"> <label for="currentTax">Current Tax</label> <wj-input-number id="currentTax" [format]="'p2'" [min]="0" [max]="1" [step]="0.025" [(value)]="tax"> </wj-input-number> </div> <div class="form-group"> <label for="changeTax">Change Tax</label> <wj-menu id="changeTax" [header]="'Tax Commands'"> <wj-menu-item [cmd]="command" [cmdParam]="0.50">Increment by 50%</wj-menu-item> <wj-menu-item [cmd]="command" [cmdParam]="0.25">Increment by 25%</wj-menu-item> <wj-menu-item [cmd]="command" [cmdParam]="0.05">Increment by 5%</wj-menu-item> <wj-menu-separator></wj-menu-separator> <wj-menu-item [cmd]="command" [cmdParam]="-0.05">Decrement by 5%</wj-menu-item> <wj-menu-item [cmd]="command" [cmdParam]="-0.25">Decrement by 25%</wj-menu-item> <wj-menu-item [cmd]="command" [cmdParam]="-0.50">Decrement by 50%</wj-menu-item> </wj-menu> </div> </div> label { width: 120px; text-align: right; margin-right: 3px; } body { margin-bottom: 24pt; } <template> <div class="container-fluid"> <div class="form-group"> <label for="currentTax">Current Tax</label> <wj-input-number id="currentTax" :format="'p2'" :min="0" :max="1" :step="0.025" :value="tax"> </wj-input-number> </div> <div class="form-group"> <label for="changeTax">Change Tax</label> <wj-menu id="changeTax" :header="'Tax Commands'" :displayMemberPath="'header'" :commandParameterPath="'cmdParam'" :itemsSource="itemSource" :initialized="initMenu"> </wj-menu> </div> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.input'; let App = Vue.extend({ name: 'app', data: function(){ return { tax: 0.0825, theMenu: null, itemSource: [ {header: '<div cmd-param=".50">Increment by 50%</div>', cmdParam: ".50"}, {header: '<div cmd-param=".25">Increment by 25%</div>', cmdParam: ".25"}, {header: '<div cmd-param=".05">Increment by 5%</div>', cmdParam: ".05"}, {header: '<div class="wj-separator"></div>', cmdParam: null}, {header: '<div cmd-param="-.05">Decrement by 5%</div>', cmdParam: "-.05"}, {header: '<div cmd-param="-.25">Decrement by 25%</div>', cmdParam: "-.25"}, {header: '<div cmd-param="-.50">Decrement by 50%</div>', cmdParam: "-.50"} ], command: { // execute the command executeCommand: (arg) => { arg = wijmo.changeType(arg, wijmo.DataType.Number, null); if (wijmo.isNumber(arg)) { this.tax += arg; } }, // check if a command can be executed canExecuteCommand: (arg) => { arg = wijmo.changeType(arg, wijmo.DataType.Number, null); if (wijmo.isNumber(arg)) { let val = this.tax + arg; return val >= 0 && val <= 1; } return false; } } } }, methods: { initMenu: function(menu){ this.theMenu = menu; } }, mounted: function(){ // set command object for the tax menu this.theMenu.command = this.command; } }) let vm = new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> label { width: 120px; text-align: right; margin-right: 3px; } 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 Menu Commands</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> 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'; // class App extends React.Component { constructor(props) { super(props); this.state = { tax: 0.0825, itemSource: [ { header: '<div cmd-param=".50">Increment by 50%</div>', cmdParam: ".50" }, { header: '<div cmd-param=".25">Increment by 25%</div>', cmdParam: ".25" }, { header: '<div cmd-param=".05">Increment by 5%</div>', cmdParam: ".05" }, { header: '<div class="wj-separator"></div>', cmdParam: null }, { header: '<div cmd-param="-.05">Decrement by 5%</div>', cmdParam: "-.05" }, { header: '<div cmd-param="-.25">Decrement by 25%</div>', cmdParam: "-.25" }, { header: '<div cmd-param="-.50">Decrement by 50%</div>', cmdParam: "-.50" } ], command: { // execute the command executeCommand: (arg) => { arg = wijmo.changeType(arg, wijmo.DataType.Number, null); if (wijmo.isNumber(arg)) { let tax = this.state.tax; this.setState({ tax: tax += arg }); } }, // check if a command can be executed canExecuteCommand: (arg) => { arg = wijmo.changeType(arg, wijmo.DataType.Number, null); if (wijmo.isNumber(arg)) { let val = this.state.tax + arg; return val >= 0 && val <= 1; } return false; } } }; } render() { return <div className="container-fluid"> <div className="form-group"> <label htmlFor="currentTax">Current Tax</label> <wjInput.InputNumber id="currentTax" format="p2" min={0} max={1} step={0.025} value={this.state.tax}> </wjInput.InputNumber> </div> <div className="form-group"> <label htmlFor="changeTax">Change Tax</label> <wjInput.Menu header="Tax Commands" id="changeTax" displayMemberPath="header" commandParameterPath="cmdParam" command={this.state.command} itemsSource={this.state.itemSource}> </wjInput.Menu> </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> label { width: 120px; text-align: right; margin-right: 3px; } body { margin-bottom: 24pt; }