The current application design style tends to be flat, and many management interfaces (Bootstrap admin template) with very beautiful UI have been implemented based on BootStrap. First, take a look at the main interface:
View the menu html structure of the left navigation:
By observation, you can find the characteristics of the menu tree. Note here that the top-level title of the menu is displayed in the span, and the class Also different. How to dynamically generate a treemenu structure that meets this feature from the database?
1 Database field design
2 Demonstration data
5 Menu class implementation:
First of all, the tree structure menu naturally thinks of using recursion to build it. The code is as follows:
public class AdminLTEHelper { ////// 根据DataTable生成AdminLTE的多级菜单目录 /// GetTreeJsonByTable(datatable, "id", "title", "pid", "0","menulevel"); /// /// 数据源 /// ID列 /// Text列 /// 关系字段(字典表中的树结构字段) /// 父ID值(0) /// 菜单显示层级列名 public StringBuilder result = new StringBuilder(); public StringBuilder sb = new StringBuilder(); public void GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId,string colmenulevel) { result.Append(sb.ToString()); sb.Clear(); if (tabel.Rows.Count > 0) { string filer = string.Format("{0}='{1}'", rela, pId); DataRow[] rows = tabel.Select(filer); if (rows.Length > 0) { foreach (DataRow row in rows) { if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0) { //第一层级,名称在多级菜单中 class为treeview //colmenulevel为menulevel,为菜单的显示层级,可以在后台进行配置 //和树的层级可能不同 if (row[colmenulevel].ToString() == "1") { sb.Append("
6 Call
7 Test
Verify whether the generated menu structure is correct. First, check whether the displayed hierarchical structure is consistent with the database. Also check whether clicking on the upper level can expand it. The last thing to note is that after the left menu shrinks, only the icons are displayed. , after moving the mouse over the icon, the submenu can be displayed correctly:
Statement: The copyright of this article belongs to the author and the blog park. Reprinting is welcome, but this statement must be retained without the author's consent, and it must be clearly visible on the article page A link to the original text is provided at the location, otherwise we reserve the right to pursue legal liability.