Home php教程 PHP开发 The data of jQuery TreeView tree control supports json string and list collection

The data of jQuery TreeView tree control supports json string and list collection

Dec 28, 2016 pm 01:04 PM

This method actually still uses the list collection method to pass it to the front desk, but has made some small changes in the front desk, and the controller code has also been partially optimized. It is worth mentioning that it is useless. Ajax front-end and back-end interaction is abandoned.

The controller code is as follows:

//实例化公共静态字典表集合
public static List<TC_DictionaryInfo> DInfo = new List<TC_DictionaryInfo>();
/// <summary>
/// TreeView视图
/// </summary>
/// <returns></returns>
public ActionResult May(string TypeCode)
{
ViewBag.TypeCode = TypeCode;
List<TC_DictionaryInfo> DInfo = dbll.GetModelList("TypeCode=" + TypeCode);
List<NodeModel> list = GetChildNodes(0, new NodeModel() { }, DInfo).nodes;
ViewBag.data = list;
}
///<summary>
/// GetChildNodes方法,此方法使用递归
/// </summary>
/// <param name="parentId"></param>
/// <returns></returns>
public NodeModel GetChildNodes(int parentId, NodeModel childnodestr, List<TC_DictionaryInfo> DInfo)
{
List<TC_DictionaryInfo> DictionaryList = DInfo.Where(e => Convert.ToInt32(e.ParentId) == parentId).ToList();
for (int i = 0; i < DictionaryList.Count; i++)
{
NodeModel NewNode = new NodeModel();
NewNode.DicId = DictionaryList[i].DicId;
NewNode.text = DictionaryList[i].DICName;
NewNode.ParentId = DictionaryList[i].ParentId;
childnodestr.nodes.Add(NewNode);
GetChildNodes(NewNode.DicId, NewNode, DInfo);
}
return childnodestr;
}
Copy after login

PS: It is no longer three methods but simplified to two methods (almost no changes).

The front-end code is as follows:

var data=&#39;@JsonConvert.SerializeObject( ViewBag.data)&#39;.replace(/"/g,&#39;"&#39;);
$(function() {
$(&#39;#treeview4&#39;).treeview({
color: "#428bca",
data: data,
onNodeSelected: function(event, data) {
alert(data);
}
});
Copy after login

PS: The open source json format serial number and deserialization class library under .NET is used here

https://www.ibm.com/developerworks/cn/web/wa-lo-json/,

The following introduces the two methods of json serialization and deserialization Important methods:

JsonConvert.SerializeObject(object value) serialization,

It has an overloaded method JsonConvert.SerializeObject(object value, params JsonConverter[] converters) .

JsonConvert.DeserializeObject(string value, Type type), deserialization,

It has an overloaded method JsonConvert.DeserializeObject(string value, Type type, params JsonConverter[] converters)

These two methods can implement basic serialization and deserialization requirements.

The function of replace in js is to replace specific symbols with the symbols you need.

The function of replace(/\/g,'"') here is to replace all / with "(because the page requires a json string).

In this case, our page can read the data and display it. BZ still feels this way is better.

The above is the jQuery TreeView tree control (2) based on MVC5 and Bootstrap introduced by the editor. The data supports json strings and list collections. I hope it will be helpful to you. If you have any If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more jQuery TreeView tree control data supporting json strings and list collections, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)