本人最近在写一个项目使用的是ASP.NET MVC需要给页面中的ztree动态赋值,我从数据库中查询的数据直接赋值给一个LIST<string>,但是ztree无法获得数据,希望大家有没有例子可供参考一下。
在前台这么写,后台直接SQL查询就能赋值了
<script type="text/javascript">
var setting = {
async: {
enable: true,
url: '/YY_BD_FaultType/JsonCYTimeApp',
autoParam: ["id","pId", "name=n"],
otherParam: { "otherParam": "zTreeAsyncTest" },
dataFilter: filter
},
data: {
simpleData: {
enable: true
}
},
check: {
enable: true
},
callback: {
beforeCheck: beforeCheck,
onCheck: onCheck
}
};
function filter(treeId, parentNode, childNodes) {
if (!childNodes) return null;
for (var i = 0, l = childNodes.length; i < l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
var code, log, className = "dark";
function beforeCheck(treeId, treeNode) {
className = (className === "dark" ? "" : "dark");
// showLog(treeNode.name);
return (treeNode.doCheck !== false);
}
function onCheck(e, treeId, treeNode) {
if (treeNode.checked == true) {
showLog(treeNode.id);
}
}
function showLog(str) {
if (!log) log = $("#log");
log.append(str);
zhi(str)
if (log.children("li").length > 6) {
log.get(0).removeChild(log.children("li")[0])
}
}
function zhi(str) {
document.getElementById("ID").value = str;
}
function checkNode(e) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
type = e.data.type,
nodes = zTree.getSelectedNodes();
if (type.indexOf("All") < 0 && nodes.length == 0) {
alert("请先选择一个节点");
}
if (type == "checkAllTrue") {
zTree.checkAllNodes(true);
} else if (type == "checkAllFalse") {
zTree.checkAllNodes(false);
} else {
for (var i = 0, l = nodes.length; i < l; i++) {
if (type == "checkTrue") {
zTree.checkNode(nodes[i], true);
} else if (type == "checkFalse") {
zTree.checkNode(nodes[i], false);
} else if (type == "checkTruePS") {
zTree.checkNode(nodes[i], true, true);
} else if (type == "checkFalsePS") {
zTree.checkNode(nodes[i], false, true);
}
}
}
}
$(document).ready(function () {
$.fn.zTree.init($("#treeDemo"), setting);
$("#checkTrue").bind("click", { type: "checkTrue" }, checkNode);
$("#checkFalse").bind("click", { type: "checkFalse" }, checkNode);
$("#checkTruePS").bind("click", { type: "checkTruePS" }, checkNode);
$("#checkFalsePS").bind("click", { type: "checkFalsePS" }, checkNode);
$("#checkAllTrue").bind("click", { type: "checkAllTrue" }, checkNode);
$("#checkAllFalse").bind("click", { type: "checkAllFalse" }, checkNode);
});
</script>