Custom MenuView

This sample shows how you can customize the built-in context menu.

<p>You can rewrite the defined context menu style class to apply different styles. For example:</p> <pre><code class="hljs js language-js"> .gc-ui-contextmenu-group-header { font-weight:normal; background-color:none; } </code></pre> <p>You can customize the menu item's view structure by overwriting the MenuView's <strong>createMenuItemElement</strong> function:</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> spread = <span class="hljs-keyword">new</span> GC.Spread.Sheets.Workbook(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'ss'</span>)); <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">CustomMenuView</span>(<span class="hljs-params"></span>) </span>{} CustomMenuView.prototype = <span class="hljs-keyword">new</span> GC.Spread.Sheets.ContextMenu.MenuView(); CustomMenuView.prototype.createMenuItemElement = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">menuItemData</span>) </span>{ <span class="hljs-comment">// create menu item view by your self</span> <span class="hljs-comment">// you can call super's createMenuItemElement here and only customize a few of menu item</span> <span class="hljs-comment">// should return menu item view back</span> }; spread.contextMenu.menuView = <span class="hljs-keyword">new</span> CustomMenuView(); </code></pre> <p>You can overwrite the MenuView's <strong>getCommandOptions</strong> function to show the options when the command is executed:</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> spread = <span class="hljs-keyword">new</span> GC.Spread.Sheets.Workbook(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'ss'</span>)); <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">CustomMenuView</span>(<span class="hljs-params"></span>) </span>{} CustomMenuView.prototype = <span class="hljs-keyword">new</span> GC.Spread.Sheets.ContextMenu.MenuView(); CustomMenuView.prototype.getCommandOptions = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">menuItemData, host, event</span>) </span>{ <span class="hljs-comment">// For most of the menu items, only the selections on sheets or the active sheet’s name are needed</span> <span class="hljs-comment">// but there may be some menu items need special parameters,like color picker,change color</span> <span class="hljs-comment">// To change colors, the command needs to know which color the user clicked on, so overwrite this function and return this</span> <span class="hljs-comment">// parameter's value here.</span> }; spread.contextMenu.menuView = <span class="hljs-keyword">new</span> CustomMenuView(); </code></pre>
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 2}); initEvents(spread); }; function initEvents(spread) { document.getElementById('addBackgroundColorMenuSelection').onchange = function () { if (isMenuItemExist(spread.contextMenu.menuData, "selectColorWithBg")) { spread.contextMenu.menuData.forEach(function (item, index) { if (item && item.name === "selectColorWithBg") { spread.contextMenu.menuData.splice(index, 1); } }); return; } var commandManager = spread.commandManager(); var selectWithABackgroundColor = { text: "Select Color", name: "selectColorWithBg", workArea: "viewport", subMenu: [ { name: "selectColorPicker", command: "selectWithBg" } ] }; spread.contextMenu.menuData.push(selectWithABackgroundColor); var selectWithABackgroundColorCommand = { canUndo: false, execute: function (spread, options) { if (options.commandOptions) { var style = new GC.Spread.Sheets.Style(); style.name = 'style1'; style.backColor = options.commandOptions; var sheet = spread.getActiveSheet(); sheet.suspendPaint(); var selections = sheet.getSelections(); var selectionIndex = 0, selectionCount = selections.length; for (; selectionIndex < selectionCount; selectionIndex++) { var selection = selections[selectionIndex]; for (var i = selection.row; i < (selection.row + selection.rowCount); i++) { for (var j = selection.col; j < (selection.col + selection.colCount); j++) { sheet.setStyle(i, j, style, GC.Spread.Sheets.SheetArea.viewport); } } } sheet.resumePaint(); } } }; commandManager.register("selectWithBg", selectWithABackgroundColorCommand, null, false, false, false, false); function CustomMenuView() { } CustomMenuView.prototype = new GC.Spread.Sheets.ContextMenu.MenuView(); CustomMenuView.prototype.createMenuItemElement = function (menuItemData) { var self = this; if (menuItemData.name === "selectColorPicker") { var containers = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(self, menuItemData); var supMenuItemContainer = containers[0]; while (supMenuItemContainer.firstChild) { supMenuItemContainer.removeChild(supMenuItemContainer.firstChild); } var colorPicker = createColorpicker(); supMenuItemContainer.appendChild(colorPicker); return supMenuItemContainer; } else { var menuItemView = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(self, menuItemData); return menuItemView; } }; CustomMenuView.prototype.getCommandOptions = function (menuItemData, host, event) { if (menuItemData && menuItemData.name === "selectColorPicker") { var ele = event.target || event.srcElement; return ele.style.backgroundColor; } }; spread.contextMenu.menuView = new CustomMenuView(); }; var colors = ['rgb(255,255,255)', 'rgb(0,255,255)', 'rgb(255,0,255)', 'rgb(255,255,0)', 'rgb(255,0,0)', 'rgb(0,255,0)', 'rgb(0,0,255)', 'rgb(0,0,0)']; function createColorpicker() { var colorPicker = document.createElement('div'); colorPicker.className = 'colorPickerContent'; for (var j = 0; j < 8; j++) { var colorDom = document.createElement("div"); colorDom.className = 'colorDom'; colorDom.style['backgroundColor'] = colors[j]; colorPicker.appendChild(colorDom); } return colorPicker; } } function isMenuItemExist(menuData, menuItemName) { var i = 0, count = menuData.length; for (; i < count; i++) { if (menuItemName === menuData[i].name) { return true; } } }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <div class="option-row"> Right click any cell to being up the context menu.You can easily modify the context menu depending on your needs. Select the option below to add a new context menu item that will allow the user to change the background color of the cell.<br/> <div class="option-row"> <input id="addBackgroundColorMenuSelection" type="checkbox" /> <label for="addBackgroundColorMenuSelection">Add background color menu selection</label> </div> </div> </div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; margin-top: 10px; } label { margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; } .buttonStyle{ width:240px; height:30px; } .colorPickerContent{ width: 100%; background-color:white; } .colorDom{ width: 14px; height: 14px; margin:0 0 0 6px; display: inline-block; border: solid 1px #333333; vertical-align: top; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }