Saturday, September 21, 2019

jQuery UI Menu

The jQuery UI Menu widget consists of a main menu bar with pop up menus. Some items in the pop up menus also have sub pop up menus. These menus are created by using markup elements as long as the parent child relationship is maintained.
The jQuery UI uses the menu() method to create menus.


Syntax:

You can use the menu() method in two forms.
  1. $(selector, context).menu (options) Method  
  1. $(selector, context).menu ("action", params) Method  

First Method

The menu (options) method specifies that an HTML element and its contents should be treated and managed as menus. Here the options parameter is an object that specifies the appearance and behavior of the menu items involved.
Syntax:
  1. $(selector, context).menu (options);  
You can use one or more options at a time using JavaScript object. In the case of more than one options, you have to separate them using a comma as follows:
  1. $(selector, context).menu({option1: value1, option2: value2..... });  
Following is a list of different options that can be used with this method.
OptionDescription
disabledIf you set this option to true, it disables the menu. By default its value is false.
iconsThis option sets the icons for submenus. By default its value is { submenu: "ui-icon-carat-1-e" }.
menusThis option is a selector for the elements that serve as the menu container, including sub-menus. By default its value is ul.
positionThis 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" }.
roleThis option is used to customize the aria roles used for the menu and menu items. By default its value is menu.

jQueryUI Menu example 1

Let's take a simple example to demonstrate the menu widget functionality, passing no parameter to the menu() method.
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Menu functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- CSS -->  
  10.       <style>  
  11.          .ui-menu {  
  12.             width: 200px;  
  13.          }  
  14.       </style>  
  15.       <!-- Javascript -->  
  16.       <script>  
  17.          $(function() {  
  18.             $( "#menu-1" ).menu();  
  19.          });  
  20.       </script>  
  21.    </head>  
  22.    <body>  
  23.       <!-- HTML -->   
  24.       <ul id="menu-1">  
  25.          <li><a href="#">C</a></li>  
  26.          <li><a href="#">C++</a></li>  
  27.          <li><a href="#">Java</a>  
  28.             <ul>  
  29.                <li><a href="#">Core Java</a></li>  
  30.                <li><a href="#">J2EE</a></li>  
  31.                <li><a href="#">Spring</a></li>  
  32.                <li><a href="#">Hibernate</a></li>  
  33.                <li><a href="#">Struts</a></li>  
  34.             </ul>  
  35.          </li>  
  36.          <li><a href="#">JSF</a></li>  
  37.          <li><a href="#">HTML5</a></li>  
  38.       </ul>  
  39.    </body>  
  40. </html>  

jQuery UI Menu() example 2

Use of icons and position:
Let's take an example to demonstrate the usage of two options icons and position in the main function of jQuery UI.
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Menu functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- CSS -->  
  10.       <style>  
  11.          .ui-menu {  
  12.             width: 200px;  
  13.          }  
  14.       </style>  
  15.       <!-- Javascript -->  
  16.       <script>  
  17.          $(function() {  
  18.             $( "#menu-2" ).menu({  
  19.                icons: { submenu: "ui-icon-circle-triangle-e"},  
  20.                position: { my: "right top", at: "right-10 top+5" }  
  21.             });  
  22.          });  
  23.       </script>  
  24.    </head>  
  25.    <body>  
  26.       <!-- HTML -->   
  27.       <ul id="menu-2">  
  28.          <li><a href="#">C</a></li>  
  29.          <li><a href="#">C++</a></li>  
  30.          <li><a href="#">Java</a>  
  31.             <ul>  
  32.                <li><a href="#">Core Java IO</a></li>  
  33.                <li><a href="#">J2EE</a></li>  
  34.                <li><a href="#">Spring</a></li>  
  35.                 <li><a href="#">Hibernate</a></li>  
  36.                  <li><a href="#">Struts</a></li>  
  37.             </ul>  
  38.          </li>  
  39.          <li><a href="#">JSF</a></li>  
  40.          <li><a href="#">HTML5</a></li>  
  41.       </ul>  
  42.    </body>  
  43. </html>  

Second Method

The menu ("action", params) method is used to 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:
  1. $(selector, context).menu ("action", params);  
Following is a list of different actions that can be used with this method.
ActionDescription
blur( [event ] )This action removes the focus from a menu. It triggers the menu's blur event by resetting any active element style. Here event is of type event and represents what triggered the menu to blur.
collapse( [event ] )This action closes the current active sub-menu. Here event is of type event and represents what triggered the menu to collapse.
collapseall( [event ] [, all ] )This action closes all the open submenus.
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.
disable()This action disables the menu. This method does not accept any arguments.
enable()This action is used to enable the menu. This method does not accept any arguments.
expand( [event ] )This action is used to open the sub-menu below the currently active item, if one exists. Here event is of type event and represents what triggered the menu to expand.
focus( [event ], item )This action activates a particular menu item, begins opening any sub-menu if present and triggers the menu's focus event. Here 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.
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.
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.
Next( [event ] )This action delegates the active state to the next menu item. Here event is of type event and represents what triggered the focus to move.
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. Here event is of type event and represents what triggered the focus to move.
option( optionName )This action gets the value currently associated with the specified optionName. Here optionName is of type string and represents the name of the option to get.
option()This action gets an object containing key/value pairs representing the current menu options hash.
option( optionName, value )This action sets the value of the menu option associated with the specified optionName. Here 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.
option( options )This action sets one or more options for the menu. Here options is of type object and represents a map of option-value pairs to set.
previous( [event ] )This action moves active state to previous menu item. Here event is of type event and represents what triggered the focus to move.
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. Here event is of type event and represents what triggered the focus to move.
refresh()This action initializes sub-menus and menu items that have not already been initialized. This method does not accept any arguments.
select( [event ] )This action selects the currently active menu item, collapses all sub-menus and triggers the menu's select event. Here event is of type event and represents what triggered the selection.
widget()This action returns a jQuery object containing the menu. This method does not accept any arguments.

jQueryUI Menu() example 3

Use focus and collapseAll method:
Let's take an example to demonstrate the use of focus and collapse all method.
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Menu functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- CSS -->  
  10.       <style>  
  11.          .ui-menu {  
  12.             width: 200px;  
  13.          }  
  14.       </style>  
  15.       <!-- Javascript -->  
  16.       <script>  
  17.          $(function() {  
  18.             var menu = $("#menu-4").menu();  
  19.             $( "#menu-4" ).menu("focus", null, $( "#menu-4" ).menu().find( ".ui-menu-item:last" ));  
  20.             $(menu).mouseleave(function () {  
  21.                menu.menu('collapseAll');  
  22.             });  
  23.          });  
  24.       </script>  
  25.    </head>  
  26.    <body>  
  27.       <!-- HTML -->   
  28.       <ul id="menu-4">  
  29.          <li><a href="#">C</a></li>  
  30.          <li><a href="#">C++</a></li>  
  31.          <li><a href="#">JSF</a></li>  
  32.          <li><a href="#">HTML5</a></li>  
  33.          <li><a href="#">Java</a>  
  34.             <ul>  
  35.                <li><a href="#">Core Java</a></li>  
  36.                <li><a href="#">J2EE</a></li>  
  37.                <li><a href="#">Spring</a></li>  
  38.                <li><a href="#">Hibernate</a></li>  
  39.                <li><a href="#">Struts</a></li>  
  40.        </ul>  
  41.          </li>  
  42.       </ul>  
  43.    </body>  
  44. </html>  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...