Angular Data TreeList Context Menu
The TreeList allows you to display a built-in context menu by using the ContextMenu component.
The context menu provides a convenient way to apply data processing features to specific rows or to the TreeList component. The user can add, edit, and delete records, reorder rows, or export the component to a PDF or an Excel file. You can customize the order of the menu items and create custom controls as necessary.
The following example demonstrates how to show a custom ContextMenu when using the kendoTreeListFlatBinding directive.
Setup
To display a custom ContextMenu on the user's right click when binding the data through the kendoTreeListFlatBinding directive, the kendoTreeListHierarchyBinding directive or the data property:
-
Get the
ContextMenuinstance through a@ViewChilddecorator inside the component code.ts@ViewChild('contextmenu') public contextMenu: any; -
To prevent the default browser context menu, handle the built-in
cellClickevent and use thepreventDefault()method.tspublic onCellClick(event: any) { if (event.type === 'contextmenu') { const originalEvent = event.originalEvent; originalEvent.preventDefault(); ... } } -
To display the menu on the exact row the user has clicked on, use the built-in
show()method of theContextMenuand the coordinates that thecellClickevent returns.tsthis.contextMenu.contextMenu.show({ left: originalEvent.pageX, top: originalEvent.pageY, }); -
Handle the
selectevent of theContextMenucomponent and determine the data operation that should be executed.tspublic onSelect({ item }): void { const menuItem = item.text; switch(menuItem) { case 'Add': this.addHandler.emit(); break; case 'Edit': this.editHandler.emit(); break; ... } }