分享 C#不用treeview控件生成漂亮的树型结构

CODE163 2012-01-02 10:51:43
加精
据说是PHPCMS用的生成树类。的确很帅,没有太多时间直接照搬改成C#版。

/// <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#生成漂亮的树型结构
...全文
6736 101 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
101 条回复
切换为时间正序
请发表友善的回复…
发表回复
nbshiny 2013-03-18
  • 打赏
  • 举报
回复
请教楼主,有没有具体的一个成功的应用的数据库数据treeview显示的案例呢。不晓得如何调用这个空间的方法。谢谢。
davidlcz 2012-03-13
  • 打赏
  • 举报
回复
通用的树型结构,慢慢学习、学习。
my8861762 2012-02-09
  • 打赏
  • 举报
回复
俺不懂CategoryInfo(分类实体类)是什么,也找不到哪里去引用~
jamesliu847 2012-02-05
  • 打赏
  • 举报
回复
好东西...
fk1984316 2012-01-18
  • 打赏
  • 举报
回复
accomp 2012-01-17
  • 打赏
  • 举报
回复
看着很不错~
altitude30000 2012-01-17
  • 打赏
  • 举报
回复
这楼给抽的,
呵呵谢谢了
随缘不变 2012-01-15
  • 打赏
  • 举报
回复
收集了
编程有钱人了 2012-01-13
  • 打赏
  • 举报
回复
进来支持下
shanghai_zhouwei 2012-01-13
  • 打赏
  • 举报
回复
楼主,写得不错。
CODE163 2012-01-10
  • 打赏
  • 举报
回复
[Quote=引用 83 楼 flyfeifei66 的回复:]
这颗树只能有两层吗?
[/Quote]

这棵树可以无限曾
赵牧野 2012-01-10
  • 打赏
  • 举报
回复
这颗树只能有两层吗?
luomaxiaoyao 2012-01-10
  • 打赏
  • 举报
回复
怎么不提供demo 下载呢?
my328420969 2012-01-09
  • 打赏
  • 举报
回复
还不错嘛
gz5182009 2012-01-09
  • 打赏
  • 举报
回复
kooyou68 2012-01-08
  • 打赏
  • 举报
回复
可能会用到。。。顶下先
开发者孙小聪 2012-01-08
  • 打赏
  • 举报
回复
东营的么????、 你们公司在哪里?????、
szjarvis 2012-01-07
  • 打赏
  • 举报
回复
不错,谢谢楼主分享。
nitaiyoucala 2012-01-07
  • 打赏
  • 举报
回复
好东西,,跪求例子源码。duchenggang.accp@163.com
jojojo456654 2012-01-07
  • 打赏
  • 举报
回复
顶下,弄下来说不定以后用得着。
加载更多回复(30)

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧