• TreeView递归绑定无限分类数据


    TreeView递归绑定无限分类数据

    实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的:

    字段

    类型

    Id

    int

    ParentId

    int

    Name

    Nvarchar(64)

    实现数据绑定:

            private void ControlsDataBind()
            {
                tvCategory.Nodes.Clear();
                List<Models.Category> types = CommonNews.Helper.OperateContext.Current.LoadNewsTypes();
                IEnumerable<Models.Category> rootNodes = types.Where(t => t.ParentId == 0);
                TreeNode node = null;
                foreach (Models.Category item in rootNodes)
                {
                    node = new TreeNode(item.CategoryName, item.CategoryId.ToString());
                    if (tvCategory.Nodes.Contains(node))
                    {
                        continue;
                    }
                    IEnumerable<Models.Category> ts = types.Where(t => t.ParentId == item.CategoryId);
                    AddNodesToTree(ts, node, 0);
                }
            }
    
            private void AddNodesToTree(IEnumerable<Models.Category> category, TreeNode node, int level)
            {
                TreeNode childNode = null;
                foreach (Models.Category c in category)
                {
                    childNode = new TreeNode(c.CategoryName, c.CategoryId.ToString());
                    if (tvCategory.Nodes.Contains(childNode))
                    {
                        continue;
                    }
                    node.ChildNodes.Add(childNode);
                    AddNodesToTree(category.Where(t => t.CategoryId == c.ParentId), childNode, level + 1);
                }
                tvCategory.Nodes.Add(node);
            }
    TreeViewBind
  • 相关阅读:
    thinkphp使用ajax
    thinkphp修改和删除数据
    thinkphp添加数据
    thinkphp中的查询语句
    thinkphp模型
    空控制器的处理
    thinkphp3.2.3版本文件目录及作用
    12月18日Smarty文件缓存
    12月15日smarty模板基本语法
    12月13日上午Smarty模版原理
  • 原文地址:https://www.cnblogs.com/weihanli/p/4582200.html
Copyright © 2020-2023  润新知