前两天我们介绍了一种新的存储无限级分类方法,但是读出分类的时候怎么实现树形显示呢?方法很简单,我们自己定义一个树形的数据结构,然后根据数据库存储的节点的深度来插入到树里面,当然显示的时候需要用递归来显示一下,不过这里的递归只是在内存里面递归,效率是相当高的。
在数据库读出数据的时候直接按照 LID 来进行 ASC 排序就可以了,默认的排列顺序就是按照树走的,大家可以插入一些数据,并读取一下就可以很明了的看到顺序了,插入树循环的时候只需要对深度进行运算就可以了。
下面我只写出了一些关键地方的代码,具体的代码自己试着多写写在纸上多画画应该就明白了。
另外就是想说下,这种分类算法只适用于一般的树形分类,并不适用于插入数据比较频繁的树形结构,比如说无限次回复的评论,无限次回复的评论有另外一种更适合的算法。
首先我们定义一个树形的数据结构:
public class ZoneList
{
private readonly ZoneList _Parent = null;
private readonly List<ZoneList> _Childs = new List<ZoneList>();
private readonly ZoneItem _Value = null;
public ZoneList Parent { get { return _Parent; } }
public List<ZoneList> Childs { get { return _Childs; } }
public ZoneItem Value { get { return _Value; } }
public ZoneList Root
{
get
{
ZoneList curNode = this;
while (curNode.Parent != null)
{
curNode = curNode.Parent;
}
return curNode;
}
}
public ZoneList() { }
public ZoneList(ZoneItem value, ZoneList parent)
{
_Value = value;
_Parent = parent;
}
public ZoneList AddChild(ZoneItem value)
{
ZoneList nZoneList = new ZoneList(value, this);
_Childs.Add(nZoneList);
return nZoneList;
}
}
然后读取数据库并插入到树:
public ZoneList Select()
{
ZoneList oZoneList = new ZoneList();
ZoneItem oZoneItem;
Int32 intDee = 0;
SqlDataReader oData = SiteSQL.ExecuteReader("ZoneSelect");
while (oData.Read())
{
oZoneItem = new ZoneItem();
oZoneItem.ID = oData.GetInt32(0);
oZoneItem.Tree = oData.GetInt32(1);
oZoneItem.Name = oData.GetString(2);
intDee = intDee - oZoneItem.Tree;
for (Int32 intI = 0; intI <= intDee; intI++)
{
oZoneList = oZoneList.Parent;
}
oZoneList = oZoneList.AddChild(oZoneItem);
intDee = oZoneItem.Tree;
}
oData.Close();
return oZoneList.Root;
}
显示的时候直接用递归就可以了:
private void ZoneOutWrite(ZoneList oZoneList)
{
foreach (ZoneList oZoneNode in oZoneList.Childs)
{
Response.Write(String.Format("<li id=\"zv_{0}\"><span id=\"zName_{0}\">{1}</span>", oZoneNode.Value.ID, oZoneNode.Value.Name));
if (oZoneNode.Childs.Count > 0)
{
Response.Write("<ul>");
ZoneOutWrite(oZoneNode);
Response.Write("</ul>");
}
Response.Write("</li>");
}
}