easyui Menu

Menu

Override defaults with $.fn.menu.defaults.

The menu is usually used for context menus. It is the base component for building other menu component such as menubutton and splitbutton. It also can be used for both navigation and executing commands.

Usage Example

Create Menu

Create menu via markup should add 'easyui-menu' class to <div/> markup. Each menu item is created via <div/> markup. We can add 'iconCls' attribute to menu item to define a icon that will display on left of menu item. Add 'menu-sep' class to menu item will generate a menu seperator.

 
<div id="mm" class="easyui-menu" style="width:120px;">
<div>New</div>
<div>
    <span>Open</span>
    <div style="width:150px;">
        <div><b>Word</b></div>
        <div>Excel</div>
        <div>PowerPoint</div>
    </div>
</div>
<div data-options="iconCls:'icon-save'">Save</div>
<div class="menu-sep"></div>
<div>Exit</div>
</div>

Create menu programatically and listening the 'onClick' event.

$('#mm').menu({
    onClick:function(item){
        //...
    }
});

 

Show Menu

When menu is created, it's hidden and not visible. Call 'show' method to display menu.

 
  1. $('#mm').menu('show', {
  2. left: 200,
  3. top: 100
  4. });

Menu Item

The menu item represents an individual item that is displayed within a menu. It contains the following properties:

NameTypeDescriptionDefault
idstringThe id attribute of menu item. 
textstringThe item text. 
iconClsstringA CSS class to display a 16x16 icon on item left. 
hrefstringSet page location while clicking the menu item. 
disabledbooleanDefines if to disable the menu item.false
onclickfunctionThe function to be called while clicking the menu item. 

Menu Properties

NameTypeDescriptionDefault
zIndexnumberMenu z-index style,increase from it.110000
leftnumberMenu left position.0
topnumberMenu top position.0
alignstringThe menu alignment, possible values are 'left' or 'right'. Available since version 1.4.2.left
minWidthnumberThe minimum width of menu. Available since version 1.3.2.120
itemHeightnumberThe menu item height. Available since version 1.4.2.22
durationnumberDefines duration time in milliseconds to hide when the mouse leaves the menu. Available since version 1.4.100
hideOnUnhoverbooleanWhen true, automatically hides the menu when mouse exits it. Available since version 1.3.5.true
inlinebooleanTrue to stay inside its parent, false to go on top of all elements. Available since version 1.4.2.false
fitbooleanTrue to set the menu size fit it's parent container. Available since version 1.4.2.false

Menu Events

NameParametersDescription
onShownoneFires after menu is showed.
onHidenoneFires after menu is hidden.
onClickitemFires when the menu item is clicked. The example below shows how to process all menu item clicking:
<div class="easyui-menu" data-options="onClick:menuHandler" style="width:120px;">
	<div data-options="name:'new'">New</div>
	<div data-options="name:'save',iconCls:'icon-save'">Save</div>
	<div data-options="name:'print',iconCls:'icon-print'">Print</div>
	<div class="menu-sep"></div>
	<div data-options="name:'exit'">Exit</div>
</div>
<script type="text/javascript">
	function menuHandler(item){
		alert(item.name)
	}
</script>

Menu Methods

NameParameterDescription
optionsnoneReturn the options object.
showposShow a menu on specified position.
pos parameter have two properties:
left: the new left position.
top: the new top position.
hidenoneHide a menu.
destroynoneDestroy a menu
getItemitemElGet the menu item properties that include a 'target' property indicating the item DOM element. The example below shows how to get the specified item by id:
<div id="mm" class="easyui-menu" style="width:120px">
	<div>New</div>
	<div id="m-open">Open</div>
	<div>Save</div>
</div>
var itemEl = $('#m-open')[0];  // the menu item element
var item = $('#mm').menu('getItem', itemEl);
console.log(item);
setTextparamSet the specified menu item text. The 'param' parameter contains two properties:
target: DOM object, the menu item to be setted.
text: string, the new text value.

Code example:

var item = $('#mm').menu('findItem', 'Save');
$('#mm').menu('setText', {
	target: item.target,
	text: 'Saving'
});
setIconparamSet the specified menu item icon. The 'param' parameter contains two properties:
target: DOM object, the menu item.
iconCls: the new icon class.

Code example:

$('#mm').menu('setIcon', {
	target: $('#m-open')[0],
	iconCls: 'icon-closed'
});
findItemtextFind the specified menu item, the return object is same as the getItem method.

Code example:

// find 'Open' item and disable it
var item = $('#mm').menu('findItem', 'Open');
$('#mm').menu('disableItem', item.target);
// find the menu item by name
var item = $('#mm').menu('findItem', {name:'name1'});
// find the menu item by customized definitions
var item = $('#mm').menu('findItem', function(item){
	if (item.name == 'name1'){
		return true;
	} else if (item.age && item.age > 23){
		return true;
	} else {
		return false;
	}
})
appendItemoptionsAppend a new menu item, the 'options' parameter indicate the new item properties. By default the added item will become a top menu item. To append a sub menu item, a 'parent' property should be set to specify the parent item element that already has sub items.

Code example:

// append a top menu item
$('#mm').menu('appendItem', {
	text: 'New Item',
	iconCls: 'icon-ok',
	onclick: function(){alert('New Item')}
});
// append a menu separator
$('#mm').menu('appendItem', {
	separator: true
});
// append a sub menu item
var item = $('#mm').menu('findItem', 'Open');  // find 'Open' item
$('#mm').menu('appendItem', {
	parent: item.target,  // the parent item element
	text: 'Open Excel',
	iconCls: 'icon-excel',
	onclick: function(){alert('Open Excel')}
});
removeItemitemElRemove the specified menu item.
enableItemitemElEnable the menu item.
disableItemitemElDisable the menu item.
showItemitemElShow the menu item. Available since version 1.4.
hideItemitemElHide the menu item. Available since version 1.4.
resizemenuElResize the special menu. Available since version 1.4.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值