-
Data Grids / Data Management
-
Data Grid
- Overview
-
Data Binding
-
Filtering
- Sorting
-
Editing
-
Grouping
-
Selection
- Focused Row
- Paging
-
Scrolling
-
Columns
-
Master-Detail
-
Data Summaries
-
Drag & Drop
-
Export to PDF
-
Export to Excel
- Appearance
-
Customization
- State Persistence
-
Adaptability
-
Keyboard Navigation
- Right-To-Left Support
-
Tree List
- Overview
-
Data Binding
-
Filtering
- Sorting
-
Editing
-
Selection
- Focused Row
- Paging
-
Columns
- Drag & Drop
- State Persistence
- Adaptability
-
Keyboard Navigation
-
Card View
-
Pivot Grid
- Overview
-
Data Binding
-
Field Management
-
Data Summaries
- Drill Down
- Filtering
-
Scrolling
-
Export to Excel
- Chart Integration
- Customization
- State Persistence
-
Filter Builder
-
-
Data Visualization
-
Charts
- Overview
-
Data Binding
-
Common Concepts
-
Axis
-
Aggregation
-
Tooltips
-
Selection
-
Customization
-
Zooming
-
Export
-
-
Area Charts
-
Bar Charts
- Bullet Charts
-
Doughnut Charts
-
Financial Charts
-
Funnel and Pyramid Charts
-
Line Charts
- Pareto Chart
-
Pie Charts
-
Point Charts
-
Polar and Radar Charts
-
Range Charts
- Sankey Chart
-
Sparkline Charts
-
Tree Map
-
Gauges
- Overview
-
Runtime update
-
Bar Gauge
-
Circular Gauge
-
Linear Gauge
-
Diagram
- Overview
-
Data Binding
-
Featured Shapes
-
Custom Shapes
-
Document Capabilities
-
User Interaction
- UI Customization
- Adaptability
-
-
Scheduling / Planning
-
Scheduler
- Overview
-
Data Binding
-
Views
-
Appointments
-
Timetable
- Editing
-
Grouping
- Virtual Scrolling
- Drag & Drop
-
Customization
- Adaptability
-
Gantt
- Overview
- Data Binding
-
Filtering
- Sorting
- Strip Lines
- Export to PDF
- Validation
-
Customization
-
-
Reporting
-
AI-powered Extensions
-
Interaction
-
Report Types
-
Data binding
-
Real-life Reports
-
Layout Features
-
Report Controls
-
Web-specific Features
-
-
Rich Text Editor
- Overview
- Load/Save
- Document Protection
-
Templates
- Autocorrect
-
Customization
- Simple View
-
Spreadsheet
- Overview
-
Open a Document
- Export And Printing
-
Features
-
UI Customization
-
Messaging
-
WYSIWYG Editor
-
Forms
-
Data Editors
- Overview
-
Common Concepts
-
Calendar
- Check Box
- Color Box
- Date Box
-
Date Range Box
-
Number Box
- Radio Group
-
Range Selector
- Range Slider
- Slider
- Switch
- Text Area
- Text Box
-
Drop-Downs
- Autocomplete
-
Drop Down Box
-
Select Box
-
Tag Box
-
Lookup
-
Buttons
-
File Upload / File Management
-
File Manager
- Overview
-
File System Types
-
Customization
-
File Uploader
-
-
Popup and Notifications
-
Navigation
- Overview
- Accordion
-
Context Menu
-
Menu
- Multi View
-
Drawer
-
Tab Panel
-
Tabs
-
Toolbar
-
Stepper
- Pagination
-
List
-
Tree View
- Right-to-Left Support
-
Layout
-
Tile View
- Splitter
-
Gallery
- Scroll View
-
-
Interactive Wrappers
-
Sortable
- Resizable
-
-
Progress Indicators
-
Maps
- Overview
-
Map
-
Vector Map
-
Data Binding
- Multiple Layers
-
Markers
- Legend
-
Zooming and Panning
-
Customization
-
-
Localization
Related Demos:
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;
})();