You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Menu component allows you to define a custom template for its items. This article explains how you can use it.
13
+
The Menu component allows you to define a custom template for its items. This article explains how to use it.
14
14
15
-
The `ItemTemplate` of an item is defined under the `ItemTemplate` tag of the menu.
15
+
## ItemTemplate
16
16
17
-
The template receives the model to which the item is bound as its `context`. You can use it to render the desired content. The menu is a generic component, so you can use a named context variable that will be of the model type without additional casting.
17
+
The template of all items is defined in the `ItemTemplate` tag of the Menu.
18
18
19
-
You can use the template to render arbitrary content according to your application's data and logic. You can use components in it and thus provide rich content instead of plain text. You can also use it to add DOM event handlers like click, doubleclick, mouseover if you need to respond to them.
19
+
The template receives the respective Menu data item as its `context`. You can use it to render the desired content. You can also set the `Context` parameter of the `ItemTemplate` tag and use a [named context variable. This is useful in nested template scenarios]().
20
20
21
-
>caption Use templates to implement navigation between views without the UrlField feature
21
+
The Menu item template can contain arbitrary content according such as HTML markup and other components. You can also use standard event handlers like `@onclick` or `@onmouseover`.
22
+
23
+
## Examples
24
+
25
+
### Use ItemTemplate for Navigation
26
+
27
+
The following example shows how to render `<NavLink>` tags inside the Menu and use them for navigation instead of the [built-in Menu navigation mechanism]({%slug menu-navigation%}). This approach requires the URL property name to be different from `Url`. In addition to rendering customization, `<NavLink>` also supports the `target="_blank"` attribute.
28
+
29
+
>caption Use Menu item template for navigation
22
30
23
31
````CSHTML
24
-
Use your own NavLink elements for navigation instead of the built-in feature of the menu
32
+
<h3>Menu with ItemTemplate</h3>
25
33
26
-
<TelerikMenu Data="@MenuItems"
27
-
ItemsField="@nameof(MenuItem.SubSectionList)">
34
+
<TelerikMenu Data="@MenuItems">
28
35
<ItemTemplate Context="item">
29
36
@{
30
-
var shouldNavigate = !string.IsNullOrEmpty(item.Page);
37
+
@*
38
+
k-menu-link-text will make the NavLink similar to built-in Menu items.
39
+
You also need to reset the underline and text color.
40
+
Do not render an SvgIcon component if item.Icon is null.
41
+
*@
42
+
43
+
var shouldNavigate = !string.IsNullOrEmpty(item.Href);
44
+
31
45
if (shouldNavigate)
32
46
{
33
-
@*k-menu-link-text will expand the NavLink to match default Menu styling*@
0 commit comments