Home > php教程 > PHP开发 > body text

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

高洛峰
Release: 2016-12-28 13:04:16
Original
1942 people have browsed it

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!

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!