Your search did not match any results.

Spreadsheet - Context Menu

This demo shows how to implement a custom context menu and display it for specified sheet cells only.

To populate the context menu with custom items, write the OnPopupMenuShowing event handler. You can add different items to the menu based on the cell data type (such as weight, currency, or discount values). Use the OnCustomCommandExecuted event to process an item click. The commandName event argument property allows you to identify the clicked item.

Backend API
@inject IWebHostEnvironment env <script src="~/DemosScripts/ContextMenuCustomization.js" asp-append-version="true"></script> <div id="popoverContainer" class="custom-popover"> </div> @{ var documentPath = System.IO.Path.Combine( env.ContentRootPath, DirectoryManagmentUtils.GetDocumentSampleFolderPath(Context), "ContextMenu.xlsx"); } @(Html.DevExpress() .Spreadsheet("spreadsheet") .Height("500px") .Width("100%") .ConfirmOnLosingChanges(confirm => confirm.Enabled(false)) .DocumentRequestHandlerUrl(Url.Action("DxDocumentRequest")) .Open(documentPath) .ClientSideEvents(events => { events.OnPopupMenuShowing("SpreadsheetPopupMenuShowing"); events.OnCustomCommandExecuted("SpreadsheetCustomCommandExecuted"); }) ) <script> $("#popoverContainer").dxPopover({ position: "right", maxWidth: 230 }); function getContextMenuCustomData(onSuccess) { var spreadsheetState = spreadsheet.getSpreadsheetState(); $.ajax({ type: 'POST', url: '@Url.Action("GetContextMenuCustomData")', data: { spreadsheetState: spreadsheetState, rowIndex: spreadsheet.getSelection().activeCellRowIndex }, success:function(data) { onSuccess(data); } }); } </script>
using System; using System.IO; using System.Linq; using System.Text; using DevExpress.AspNetCore.Spreadsheet; using DevExpress.Spreadsheet; using DevExpress.XtraSpreadsheet.Export; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; namespace AspNetCoreDemos.Spreadsheet { [Route("[action]")] public class SpreadsheetController : Controller { public ActionResult ContextMenuCustomization() { return View(); } [HttpPost] public IActionResult GetContextMenuCustomData(SpreadsheetClientState spreadsheetState, int rowIndex) { var spreadsheet = SpreadsheetRequestProcessor.GetSpreadsheetFromState(spreadsheetState); Worksheet activeWorksheet = spreadsheet.Document.Worksheets.ActiveWorksheet; rowIndex = rowIndex + 1; var discountInfoRange = activeWorksheet.Range["J" + rowIndex + ":N" + rowIndex]; string resultFormat = "{0} to {4} units - {1}, <br /> {2} units or more - {3}"; string[] cellValues = discountInfoRange.ExistingCells.Select((c, index) => (index % 2 == 0) ? c.Value.ToString() : c.DisplayText).ToArray(); return Ok(string.Format(resultFormat, cellValues)); } } }
(function() { var minRowIndex = 9, maxRowIndex = 17; var currencyColumns = [3, 5, 6, 7], discountColumns = [6], weightColumns = [2]; var convertCurrencyCommand = { name: 'ConvertCurrency', text: 'Get Value In Euros', conversionRate: 0.87 }, discountCommand = { name: 'GetDiscountStructure', text: 'How the discount is calculated?' }, convertUnitsCommand = { name: 'ConvertUnits', text: 'Get Value In Lbs', conversionRate: 2.20462 }; function showPopoverAtElement(element, contentText) { $('#popoverContainer').dxPopover('show', element); $('#popoverContainer').dxPopover('instance').content().html('<div class="custom-content">' + contentText + '</div>'); $('#popoverContainer').dxPopover('repaint'); } function SpreadsheetPopupMenuShowing(s, e) { if(e.menuType === DevExpress.AspNetCore.Spreadsheet.PopupMenuType.Cell) { e.menuItems.Clear(); if(needToAddCustomMenuItems(s)) { if(isSelectedCellOfType(s, currencyColumns)) addCustomMenuItem(e.menuItems, convertCurrencyCommand); if(isSelectedCellOfType(s, discountColumns)) addCustomMenuItem(e.menuItems, discountCommand); if(isSelectedCellOfType(s, weightColumns)) addCustomMenuItem(e.menuItems, convertUnitsCommand); } } else if(e.menuType !== DevExpress.AspNetCore.Spreadsheet.PopupMenuType.AutoFilter) e.cancel = true; } function SpreadsheetCustomCommandExecuted(s, e) { if(e.commandName === convertCurrencyCommand.name || e.commandName === convertUnitsCommand.name) { var command = e.commandName === convertCurrencyCommand.name ? convertCurrencyCommand : convertUnitsCommand; showPopoverAtElement(getActiveCellElement(s), convertActiveCellValue(s, command)); } else if(e.commandName === discountCommand.name) { getContextMenuCustomData(function(contentText) { showPopoverAtElement(getActiveCellElement(s), contentText); }); } } function needToAddCustomMenuItems(spreadsheet) { var selection = spreadsheet.GetSelection(); return isSingleCellSelected(selection) && isSelectionWithinTable(selection) && spreadsheet.GetActiveCellValue() !== null; } function isSingleCellSelected(selection) { return selection.leftColumnIndex === selection.rightColumnIndex && selection.topRowIndex === selection.bottomRowIndex; } function isSelectionWithinTable(selection) { return selection.activeCellRowIndex >= minRowIndex && selection.activeCellRowIndex <= maxRowIndex; } function isSelectedCellOfType(spreadsheet, typedColumnsIndices) { var selection = spreadsheet.GetSelection(); return typedColumnsIndices.indexOf(selection.activeCellColumnIndex) >= 0 } function addCustomMenuItem(menuItems, command) { var item = new DevExpress.AspNetCore.Spreadsheet.PopupMenuItem(command.name, command.text, null, null); menuItems.Add(item); } function getActiveCellElement(spreadsheet) { var renderHelper = spreadsheet.getRenderHelper(); return renderHelper.getActiveCellElement(); } function convertActiveCellValue(spreadsheet, command) { var convertedValue = convertNumericValue(getActiveCellNumericValue(spreadsheet), command); if(command.name === convertUnitsCommand.name) convertedValue = convertedValue + ' lbs'; else convertedValue = '€' + convertedValue; return convertedValue; } function getActiveCellNumericValue(spreadsheet) { return spreadsheet.GetActiveCellValue().replace(/\$|(?:\skg)/g, ''); } function convertNumericValue(value, conversionCommand) { return Math.round(value * conversionCommand.conversionRate * 100) / 100; } window.SpreadsheetPopupMenuShowing = SpreadsheetPopupMenuShowing; window.SpreadsheetCustomCommandExecuted = SpreadsheetCustomCommandExecuted; })();