/// <summary>
/// 通用的树型类,可以生成任何树型结构
/// </summary>
public class CateTreeView
{
IList<CategoryInfo> lsCate = new List<CategoryInfo>(); //初始化数组
string[] icons = new string[] { "│", "├", "└" }; //修饰符号
string htmlret = "";
string nbsp = " ";
public CateTreeView(IList<CategoryInfo> ls)
{
lsCate = ls;
htmlret = "";
}
public CategoryInfo getOne(int id)
{
foreach (CategoryInfo m in lsCate)
{
if (m.cateid == id)
{
return m;
}
}
return null;
}
/// <summary>
/// 得到父级数组
/// </summary>
/// <param name="myid"></param>
/// <returns></returns>
public IList<CategoryInfo> get_parent(int myid)
{
IList<CategoryInfo> lsnew = new List<CategoryInfo>();
CategoryInfo m = getOne(myid);
if (m == null) return lsnew;
int pid = m.parentid;
if (pid == 0) return lsnew;
pid = getOne(pid).parentid;
foreach (CategoryInfo mcate in lsCate)
{
if (mcate.parentid == pid)
{
lsnew.Add(mcate);
}
}
return lsnew;
}
/// <summary>
/// 得到子级分类
/// </summary>
/// <param name="myid"></param>
/// <returns></returns>
public IList<CategoryInfo> get_child(int myid)
{
IList<CategoryInfo> lsnew = new List<CategoryInfo>();
foreach (CategoryInfo mcate in lsCate)
{
if (mcate.parentid == myid)
{
lsnew.Add(mcate);
}
}
return lsnew;
}
/// <summary>
/// 得到树型结构
/// </summary>
/// <param name="myid">表示获得这个ID下的所有子级</param>
/// <param name="str">生成树型结构的基本代码,例如: option value=$id $selected $spacer$name option </param>
/// <param name="sid">被选中的ID,比如在做树型下拉框的时候需要用到</param>
/// <param name="adds"></param>
/// <param name="str_group"></param>
/// <returns></returns>
public string get_tree(int myid, string str, int sid, string adds, string str_group)
{
int number = 1;
IList<CategoryInfo> child = get_child(myid);
if (child.Count > 0)
{
int total = child.Count;
foreach (CategoryInfo m in child)
{
string j = "", k = "";
if (number == total)
{
j += icons[2];
}
else
{
j += icons[1];
k = adds != "" ? icons[0] : "";
}
string spacer = adds != "" ? adds + j : "";
string selected = m.cateid == sid ? "selected" : "";
string tempstr = "";
if (m.parentid == 0 && str_group != "") //? "\$nstr = \"$str_group\";" : "\$nstr = \"$str\";";
{
tempstr = str_group;
}
else
{
tempstr = str;
}
tempstr = tempstr.Replace("$id", m.cateid.ToString());
tempstr = tempstr.Replace("$parentid", m.parentid.ToString());
tempstr = tempstr.Replace("$select", selected);
tempstr = tempstr.Replace("$spacer", spacer);
tempstr = tempstr.Replace("$name", m.catename);
tempstr = tempstr.Replace("$modelid", m.modelid.ToString());
htmlret += tempstr;
string bspstr = nbsp;
get_tree(m.cateid, str, sid, adds + k + bspstr, str_group);
number++;
//$this->ret .= $nstr;
//$nbsp = $this->nbsp;
//$this->get_tree($id, $str, $sid, $adds.$k.$nbsp,$str_group);
//$number++;
}
}
return htmlret;
//return $this->ret;
}
/// <summary>
/// 同上一类方法,jquery treeview 风格,可伸缩样式(需要treeview插件支持)
/// </summary>
/// <param name="myid">表示获得这个ID下的所有子级</param>
/// <param name="effected_id">需要生成treeview目录数的id</param>
/// <param name="str">末级样式</param>
/// <param name="str2">目录级别样式</param>
/// <param name="showlevel">直接显示层级数,其余为异步显示,0为全部限制</param>
/// <param name="style">目录样式 默认 filetree 可增加其他样式如'filetree treeview-famfamfam</param>
/// <param name="currentlevel">计算当前层级,递归使用 适用改函数时不需要用该参数</param>
/// <param name="recursion">递归使用 外部调用时为FALSE</param>
/// <returns>HTML</returns>
public string get_treeview(int myid, string effected_id, string str, string str2, int showlevel, string style, int currentlevel, bool recursion)
{
IList<CategoryInfo> child = get_child(myid);
string effected = "id=\"" + effected_id + "\"";
string placeholder = "<ul><li><span class='placeholder'></span></li></ul>";
if (!recursion) htmlret += "<ul " + effected + " class=\"" + style + "\">";
foreach (CategoryInfo m in child)
{
IList<CategoryInfo> child2 = get_child(m.cateid);
string folder = "";
//@extract($a);
if (showlevel > 0 && showlevel == currentlevel && (child2.Count > 0)) folder = "hasChildren"; //如设置显示层级模式@2011.07.01
string floder_status = ((folder != "") ? " class=\"" + folder + "\"" : "");
htmlret += recursion ? "<ul><li " + floder_status + " id=\"" + m.cateid + "\">" : "<li " + floder_status + " id=\"" + m.cateid + "\">";
recursion = false;
if (child2.Count > 0)
{
string nstr = str2;
//eval("\$nstr = \"$str2\";");
string tempstr = str2;
tempstr = tempstr.Replace("$id", m.cateid.ToString());
tempstr = tempstr.Replace("$parentid", m.parentid.ToString());
//tempstr = tempstr.Replace("$select", selected);
//tempstr = tempstr.Replace("$spacer", spacer);
tempstr = tempstr.Replace("$name", m.catename);
tempstr = tempstr.Replace("$modelid", m.modelid.ToString());
htmlret += tempstr;
if (showlevel == 0 || (showlevel > 0 && showlevel > currentlevel))
{
get_treeview(m.cateid, effected_id, str, str2, showlevel, style, currentlevel + 1, true);
}
else if (showlevel > 0 && showlevel == currentlevel)
{
htmlret += placeholder;
}
}
else
{
string nstr = str;
string tempstr = str;
tempstr = tempstr.Replace("$id", m.cateid.ToString());
tempstr = tempstr.Replace("$parentid", m.parentid.ToString());
//tempstr = tempstr.Replace("$select", selected);
//tempstr = tempstr.Replace("$spacer", spacer);
tempstr = tempstr.Replace("$name", m.catename);
tempstr = tempstr.Replace("$modelid", m.modelid.ToString());
htmlret += tempstr;
//str += nstr;
}
htmlret += recursion ? "</li></ul>" : "</li>";
}
if (!recursion)htmlret += "</ul>";
return htmlret;
}
}
没有太高的复用性,用到的人自行改进把呵呵。别忘了改后再分享下。 其中CategoryInfo类是 分类实体类。
结合jquery treeview 插件 做出的效果图如下:
详细参考: 东营网站建设:C#生成漂亮的树型结构
101 个解决方案
#1
#2
#3
#4
#5
#6
#7
#8
楼上的 乱发广告 小心生娃没屁眼儿
#9
#10
#11
#12
#13
很COOL....MARK之
#14
好,不错
#15
#16
#17
#18
#19
不用treeview,会不会很复杂?
#20
据说是PHPCMS用的生成树类。的确很帅,没有太多时间直接照搬改成C#版
#21
好,不错
#22
#23
#24
#25
#26
#27
是很不错,不过来做广告的吧
#28
#29
#30
#31
#32
#33
O(∩_∩)O楼主无私。thank you
#34
#35
#36
#37
#38
看效果还不错 回去弄弄自己实现看下
#39
#40
谢谢奉献,我以前都是用treeview控件的
#41
#42
好东西.....
#43
#44
谢谢楼主,收藏了
#45
#46
#47
#48
#49
#50
NICE MARK
#1
#2
#3
#4
#5
#6
#7
#8
楼上的 乱发广告 小心生娃没屁眼儿
#9
#10
#11
#12
#13
很COOL....MARK之
#14
好,不错
#15
#16
#17
#18
#19
不用treeview,会不会很复杂?
#20
据说是PHPCMS用的生成树类。的确很帅,没有太多时间直接照搬改成C#版
#21
好,不错
#22
#23
#24
#25
#26
#27
是很不错,不过来做广告的吧
#28
#29
#30
#31
#32
#33
O(∩_∩)O楼主无私。thank you
#34
#35
#36
#37
#38
看效果还不错 回去弄弄自己实现看下
#39
#40
谢谢奉献,我以前都是用treeview控件的
#41
#42
好东西.....
#43
#44
谢谢楼主,收藏了
#45
#46
#47
#48
#49
#50
NICE MARK