您好,欢迎访问一九零五行业门户网

jQuery TreeView树形控件之数据支持json字符串、list集合

这种方式其实还是利用list集合的方式传给前台,只不过在前台做了一些小小的变化,而控制器代码也进行了部分的优化,值的一提的是:没用的ajax前后台交互舍弃掉了。
控制器代码如下:
//实例化公共静态字典表集合 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; }
ps:不再是三个方法而简化为两个方法(几乎没什么变动)。
前台代码如下:
var data='@jsonconvert.serializeobject( viewbag.data)'.replace(/"/g,'"'); $(function() { $('#treeview4').treeview({ color: "#428bca", data: data, onnodeselected: function(event, data) { alert(data); } });
ps:这里用到了.net 下开源的json格式序列号和反序列化的类库
https://www.ibm.com/developerworks/cn/web/wa-lo-json/ ,
下面介绍json序列化和反序列化的两个重要方法:
jsonconvert.serializeobject(object value)序列化,
它有个重载方法jsonconvert.serializeobject(object value, params jsonconverter[] converters)。
jsonconvert.deserializeobject(string value, type type),反序列化,
它有个重载方法jsonconvert.deserializeobject(string value, type type, params jsonconverter[] converters)
这两个方法可以实现基本的序列化和反序列化要求。
js中的replace的作用是将特定的符号替换为自己需要的符号。
而这里replace(/\/g,'')的作用是把所有的 / 都替换为“(因为页面需要的是json字符串)。
这样的话 我们的页面就可以读取数据进行显示了。bz还是感觉这种方式更好一点。
以上所述是小编给大家介绍的基于mvc5和bootstrap的jquery treeview树形控件(二)之数据支持json字符串、list集合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
更多jquery treeview树形控件之数据支持json字符串、list集合。
其它类似信息

推荐信息