A menu widget usually consists of a main menu bar with pop up menus.
Items in pop up menus often have sub pop up menus. A menu can be created
using the markup elements as long as the parent-child relation is
maintained (using <ul> or <ol>). Each menu item has an
anchor element.
The Menu Widget in jQueryUI can be used for inline and popup menus, or as a base for building more complex menu systems. For example, you can create nested menus with custom positioning.
jQueryUI provides menu() methods to create a menu.
In the above example, you can see a themeable menu with mouse and keyboard interactions for navigation.
In the above example, you can see we have applied an icon image for the submenu list and also changed the submenu position.
The following examples demonstrate how to use the actions given in the above table.
In the above example, you can see the menu is disabled.
In the above example, you can see the focus is on the last menu item. Now expand the submenu and when the mouse leaves the submenu, the submenu is closed.
In the above example, we are printing the messages based on the events triggered.
The Menu Widget in jQueryUI can be used for inline and popup menus, or as a base for building more complex menu systems. For example, you can create nested menus with custom positioning.
jQueryUI provides menu() methods to create a menu.
Syntax
The menu() method can be used in two forms −$ (selector, context).menu (options) Method
The menu (options) method declares that an HTML element and its contents should be treated and managed as menus. The options parameter is an object that specifies the appearance and behavior of the menu items involved.Syntax
$(selector, context).menu (options);You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −
$(selector, context).menu({option1: value1, option2: value2..... });The following table lists the different options that can be used with this method −
Sr.No. | Option & Description |
---|---|
1 | disabled
This option if set to true disables the menu. By default its value is false. |
2 | icons
This option sets the icons for submenus. By default its value is { submenu: "ui-icon-carat-1-e" }. |
3 | menus
This option is a selector for the elements that serve as the menu container, including sub-menus. By default its value is ul. |
4 | position
This option sets the position of submenus in relation to the associated parent menu item. By default its value is { my: "left top", at: "right top" }. |
5 | role
This option is used to customize the ARIA roles used for the menu and menu items. By default its value is menu. |
Default Functionality
The following example demonstrates a simple example of menu widget functionality, passing no parameters to the menu() method.<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Menu functionality</title> <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- CSS --> <style> .ui-menu { width: 200px; } </style> <!-- Javascript --> <script> $(function() { $( "#menu-1" ).menu(); }); </script> </head> <body> <!-- HTML --> <ul id = "menu-1"> <li><a href = "#">Spring</a></li> <li><a href = "#">Hibernate</a></li> <li><a href = "#">Java</a> <ul> <li><a href = "#">Java IO</a></li> <li><a href = "#">Swing</a></li> <li><a href = "#">Jaspr Reports</a></li> </ul> </li> <li><a href = "#">JSF</a></li> <li><a href = "#">HTML5</a></li> </ul> </body> </html>Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
In the above example, you can see a themeable menu with mouse and keyboard interactions for navigation.
Use of icons and position
The following example demonstrates the usage of two options icons, and position in the menu function of JqueryUI.<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Menu functionality</title> <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- CSS --> <style> .ui-menu { width: 200px; } </style> <!-- Javascript --> <script> $(function() { $( "#menu-2" ).menu({ icons: { submenu: "ui-icon-circle-triangle-e"}, position: { my: "right top", at: "right-10 top+5" } }); }); </script> </head> <body> <!-- HTML --> <ul id = "menu-2"> <li><a href = "#">Spring</a></li> <li><a href = "#">Hibernate</a></li> <li><a href = "#">Java</a> <ul> <li><a href = "#">Java IO</a></li> <li><a href = "#">Swing</a></li> <li><a href = "#">Jaspr Reports</a></li> </ul> </li> <li><a href = "#">JSF</a></li> <li><a href = "#">HTML5</a></li> </ul> </body> </html>Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
In the above example, you can see we have applied an icon image for the submenu list and also changed the submenu position.
$ (selector, context).menu ("action", params) Method
The menu ("action", params) method can perform an action on menu elements, such as enabling/disabling the menu. The action is specified as a string in the first argument (e.g., "disable" disables the menu). Check out the actions that can be passed, in the following table.Syntax
$(selector, context).menu ("action", params);;The following table lists the different actions that can be used with this method −
Sr.No. | Action & Description |
---|---|
1 | blur( [event ] )
This action removes the focus from a menu. It triggers the menu's blur event by resetting any active element style. Where event is of type Event and represents what triggered the menu to blur. |
2 | collapse( [event ] )
This action closes the current active sub-menu. Where event is of type Event and represents what triggered the menu to collapse. |
3 | collapseAll( [event ] [, all ] )
This action closes all the open submenus. |
4 | destroy()
This action removes menu functionality completely. This will return
the element back to its pre-init state. This method does not accept any
arguments. |
5 | disable()
This action disables the menu. This method does not accept any arguments. |
6 | enable()
This action enables the the menu. This method does not accept any arguments. |
7 | expand( [event ] )
This action opens the sub-menu below the currently active item, if one exists. Where event is of type Event and represents what triggered the menu to expand. |
8 | focus( [event ], item )
This action activates a particular menu item, begins opening any sub-menu if present and triggers the menu's focus event. Where event is of type Event and represents what triggered the menu to gain focus. and item is a jQuery object representing the menu item to focus/activate. |
9 | isFirstItem()
This action returns a boolean value, which states if the current active menu item is the first menu item. This method does not accept any arguments. |
10 | isLastItem()
This action returns a boolean value, which states if the current active menu item is the last menu item. This method does not accept any arguments. |
11 | next( [event ] )
This action delegates the active state to the next menu item. Where event is of type Event and represents what triggered the focus to move. |
12 | nextPage( [event ] )
This action moves active state to first menu item below the bottom of
a scrollable menu or the last item if not scrollable. Where event is of type Event and represents what triggered the focus to move. |
13 | option( optionName )
This action gets the value currently associated with the specified optionName. Where optionName is of type String and represents the name of the option to get. |
14 | option()
This action gets an object containing key/value pairs representing the current menu options hash. |
15 | option( optionName, value )
This action sets the value of the menu option associated with the specified optionName. Where optionName is of type String and represents name of option to set and value is of type Object and represents value to set for the option. |
16 | option( options )
This action sets one or more options for the menu. Where options is of type Object and represents a map of option-value pairs to set. |
17 | previous( [event ] )
This action moves active state to previous menu item. Where event is of type Event and represents what triggered the focus to move. |
18 | previousPage( [event ] )
This action moves active state to first menu item above the top of a scrollable menu or the first item if not scrollable. Where event is of type Event and represents what triggered the focus to move. |
19 | refresh()
This action initializes sub-menus and menu items that have not
already been initialized. This method does not accept any arguments. |
20 | select( [event ] )
This action selects the currently active menu item, collapses all sub-menus and triggers the menu's select event. Where event is of type Event and represents what triggered the selection. |
21 | widget()
This action returns a jQuery object containing the menu. This method does not accept any arguments. |
Use of disable method
The following example demonstrates the use of disable() method.<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Menu functionality</title> <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- CSS --> <style> .ui-menu { width: 200px; } </style> <!-- Javascript --> <script> $(function() { $( "#menu-3" ).menu(); $( "#menu-3" ).menu("disable"); }); </script> </head> <body> <!-- HTML --> <ul id = "menu-3"> <li><a href = "#">Spring</a></li> <li><a href = "#">Hibernate</a></li> <li><a href = "#">Java</a> <ul> <li><a href = "#">Java IO</a></li> <li><a href = "#">Swing</a></li> <li><a href = "#">Jaspr Reports</a></li> </ul> </li> <li><a href = "#">JSF</a></li> <li><a href = "#">HTML5</a></li> </ul> </body> </html>Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
In the above example, you can see the menu is disabled.
Use of focus and collapseAll methods
The following example demonstrates the use of focus() and collapseAll methods.<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Menu functionality</title> <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- CSS --> <style> .ui-menu { width: 200px; } </style> <!-- Javascript --> <script> $(function() { var menu = $("#menu-4").menu(); $( "#menu-4" ).menu( "focus", null, $( "#menu-4" ).menu().find( ".ui-menu-item:last" )); $(menu).mouseleave(function () { menu.menu('collapseAll'); }); }); </script> </head> <body> <!-- HTML --> <ul id = "menu-4"> <li><a href = "#">Spring</a></li> <li><a href = "#">Hibernate</a></li> <li><a href = "#">JSF</a></li> <li><a href = "#">HTML5</a></li> <li><a href = "#">Java</a> <ul> <li><a href = "#">Java IO</a></li> <li><a href = "#">Swing</a></li> <li><a href = "#">Jaspr Reports</a></li> </ul> </li> </ul> </body> </html>Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
In the above example, you can see the focus is on the last menu item. Now expand the submenu and when the mouse leaves the submenu, the submenu is closed.
Event Management on menu elements
In addition to the menu (options) method which we saw in the previous sections, JqueryUI provides event methods which gets triggered for a particular event. These event methods are listed below −Sr.No. | Event Method & Description |
---|---|
1 | blur(event, ui)
This event is triggered when a menu loses focus. |
2 | create(event, ui)
This event is triggered when a menu is created. |
3 | focus(event, ui)
This event is triggered when a menu gains focus or when any menu item is activated. |
4 | select(event, ui)
This event is triggered when a menu item is selected. |
Example
The following example demonstrates the event method usage for menu widget functionality. This example demonstrates the use of event create, blur and focus.<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Menu functionality</title> <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- CSS --> <style> .ui-menu { width: 200px; } </style> <!-- Javascript --> <script> $(function() { $( "#menu-5" ).menu({ create: function( event, ui ) { var result = $( "#result" ); result.append( "Create event<br>" ); }, blur: function( event, ui ) { var result = $( "#result" ); result.append( "Blur event<br>" ); }, focus: function( event, ui ) { var result = $( "#result" ); result.append( "focus event<br>" ); } }); }); </script> </head> <body> <!-- HTML --> <ul id = "menu-5"> <li><a href = "#">Spring</a></li> <li><a href = "#">Hibernate</a></li> <li><a href = "#">JSF</a></li> <li><a href = "#">HTML5</a></li> <li><a href = "#">Java</a> <ul> <li><a href = "#">Java IO</a></li> <li><a href = "#">Swing</a></li> <li><a href = "#">Jaspr Reports</a></li> </ul> </li> </ul> <span id = "result"></span> </body> </html>Let us save the above code in an HTML file menuexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
In the above example, we are printing the messages based on the events triggered.
No comments:
Post a Comment