下面是今天实际例子,动态加载菜单的代码。非常好用哦
下面有两种方式添加事件:
MenuItem mi = new Ext.Net.MenuItem(entity.FullName)
{
IconCls = iconCls,
Menu =
{
new Ext.Net.Menu
{
Items = {
new Ext.Net.MenuItem { IconCls="x-loading-indicator", Text="Loading...", CanActivate=false, HideOnClick=false }
},
TagString = entity.FullName,
Loader = new ComponentLoader
{
Mode = LoadMode.Component,
DirectMethod = "#{DirectMethods}.Items",//加载方法
RemoveAll = true,
Params = {//设置调用事件时给的参数
new Ext.Net.Parameter("tag", "this.tag", ParameterMode.Raw),
new Ext.Net.Parameter("id", entity.Id.ToString(), ParameterMode.Raw)
}
}
}
}
//第一种方法,在初始时给listeners事件初始
// ,Listeners =
//{
// Click = { Handler = "loadMenuExample('" + this.UserInfo.GetUserParameter(entity.NavigateUrl) + "','" + entity.Id + "','" + entity.FullName + "','" + iconCls + "');" }
//}
};
var childList = this.UserModuleList.Where(child => child.ParentId != null && child.ParentId.ToString().Equals(entity.Id.ToString())).OrderBy(child => child.SortCode);
if (childList.Count() <= 0) //是否有子菜单,如果有不本级菜单不加载事件. 第二种方式:在创建完menuitem时再追加进去事件
mi.Listeners.Click.Handler = "loadMenuExample('" + this.UserInfo.GetUserParameter(entity.NavigateUrl) + "','" + entity.Id + "','" + entity.FullName + "','" + iconCls + "');" ;
items.Add(mi);
调用的Items方法使用:
[DirectMethod]
public new string Items(Dictionary<string, string> parameters)
{
//System.Threading.Thread.Sleep(1500); //emulate net delay
string tag = parameters["tag"];
string id = parameters["id"];
}