.net简单算法实现无限级分类(二)
上文我们提到了如何用treeview读取和表示表中无限级分类的数据.
2)添加新节点,首先选择父节点,范例代码如下:
做到删除 节点我们需要的是将它的子节点一并删除,看似很难实现,但我们有了spath实现时就十分简单了首先当然得得到所要删除节点的typeid,然后代码如下:
其他的操作就变的十分简单了,有的与上面的大同小异,这个算法不是很难,但是基本上可以实现无限级分类的要求.
2)添加新节点,首先选择父节点,范例代码如下:
//得到父节点的typeid
int typeid = Convert.ToInt32(Request["pid"]);
//得到父节点的所有字段的值
SqlDataReader dr = man.gettypeaMess(typeid);
while (dr.Read())
{
pid = Convert.ToInt32(dr["TypeId"].ToString());
name = typename.Text;
man.addtype(pid, name);
spth = dr["spath"].ToString();
child = dr["childrenID"].ToString();
man.update(spth);
man.upechildid(pid, child);
}
其中man.gettypeaMess(typeid)方法是我定义的根据父节点typeID得到父节点所有字段的值包括SPATH,CHILDRENID等.update()和upechildid()方法分别是根据父节点SPATH和CHILDRENID得到新节点的SPATH和父节点要增加一个child的id,然后更新对应的数据库代码如下:int typeid = Convert.ToInt32(Request["pid"]);
//得到父节点的所有字段的值
SqlDataReader dr = man.gettypeaMess(typeid);
while (dr.Read())
{
pid = Convert.ToInt32(dr["TypeId"].ToString());
name = typename.Text;
man.addtype(pid, name);
spth = dr["spath"].ToString();
child = dr["childrenID"].ToString();
man.update(spth);
man.upechildid(pid, child);
}
/// <summary>
/// 更新路径
/// </summary>
/// <param name="spath"></param>
public void update(string spath)
{
int type=dbExecuteSql4Value("select top 1 typeid from QType order by typeid desc");
string path=spath+","+Convert.ToString(type);
dbExecuteSql("update QType set spath='" + path + "' where TypeId=" + type);
}
/// <summary>
/// 更新父类childid
/// </summary>
/// <param name="typeid"></param>
/// <returns></returns>
public void upechildid(int typeid,string child)
{
string childid;
int type = dbExecuteSql4Value("select top 1 typeid from QType order by typeid desc");
if (child.Length==0)
{childid= Convert.ToString(type);}
else{
childid = child + "," + Convert.ToString(type);}
dbExecuteSql("update QType set childrenID='" + childid + "' where TypeId=" + typeid);
}
3)删除节点/// 更新路径
/// </summary>
/// <param name="spath"></param>
public void update(string spath)
{
int type=dbExecuteSql4Value("select top 1 typeid from QType order by typeid desc");
string path=spath+","+Convert.ToString(type);
dbExecuteSql("update QType set spath='" + path + "' where TypeId=" + type);
}
/// <summary>
/// 更新父类childid
/// </summary>
/// <param name="typeid"></param>
/// <returns></returns>
public void upechildid(int typeid,string child)
{
string childid;
int type = dbExecuteSql4Value("select top 1 typeid from QType order by typeid desc");
if (child.Length==0)
{childid= Convert.ToString(type);}
else{
childid = child + "," + Convert.ToString(type);}
dbExecuteSql("update QType set childrenID='" + childid + "' where TypeId=" + typeid);
}
做到删除 节点我们需要的是将它的子节点一并删除,看似很难实现,但我们有了spath实现时就十分简单了首先当然得得到所要删除节点的typeid,然后代码如下:
public void deletetype(string typeid)
{
int tid = Convert.ToInt32(typeid);
dbExecuteSql("delete from QType where CHARINDEX(',"+tid+",',spath)>0");
dbExecuteSql("delete from QType where TypeId=" + tid);
}
在这里最重要的就是CHARINDEX了,不明白就去查查吧,我这就不详述了.{
int tid = Convert.ToInt32(typeid);
dbExecuteSql("delete from QType where CHARINDEX(',"+tid+",',spath)>0");
dbExecuteSql("delete from QType where TypeId=" + tid);
}
其他的操作就变的十分简单了,有的与上面的大同小异,这个算法不是很难,但是基本上可以实现无限级分类的要求.