我正在使用内置的 z80 导航控件,这里是演示链接: Z80 Navigation Menu
如果有人看到该控件,它就会有一个对象来创建类似父菜单的菜单,并在其下创建子菜单。类似于以下内容:
public List<NavBarItem> sampleDynamicNav; //List of navbar objects
public DemoItems()
{
//Create object instance here and assign the parent as well child menus here
sampleDynamicNav = new List<NavBarItem> {
new NavBarItem {ID = 1, Text = "UserInfo", Icon = new ItemIcon {Default = SampleProject.Properties.Resources.nav_new_home, Hover = SampleProject.Properties.Resources.nav_new_home, Selected = SampleProject.Properties.Resources.nav_new_home}, ToolTip = "tooltip Main Menu", Height = 40,
Icon = new ItemIcon {Default = SampleProject.Properties.Resources.nav_new_home, Hover = SampleProject.Properties.Resources.nav_new_home, Selected = SampleProject.Properties.Resources.nav_new_home }, ToolTip = "tooltip Desktop"},
Childs = new List<NavBarItem> {
new NavBarItem {ID = 41, Text = "Add/Edit Users", Height = 30 },
new NavBarItem {ID = 42, ParentID = 1, Text = "Inactive User", Height = 30}
};
}
如果我们静态分配菜单,这非常简单。但我坚持下去,当尝试动态添加它们时,我的意思是从数据库创建菜单,如下所示:
public DemoItems()
{
foreach (var parent in GetParent("USER-0001"))
{
foreach (var child in GetChild(parent.MenuNo))
{
sampleDynamicNav = new List<NavBarItem> {
new NavBarItem {
ID = parent.MenuNo, Text = parent.MenuName, Icon = new ItemIcon {Default = SampleProject.Properties.Resources.nav_new_home, Hover = SampleProject.Properties.Resources.nav_new_home, Selected = SampleProject.Properties.Resources.nav_new_home}, ToolTip = "tooltip Main Menu", Height = 40,
Childs = new List<NavBarItem> {
new NavBarItem {ID = child.MenuNo, ParentID = parent.MenuNo, Text = child.MenuName, Height = 30 },
}
}
};
}
}
}
使用上面的代码,它应该至少在导航栏中获得父菜单。现在,将子菜单放在一边,它在导航栏中显示一个父菜单,如下所示:
但它应该如下所示,因为有两个父菜单并使用foreach
循环迭代列表(GetParents() 返回对象列表):
我不知道我是否需要为此做任何其他事情,并且想知道我是否可以遍历导航栏的子属性,如下所示:
foreach (var child in GetChild(parent.MenuNo))
{
Childs = new List<NavBarItem> {
new NavBarItem {ID = child.MenuNo, ParentID = parent.MenuNo, Text = child.MenuName, Height = 30 },
}
注意:当尝试使用循环迭代子属性时,它现在会抛出错误。第二个内部循环也可以工作并取出子菜单,但是说父菜单有两个子菜单,它当时返回 1。我调试了列表,它像往常一样返回两个父菜单,但没有显示在导航栏中。
GetParents 方法:
/**Get Menu Details - Starts**/
public IEnumerable<UserViewModel> GetParent(string empNo)
{
List<UserViewModel> lstUser = new List<UserViewModel>();
string query = "SELECT DISTINCT M.PARENT, M.MENUNO, M.MENUNAME FROM (SELECT DISTINCT M.MENUNO, M.MENUNAME, M.PARENT " +
"FROM USER_DETAILS U INNER JOIN USER_GROUP_DETAILS UG ON UG.EMPNO = U.EMPNO " +
"INNER JOIN ASSIGN_MENU_DETAILS AM ON AM.GROUPNO = UG.GROUPNO INNER JOIN MENU_DETAILS M " +
"ON M.MENUNO = AM.MENUNO WHERE U.EMPNO = '" + empNo + "' " +
"UNION ALL " +
"SELECT DISTINCT M.MENUNO, M.MENUNAME, " +
"M.PARENT FROM MENU_DETAILS M " +
"INNER JOIN MENU_DETAILS C " +
"ON C.PARENT = M.MENUNO) m WHERE M.PARENT = '0' ORDER BY M.PARENT";
DataTable dt = SelectData(query);
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
UserViewModel bo = new UserViewModel();
bo.Parent = Convert.ToInt32(dr["PARENT"].ToString());
bo.MenuNo = Convert.ToInt32(dr["MENUNO"].ToString());
bo.MenuName = dr["MENUNAME"].ToString();
lstUser.Add(bo);
}
}
return lstUser;
}
/**Get Menu Details - Ends**/