C#集合在前端通过jQuery生成json树代码

@{
Layout = null;
}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div>

</div>
</div>

<script type="text/javascript">

data = "@ViewBag.treeData";


function BindChildNode(source, parcode) {
var result = "";
for (var i = 0; i < source.length; i++) {
if (source[i].PID == parcode) {
if (result == "") {
result = ",\"children\":[";
result += "{\"id\":\"" + source[i].ID + "\",\"text\":\"" + source[i].Name + "\",\"Note\":" + source[i].Note + "" + "}";
}
else {
result += ",{\"id\":\"" + source[i].ID + "\",\"text\":\"" + source[i].Name + "\",\"Note\":" + source[i].Note + "" + "}";
}
}
}
if (result != "")
result += "]";
return result;
}

$(function () {

data = data.replace(/&quot;/g, "\"");//&quot;
data = "{\"d\":" + data + "}";
treeBaseData = $.parseJSON(data);

var jsonstr = "[";
for (var i = 0; i < treeBaseData.d.length; i++) {
if (treeBaseData.d[i].PID.trim() == "" || treeBaseData.d[i].PID.trim() == null || treeBaseData.d[i].PID.trim() == "null") {
if (jsonstr == "[") {
jsonstr += "{\"id\":\"" + treeBaseData.d[i].ID + "\",\"text\":\"" + treeBaseData.d[i].Name + "\"" + BindChildNode(treeBaseData.d, treeBaseData.d[i].ID) + "}";
} else {
jsonstr += ",{\"id\":\"" + treeBaseData.d[i].ID + "\",\"text\":\"" + treeBaseData.d[i].Name + "\"" + BindChildNode(treeBaseData.d, treeBaseData.d[i].ID) + "}";
}
}
}
jsonstr += "]";
var a = ""
alert(jsonstr);
});
</script>
</body>
</html>

你可能感兴趣的:(C#集合在前端通过jQuery生成json树代码)