C # implement the dynamic loading tree menu,

Source: Internet
Author: User

C # implement the dynamic loading tree menu,

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

[Csharp] view plaincopy

PublicstringgetTree (stringstrTree)

{

StringChilstr = "";

// Obtain the DataTable

ZTreeBLLzTree = newzTreeBLL ();

DataTabledt = newDataTable ();

Dt = zTree. QueryResource ();

// Query the number of non-duplicate data records on the parent node

ZTreeBLLzTree1 = newzTreeBLL ();

DataTabledt1 = newDataTable ();

Dt1 = zTree1.QueryParidNum ();

// The parent node can only be added to 4

// For (intp = 0; p //{

IntparentId = 0;

// Find the number of the first parent node, that is, the parent node whose initial value is 0.

DataRow [] rowsP = dt. Select ("ParentID =" + parentId );

// Concatenate the parent node using a loop

For (inti = 0; I {

// Obtain the value in rowsP

Foreach (DataRowdrPinrowsP)

{

StringparName = drP ["ResourceName"]. ToString ();

// StrTree = "[{name: \" "+ parName + "\"";

StrTree = "[{name: \" "+ parName + "\"";

DataRow [] rowsC = dt. Select ("ParentID =" + parentId + 1 );

If (rowsC. Length> 0) // if the child node is not 0, concatenate the string of the child node

{

// Concatenate child nodes corresponding to the parent node in a loop

Foreach (DataRowdrCinrowsC)

{

StringchilName = drC ["ResourceName"]. ToString ();

Chilstr = Chilstr + "{name: \" "+ chilName + "\"}";

Chilstr = Chilstr + ",";

}

Chilstr = Chilstr. Remove (Chilstr. LastIndexOf (","), 1 );

StrTree = strTree + ", children: [" + Chilstr + "]}];";

}

Else

{

StrTree = strTree + "\"}]; ";

}

}

}

// ParentId ++;

//}

ReturnstrTree;

}

This corresponds to the splicing of multi-level menus.

[Csharp] view plaincopy

PublicstringgetTree (stringstrTree)

{

StringChilstr = "";

// Obtain the DataTable

ZTreeBLLzTree = newzTreeBLL ();

DataTabledt = newDataTable ();

Dt = zTree. QueryResource ();

// Query the number of non-duplicate data records on the parent node

ZTreeBLLzTree1 = newzTreeBLL ();

DataTabledt1 = newDataTable ();

Dt1 = zTree1.QueryParidNum ();

// The parent node can only be added to 4

// For (intp = 0; p //{

IntparentId = 0;

// Find the number of the first parent node, that is, the parent node whose initial value is 0.

DataRow [] rowsP = dt. Select ("ParentID =" + parentId );

// Concatenate the parent node using a loop

For (inti = 0; I {

// Obtain the value in rowsP

Foreach (DataRowdrPinrowsP)

{

StringparName = drP ["ResourceName"]. ToString ();

// StrTree = "[{name: \" "+ parName + "\"";

StrTree = "[{name: \" "+ parName + "\"";

DataRow [] rowsC = dt. Select ("ParentID =" + parentId + 1 );

If (rowsC. Length> 0) // if the child node is not 0, concatenate the string of the child node

{

// Concatenate child nodes corresponding to the parent node in a loop

Foreach (DataRowdrCinrowsC)

{

StringThreestr = "";

// Determine the corresponding ID under the level-2 menu, that is, find the ParentID of the level-3 menu

StringchilFollow = drC ["ID"]. ToString ();

// Query the level-3 menu

DataRow [] rowsThree = dt. Select ("ParentID =" + chilFollow );

// Determine whether a level-3 menu exists

If (rowsThree. Length> 0)

{

Foreach (DataRowdrThreeinrowsThree)

{

StringThreeName = drThree ["ResourceName"]. ToString ();

Threestr = Threestr + "{name: \" "+ ThreeName + "\"}";

Threestr = Threestr + ",";

}

Threestr = Threestr. Remove (Threestr. LastIndexOf (","), 1 );

StringchilName = drC ["ResourceName"]. ToString ();

Chilstr = Chilstr + "{name: \" "+ chilName +" \ ", children: [" + Threestr + "]},";

}

// If no level-3 menu exists, load the level-2 menu directly.

Else

{

StringchilName = drC ["ResourceName"]. ToString ();

Chilstr = Chilstr + "{name: \" "+ chilName + "\"}";

Chilstr = Chilstr + ",";

}

}

Chilstr = Chilstr. Remove (Chilstr. LastIndexOf (","), 1 );

StrTree = strTree + ", children: [" + Chilstr + "]}];";

}

Else

{

StrTree = strTree + "\"}]; ";

}

}

}

ReturnstrTree;

}

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.