zoukankan      html  css  js  c++  java
  • 三种方式实现递归树加载

    1、数据库实现:

    可以将此段数据库代码修改为存储过程实现:

    with temp
    as
    (SELECT * FROM EKP_Module WHERE mod_parentid = 2165
    UNION ALL
    SELECT m.* FROM EKP_Module AS m
    INNER JOIN temp AS child ON m.mod_parentid = child.mod_id
    )

    select * from temp

    2、Linq to Sql实现:

    public IEnumerable<APPModuleInfo> GetModulesByPID(int p_id)
    {
    IQueryable<APPModuleInfo> mods = from m in ekpEntities.EKP_MODULE
    where m.mod_parentid == p_id
    select new APPModuleInfo
    {
    ModId = m.mod_id,
    ModLink = m.ModLink,
    ModName = m.mod_name,
    ModParentId = m.mod_parentid
    };

    return mods.ToList().Concat(mods.ToList().SelectMany(m => GetModulesByPID(m.ModId)));
    }

    3、常规方式实现:

    public void AddTree(string ParentID, TreeNode pNode)
        {
            if (ds.Tables.Count > 0)
            {
                DataView dvTree = new DataView(ds.Tables[0]);
                //过滤ParentOrgID,得到当前的所有子节点   
                dvTree.RowFilter = "[IndexParentID]   =   '" + ParentID + "' and [StatusFlag]='1'";
                foreach (DataRowView Row in dvTree)
                {
                    TreeNode node = new TreeNode();
                    if (pNode == null)
                    {         //添加根节点   
                        node.Text = Row["IndexName"].ToString();
                        node.Value = Row["IndexID"].ToString();
                        TreeView1.Nodes.Add(node);
                        AddTree(Row["IndexID"].ToString(), node);         //再次递归   
                    }
                    else
                    {       //添加当前节点的子节点   
                        node.Text = Row["IndexName"].ToString();
                        node.Value = Row["IndexID"].ToString();
                        pNode.ChildNodes.Add(node);
                        AddTree(Row["IndexID"].ToString(), node);         //再次递归   
                    }
                }
                dvTree.Dispose();
                ds.Dispose();
            }

        }

  • 相关阅读:
    解决mybatis xml文件代码提示
    SVN cleanup failed–previous operation has not finished; run cleanup if it was interrupted
    有36辆自动赛车和6条跑道,没有计时器的前提下,最少用几次比赛可以筛选出最快的三辆赛车?
    mybatis如何在控制台打印执行的sql语句
    Ionic2如何下拉刷新和上拉加载
    Ionic 如何把左上角的按钮去掉?
    Ionic1与Ionic2
    java的四种引用,强弱软虚
    equals变量在前面或者在后面有什么区别吗?这是一个坑点
    Java基础—复用类
  • 原文地址:https://www.cnblogs.com/GGLoner/p/6830397.html
Copyright © 2011-2022 走看看