|
| 1 | +--- |
| 2 | +title: Show a context menu |
| 3 | +page_title: Show a context menu |
| 4 | +description: Show a context menu |
| 5 | +--- |
| 6 | + |
| 7 | +# Show a context menu |
| 8 | + |
| 9 | +The example below demonstrates how to show a context menu for the TreeList rows |
| 10 | + |
| 11 | +#### Example: |
| 12 | + |
| 13 | +```html |
| 14 | + <div id="treelist"></div> |
| 15 | + |
| 16 | + |
| 17 | + <ul id="menu"> |
| 18 | + <li><span class='k-icon k-add'></span>Add</li> |
| 19 | + <li><span class='k-icon k-delete'></span>Delete</li> |
| 20 | + </ul> |
| 21 | + |
| 22 | + <script> |
| 23 | + $(document).ready(function () { |
| 24 | + var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service"; |
| 25 | +
|
| 26 | + var dataSource = new kendo.data.TreeListDataSource({ |
| 27 | + transport: { |
| 28 | + read: { |
| 29 | + url: crudServiceBaseUrl + "/EmployeeDirectory/All", |
| 30 | + dataType: "jsonp" |
| 31 | + }, |
| 32 | + update: { |
| 33 | + url: crudServiceBaseUrl + "/EmployeeDirectory/Update", |
| 34 | + dataType: "jsonp" |
| 35 | + }, |
| 36 | + destroy: { |
| 37 | + url: crudServiceBaseUrl + "/EmployeeDirectory/Destroy", |
| 38 | + dataType: "jsonp" |
| 39 | + }, |
| 40 | + create: { |
| 41 | + url: crudServiceBaseUrl + "/EmployeeDirectory/Create", |
| 42 | + dataType: "jsonp" |
| 43 | + }, |
| 44 | + parameterMap: function(options, operation) { |
| 45 | + if (operation !== "read" && options.models) { |
| 46 | + return {models: kendo.stringify(options.models)}; |
| 47 | + } |
| 48 | + } |
| 49 | + }, |
| 50 | + batch: true, |
| 51 | + schema: { |
| 52 | + model: { |
| 53 | + id: "EmployeeId", |
| 54 | + fields: { |
| 55 | + EmployeeId: { type: "number", editable: false, nullable: false }, |
| 56 | + parentId: { field: "ReportsTo", nullable: true }, |
| 57 | + FirstName: { validation: { required: true } }, |
| 58 | + LastName: { validation: { required: true } }, |
| 59 | + HireDate: { type: "date" }, |
| 60 | + Phone: { type: "string" }, |
| 61 | + HireDate: { type: "date" }, |
| 62 | + BirthDate: { type: "date" }, |
| 63 | + Extension: { type: "number", validation: { min: 0, required: true } }, |
| 64 | + Position: { type: "string" } |
| 65 | + }, |
| 66 | + expanded: true |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | +
|
| 71 | + $("#treelist").kendoTreeList({ |
| 72 | + dataSource: dataSource, |
| 73 | + toolbar: [ "create" ], |
| 74 | + editable: true, |
| 75 | + height: 540, |
| 76 | + columns: [ |
| 77 | + { field: "FirstName", expandable: true, title: "First Name", width: 220 }, |
| 78 | + { field: "LastName", title: "Last Name", width: 100 }, |
| 79 | + { field: "Position" }, |
| 80 | + { field: "HireDate", title: "Hire Date", format: "{0:MMMM d, yyyy}" }, |
| 81 | + { field: "Phone", title: "Phone" }, |
| 82 | + { field: "Extension", title: "Ext", format: "{0:#}" } |
| 83 | + ] |
| 84 | + }); |
| 85 | +
|
| 86 | +
|
| 87 | + $("#menu").kendoContextMenu({ |
| 88 | + // listen to right-clicks on the treelist container |
| 89 | + target: "#treelist", |
| 90 | +
|
| 91 | + // show when a row is clicked |
| 92 | + filter: "tbody > tr", |
| 93 | +
|
| 94 | + // handle item clicks |
| 95 | + select: function(e) { |
| 96 | + var button = $(e.item); |
| 97 | + var row = $(e.target); |
| 98 | + var dataItem = $("#treelist").data("kendoTreeList").dataItem(row); |
| 99 | +
|
| 100 | + alert(kendo.format("'{0}' button clicked on '{1}'", button.text(), dataItem.FirstName)); |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + </script> |
| 105 | +``` |
0 commit comments