Easyui Tree 异步加载实例

前台:

 

   <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();
        }

 

转载于:https://www.cnblogs.com/shenzhenhua/p/5521481.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值