A tree menu is required for the permission system. Is a tree menu style
But the problem is that we can implement a dead tree menu. What is written to death? In the front-end code, write down what the tree menu to be loaded looks like. However, our permission system requires that the tree menu be dynamically loaded Based on the table content in the database.
The first thing I want to talk about is database design. to dynamically load a tree menu, you must set the parent node ID and its own ID in the database table. The parent node ID is used to determine which level menu it belongs to, and the parent node ID is used to determine the corresponding level menu. This is something you should pay attention to in database design. Without a parent node or a self-built node, you cannot dynamically load the tree menu.
ZTree is used to dynamically load tree menus. Here, by the way, the zTree website is doing really well, and the various types of zTree are doing very well. I implemented it using zTree, but the difference is that the tree menu implemented by zTree is also written at the front end, all we need to do is to splice the code queried by the database in the background into the code format already written in the foreground.
This is the format of the tree menu written at the front end:
What we need to do is to combine the front-end tree menu format in the background, and the menu name displayed in the tree menu is queried from the database. At the beginning, I tried to spell out the JSON string, but it turns out that I failed, and the spelled string cannot be displayed during running.
The format of the JSON string to be spliced is different from that of the JSON string to be spliced. The format of the JSON string to be spliced is relatively simple, so that the required string can be spliced through loops. The string we need is to determine whether there are subnodes at the next level. I didn't implement this using the JSON string. I can only implement the first-level menu, but I didn't spell out the corresponding three-level menu. Unfortunately, loop, traversal, and DataTable queries are used to concatenate tree menu strings. Below is the code I implemented
This corresponds to the splicing of the implementation level-2 menu
Public string getTree (string strTree) {string Chilstr = ""; // obtain the DataTable zTreeBLL zTree = new zTreeBLL (); DataTable dt = new DataTable (); dt = zTree. queryResource (); // query the number of duplicate data records on the parent node zTreeBLL zTree1 = new zTreeBLL (); DataTable dt1 = new DataTable (); dt1 = zTree1.QueryParidNum (); // The parent node can only be added to 4 // for (int p = 0; p <Convert. toInt32 (dt1.Rows [0] [0]); p ++) // Convert. toInt32 forcibly converts the Object type to int // {int parentId = 0; // queries the number of first parent nodes, that is, the parent node DataRow [] rowsP = dt whose initial value is 0. select ("ParentID =" + parentId); // concatenate the parent node with a loop (int I = 0; I <rowsP. length; I ++) {// obtain the value in rowsP from foreach (DataRow drP in rowsP) {string parName = drP ["ResourceName"]. toString (); // strTree = "[{name: \" "+ parName +" \ ""; strTree = "[{name: \ "" + parName + "\" "; DataRow [] rowsC = dt. select ("ParentID =" + parentId + 1); if (rowsC. length> 0) // If the subnode is not 0, then the child node string {// concatenate the child nodes corresponding to the parent node in a loop (DataRow drC in rowsC) {string chilName = drC ["ResourceName"]. toString (); Chilstr = Chilstr + "{name: \" "+ chilName +" \ "}"; Chilstr = Chilstr + "," ;}chilstr = Chilstr. remove (Chilstr. lastIndexOf (","), 1); strTree = strTree + ", children: [" + Chilstr + "]}];";} else {strTree = strTree + "\"}]; ";}}// parentId ++; //} return strTree ;}
This corresponds to the splicing of multi-level menus.
Public string getTree (string strTree) {string Chilstr = ""; // obtain the DataTable zTreeBLL zTree = new zTreeBLL (); DataTable dt = new DataTable (); dt = zTree. queryResource (); // query the number of duplicate data records on the parent node zTreeBLL zTree1 = new zTreeBLL (); DataTable dt1 = new DataTable (); dt1 = zTree1.QueryParidNum (); // The parent node can only be added to 4 // for (int p = 0; p <Convert. toInt32 (dt1.Rows [0] [0]); p ++) // Convert. toInt32 forcibly converts the Object type to int // {int parentId = 0; // queries the number of first parent nodes, that is, the parent node DataRow [] rowsP = dt whose initial value is 0. select ("ParentID =" + parentId); // concatenate the parent node with a loop (int I = 0; I <rowsP. length; I ++) {// obtain the value in rowsP from foreach (DataRow drP in rowsP) {string parName = drP ["ResourceName"]. toString (); // strTree = "[{name: \" "+ parName +" \ ""; strTree = "[{name: \ "" + parName + "\" "; DataRow [] rowsC = dt. select ("ParentID =" + parentId + 1); if (rowsC. length> 0) // If the subnode is not 0, then the child node string {// Concatenates the child nodes corresponding to the parent node into a foreach (DataRow drC in rowsC) {string Threestr = ""; // determine the corresponding ID under the level-2 menu, that is, find the ParentID string chilFollow = drC ["ID"] of the level-3 menu. toString (); // query the DataRow [] rowsThree = dt. select ("ParentID =" + chilFollow); // determines whether a level-3 menu if (rowsThree. length> 0) {foreach (DataRow drThree in rowsThree) {string ThreeName = drThree ["ResourceName"]. toString (); Threestr = Threestr + "{name: \" "+ ThreeName +" \ "}"; Threestr = Threestr + ",";} Threestr = Threestr. remove (Threestr. lastIndexOf (","), 1); string chilName = drC ["ResourceName"]. toString (); Chilstr = Chilstr + "{name: \" "+ chilName +" \ ", children: [" + Threestr + "]},";} // if no level-3 menu exists, load the level-2 menu else {string chilName = drC ["ResourceName"]. toString (); Chilstr = Chilstr + "{name: \" "+ chilName +" \ "}"; Chilstr = Chilstr + "," ;}} Chilstr = Chilstr. remove (Chilstr. lastIndexOf (","), 1); strTree = strTree + ", children: [" + Chilstr + "]}];";} else {strTree = strTree + "\"}]; ";}} return strTree ;}
Final Effect
In fact, there are many methods to implement this example, such as recursion. Although the JSON string is not spliced successfully, I think the JSON string can be implemented. It is because of my personal abilities that the splicing was not successful. The function of dynamically loading a tree menu is finally implemented, but there are still many areas to be improved, such as how to encapsulate this method so that unlimited calls can be made without defects, this is another question that deserves further exploration.