JavaScript/jQuery Menu - Getting Started
This tutorial shows how to add the Menu component to your application and configure the component's core features.
Each section in this tutorial describes a single configuration step. You can also find the full source code in the GitHub repository.
Create Base Menu Level
You can display Menu items from the items array or a data source. This tutorial uses the first technique. To see how to use the dataSource, refer to the following demo: Menu Overview Demo.
To create a base Menu level, define the component in the markup and populate it with items one by one. You can use predefined item properties to customize the items. For example, the code example below uses icon and text item properties. To further customize the appearance and content of items, use an itemTemplate to customize all items simultaneously or the item template property to customize individual items.
- $(function() {
- const menu = $("#menu").dxMenu({
- items: [
- {
- icon: 'home'
- },
- {
- text: 'About'
- },
- {
- text: 'Products'
- },
- {
- icon: 'cart'
- }
- ]
- }).dxMenu('instance');
- });
- <html>
- <head>
- <!-- ... -->
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
- <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/24.2.3/css/dx.light.css">
- <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/24.2.3/js/dx.all.js"></script>
- <script type="text/javascript" src="index.js"></script>
- <link rel="stylesheet" type="text/css" href="index.css">
- </head>
- <body>
- <div id="container">
- <div id="menu"></div>
- </div>
- </body>
- </html>
Create Submenus
To create a submenu, use nested items. An array within the base item object corresponds to a submenu.
Use the beginGroup property to separate items in the submenu.
- $(function() {
- const menu = $("#menu").dxMenu({
- items: [
- // ...
- {
- text: 'Products',
- items: [
- {
- text: 'Product 1',
- },
- {
- text: 'Category',
- items: [
- {
- text: 'Product 2'
- },
- {
- beginGroup: true,
- text: 'Product 3'
- },
- {
- text: 'Product 4'
- }
- ]
- },
- {
- disabled: true,
- text: 'Product 5'
- }
- ]
- },
- // ...
- ]
- }).dxMenu('instance');
- });
Handle Clicks on Items
To access the clicked item, use the onItemClick event handler function. The following code displays the clicked item's name in the console:
- $(function() {
- const menu = $("#menu").dxMenu({
- // ...
- onItemClick: function(e) {
- if (e.itemData.text) {
- console.log(e.itemData.text + ' has been clicked!');
- }
- else if (e.itemData.icon) {
- console.log(e.itemData.icon.charAt(0).toUpperCase() + e.itemData.icon.slice(1) + ' has been clicked!');
- }
- }
- }).dxMenu('instance');
- });
Enable Menu Adaptivity
In adaptive render mode, the Menu is shown as a list icon, and Menu items are arranged hierarchically like TreeView items. This functionality is in effect only if the container's width is less than the Menu width. Set the adaptivityEnabled property to true to enable adaptive rendering.
In the code below, the CheckBox toggles Menu render mode.
- $(function() {
- const menu = $("#menu").dxMenu({
- // ...
- }).dxMenu('instance');
- $("#checkbox").dxCheckBox({
- text: 'Enable adaptivity',
- onValueChanged: function(e) {
- menu.option('adaptivityEnabled', e.value)
- }
- });
- });
- <html>
- <head>
- <!-- ... -->
- </head>
- <body>
- <div id="container">
- <div id="menu"></div>
- </div>
- <div id="checkbox"></div>
- </body>
- </html>
- #container {
- width: 200px;
- height: 140px;
- }
If you have technical questions, please create a support ticket in the DevExpress Support Center.