首页 > 代码库 > zTree的后台数据绑定
zTree的后台数据绑定
1 前台js: 2 var treeNodes; 3 $.ajax({ 4 async: false, 5 cache: false, 6 type: ‘POST‘, 7 contentType: "application/json", 8 url: "/MsgTypeTemItem/GetAllStr?temId=" + $("#temId").val(), //请求的action路径 9 error: function () {//请求失败处理函数 10 alert(‘请求失败‘);11 },12 success: function (data) { //请求成功后处理函数13 data = http://www.mamicode.com/"[" + data + "]";14 treeNodes = eval(data);//格式化(object)数据15 }16 });17 $.fn.zTree.init($("#treeDemo"), setting, treeNodes);18 后台Controller:19 public string GetAllStr()20 {21 string temId = Request.QueryString["temId"];22 List<MsgTypeTemItem> allList = msgTypeItemService.GetAll();23 string querySql = "select * from MessageTempletItem where TempletId=‘" + temId + "‘";24 DataTable dt = sqlHelper.GetDataTable(querySql);25 string result = string.Empty;26 foreach (DataRow dr in dt.Select("FatherId=‘0‘"))27 {28 result += AppendJson(dr, dt);29 }30 int index = result.LastIndexOf("\r\n");31 if (result.Length > 0)32 result = result.Substring(0, index - 1);33 return result;34 }35 private static string AppendJson(DataRow dr, DataTable dtAll)36 {37 string parentNode = string.Empty;38 string isTrue = "false";39 parentNode = "{id:\‘{id}\‘, pId:\‘{pId}\‘, name:\‘{name}\‘, open:" + isTrue + ", drag:true" + "},\r\n";40 parentNode = parentNode.Replace("{id}", dr["ItemId"].ToString()).Replace("{pId}", dr["FatherId"].ToString()).Replace("{name}", dr["ItemContent"].ToString());41 DataRow[] rows = dtAll.Select("FatherId=‘" + dr["ItemId"].ToString().Trim() + "‘");42 if(rows.Length!=0)43 {44 foreach (DataRow row in rows)45 {46 parentNode += AppendJson(row, dtAll);47 }48 }49 return parentNode;50 }51 注意:①.要后台数据进行eval(object化)--eval52 eval前:[{id:‘y1‘,pId:‘0‘,name:‘y1‘,drag:true},{id:‘y11‘,pId:‘y1‘,name:‘y11‘,drag:true},{id:‘y2‘,pId:‘0‘,name:‘y2‘,drag:true}]53 eval后:[object Object],[object Object],[object Object]
相关知识点①:eval
var code1=‘"a" + 2‘; //表达式
var code2=‘{a:2}‘; //语句
var code3=[{a:1,b:2},{a:2,b:3}];
alert(eval(code1)); //->‘a2‘
alert(eval(code2)); //->undefined
alert(eval(‘(‘ + code2 + ‘)‘)); //->[object Object]
alert(eval(code3));//->[object Object],[object Object]
zTree的后台数据绑定
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。