您的位置:首页 > 产品设计 > UI/UE

Easyui Tree 异步加载实例

2016-05-23 21:27 351 查看
前台:

<div style="border:1px solid #ccc;width:100px;padding:10px">
<ul id="MyTree"></ul>
</div>
<button onclick="getChecked()">获得选中值</button>
<script type="text/javascript">
$('#MyTree').tree({
url: 'tree.ashx?id=0&state=closed',
checkbox: true,
onBeforeExpand: function (node, param) {
$(this).tree('options').url = "tree.ashx?state=open&id=" + node.id;
}
});

function getChecked() {
var arr = [];
var nodes = $('#MyTree').tree('getChecked', 'checked');
for (var i = 0; i < nodes.length; i++) {
arr.push(nodes[i].id);
}
alert(arr.join(','));
}
</script>


后台:

string id = context.Request["id"];
string state = context.Request["state"];

string json = EasyuiHelper.GetTreeJson(dt,Convert.ToInt32(id) , "linkageid","name", "parentid", state);

context.Response.ContentType = "text/plain";
context.Response.Write(json);

public static string GetTreeJson(DataTable dt, int RootId, string IdFieldName, string TextFieldName,string ParentRelationFieldName, string state = "open")
{
StringBuilder json = new StringBuilder();
json.Append("[");
int i = 0;
foreach (DataRow row in dt.Rows)
{
if (Convert.ToInt32(row[ParentRelationFieldName].ToString()) == RootId)
{
json.Append("{");
json.Append("\"id\":" + row[IdFieldName].ToString());
json.Append(",\"text\":\"" + row[TextFieldName].ToString() + "\"");
json.Append(string.Format(",\"state\":\"{0}\"", state));
if (state == "open")
{
string ChildrenNode = GetTreeJson(dt, Convert.ToInt32(row[IdFieldName].ToString()), IdFieldName, TextFieldName, ParentRelationFieldName, state);
if (ChildrenNode != "[]")
{
json.Append(",\"children\":" + ChildrenNode);
}
}
json.Append("},");
}

}
if (json.ToString().EndsWith(","))
{
json.Remove(json.Length - 1, 1);
}
json.Append("]");
return json.ToString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: