asp.net 中用easyui中的treegird的简单使用

几乎每个‘数人头’项目中都会用到的功能,这里先记下来,以后直接到这里复制代码就行了,ASP.NET MVC中的使用

数据库用户表中的除了有个parentid父级ID外,我还多加了以个字段,parentpath,表示父级路径,把该用户的所有上级ID全都存起来,以,间隔,如用户ID=5的低级路径 为:,1,2,3,4,  最前面加多一个,是为了查询该用户的下级用户总数方便

下面是HTML代码:


@{
    ViewBag.Title = "推荐图谱";
}
<link href="~/jquery-easyui-1.5.4.1/themes/default/easyui.css" rel="stylesheet" />
<link href="~/jquery-easyui-1.5.4.1/themes/icon.css" rel="stylesheet" />
<script src="~/jquery-easyui-1.5.4.1/jquery.min.js"></script>
<script src="~/jquery-easyui-1.5.4.1/jquery.easyui.min.js"></script>
<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <ul class="layui-tab-title">
        <li class="layui-this">@ViewBag.Title</li>
    </ul>
</div>


<table title="推荐图谱" class="easyui-treegrid" style="width:700px;height:300px"
       url="/Userinfo/TJTuPu_Ajax"
       rownumbers="true"
       idField="id" treeField="username">
    <thead>
        <tr>
            <th field="username">用户名</th>
            <th field="regdate">注册时间</th>
            <th field="xjcount" >下级总数</th>
        </tr>
    </thead>
</table>

下面是相应的后台代码:
        //推荐图谱-取数据
        public ActionResult TJTuPu_Ajax(int? id)
        {
            ArrayList arr = new ArrayList();
            if (id == null)
            {
                //取自己
                Model.Userinfo u = base.GetLoginUser();
                int xjcount = udal.CalcCount($"parentpath like '%,{u.id},%'");
                string state = xjcount == 0 ? "open" : "closed";
                arr.Add(new { id = u.id, username = u.username, regdate = u.createtime.ToString("yyyy-MM-dd"), xjcount = xjcount, state = state, });
            }
            else
            {
                //根据ID取下一级
                List<Model.Userinfo> list_u = udal.GetListArray($"parentid={id.Value}");
                foreach (var u in list_u)
                {
                    int xjcount = udal.CalcCount($"parentpath like '%,{u.id},%'");
                    string state = xjcount == 0 ? "open" : "closed";
                    arr.Add(new { id = u.id, username = u.username, regdate = u.createtime.ToString("yyyy-MM-dd"), xjcount = xjcount, state = state, });
                }

            }
            return Json(arr);
        }

其实以上的代码都是直接从easyui官网上的demo里复制出来的,我只是把他的后台php示例代码改为了c#而已

猜你喜欢

转载自niunan.iteye.com/blog/2410062