asp.net MVC中使用EasyUI Treegrid 树形网格

前台代码:

<link href="~/Content/plugins/jquery-easyui-1.7.0/themes/default/easyui.css" rel="stylesheet" />
<script src="~/Content/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="~/Content/plugins/jquery-easyui-1.7.0/jquery.easyui.min.js"></script>

<table id="tt" style="width:600px;height:400px"></table>

<script>
    $(document).ready(function () {
        $.ajax({
            type: 'get',
            url: '/Design/GetTreeData',
            dataType: 'json',
            success: function (data) {
                console.log(JSON.stringify(data));
                $('#tt').treegrid({
                    data: data,
                    idField: 'id',
                    treeField: 'topic',
                    columns: [[
                    { title: 'topic', field: 'topic', width: 180 },
                    { field: 'id', title: 'id', width: 100, align: 'right' },
                    { field: '_parentId', title: '_parentId', width: 200 }
                    ]]
                });
            },
            error: function (xmlReq, err, c) {

            }
        });
    });
</script>

后台代码:

 public JsonResult GetTreeData()
        {
            List<tests> list = new List<tests>();
            list.Add(new tests() { id = 1, _parentId = 0, topic = "t1" });
            list.Add(new tests() { id = 2, _parentId = 1, topic = "t2" });
            list.Add(new tests() { id = 3, _parentId = 2, topic = "t3" });
            Dictionary<string, object> json = new Dictionary<string, object>();
            var newList = list.Select(n => new { 
            id = n.id, 
            topic = n.topic,
            _parentId = n._parentId == 0 ? null : n._parentId,//跟节点parentid为0时需要处理,不然easyui加载出错
            state = n.DrawingLeave == 0 ? "open" : "closed" //该级节点是否展开
            });
            json.Add("total", 3);
            json.Add("rows", newList);
            return new JsonResult { Data = json, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

   public class tests 
    {
        public int id { get; set; }
        public int? _parentId { get; set; }
        public string topic { get; set; }
    }

 

 

转载于:https://www.cnblogs.com/fengyeqingxiang/p/11018642.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值