Easyui Tree 异步加载实例

前台:

   <div style="border:1px solid #ccc;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();
        }
原文地址:https://www.cnblogs.com/southhuachen/p/5521481.html