基于与父/子链接单个数据库表添加子节点树视图 [英] Adding child nodes to tree view based on single database table with parent/child links

查看:201
本文介绍了基于与父/子链接单个数据库表添加子节点树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何构造,其中子节点是基于单个数据库表中的行可以链接到其他行表示父/子关系的树视图。



例如,由于该表:

 
ID ID_parent十
1零点的
2 1 b
3 1 C
4空D
5 4 E
6 4 F

我要表明如下:

 
A
B
C
D $ b $是
F


解决方案

不知道你的数据检索技术,或者你将如何将数据绑定到组件我会是通用越好。



您需要做的第一件事就是让数据从数据库中。有办法做到这一点层次在SQL Server中,但被仿制的精神让我们假设你打算把它作为一个扁平结构。最重要的是你的数据被勒令它的父ID,您可以在SQL或代码做到这一点。



假设你现在有数据的有序集合,我们可以填充我们的节点对象。这方面的一个例子是:



 公共类节点:ICollection的<节点> 
{
私有列表<节点>子节点;

公共节点()
{
子节点=新的List<节点>();
}

公共节点本[INT指数]
{
{返回子节点[指数] }
集合{子节点[指数] =值; }
}

公共无效添加(节点childNode)
{
childNodes.Add(childNode);
}

/ *的ICollection<的休息; T>执行* /
}

现在来填充你需要通过对原始数据进行迭代结构

 公共节点PopulateTree(TreeData [] treeData)
{
字典< {IdType},节点> ; flattenedTree =新词典< {} IdType,节点>();

的foreach(在treeData TreeData数据)
{
节点node =新节点();

如果(data.ParentId = {} EmptyId!)
{
节点parentNode = flattenedTree [data.ParentId]
parentNode.Add(节点);
}

flattenedTree.Add(data.Id,节点);
}
}



我没有测试过伪代码,但它应该显示你好吗展数据到一个层次结构的一种方式。你应该能够重构,这归因于清洁的结构,但如何依赖于你的代码库。


I'm trying to work out how to construct a tree view where child nodes are based on a single database table in which rows can link to other rows indicating the parent/child relationship.

For example, given the table:

ID  ID_parent  Ten
1   null       a
2   1          b
3   1          c
4   null       d
5   4          e
6   4          f

I want show follow:

a
  b
  c
d
  e
  f

解决方案

Without knowing your data retrieval technology or how you are going to data bind to your component I will be a generic as possible.

First thing you need to do is get the data out of the database. There are ways to do this hierarchically in sql server but in the spirit of being generic lets assume you are going to get it out as a flat structure. The important thing is your data is ordered by it's parent id, you can do this in sql or in code.

Assuming that you now have an ordered set of data we can populate our node objects. An example of this would be:

public class Node : ICollection<Node>
{
    private List<Node> childNodes;

    public Node()
    {
        childNodes = new List<Node>();
    }

    public Node this[int index]
    {
        get { return childNodes[index]; }
        set { childNodes[index] = value; }
    }

    public void Add(Node childNode)
    {
        childNodes.Add(childNode);
    }            

    /* Rest of ICollection<T> implementation */
}

Now to populate the structure you need to iterate through the original data.

public Node PopulateTree(TreeData[] treeData)
{
    Dictionary<{IdType}, Node> flattenedTree = new Dictionary<{IdType}, Node>();

     foreach(TreeData data in treeData)
     {
         Node node = new Node();

         if (data.ParentId != {EmptyId})
         {
             Node parentNode = flattenedTree[data.ParentId];
             parentNode.Add(node);
         }

         flattenedTree.Add(data.Id, node);
     }
}

I have not tested the pseudo code but it should show you a way of getting flattened data into a hierarchical structure. You should be able to refactor this down to a cleaner structure but how depends on your code base.

这篇关于基于与父/子链接单个数据库表添加子节点树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆