返回> 网站首页 

[转载]C#不用treeview控件生成漂亮的树型结构

yoours2014-04-23 12:44:23 阅读 1495

简介一边听听音乐,一边写写文章。

 C# Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/// 通用的树型类,可以生成任何树型结构   
public class CateTreeView  
{  
    IList lsCate = new List();//初始化数组   
    string[] icons = new string[] { "│""├""└" };//修饰符号   
    string htmlret = "";
    string nbsp = " ";
    public CateTreeView(IList ls)  
    {  
        lsCate = ls;
        htmlret = "";
    }
    public CategoryInfo getOne(int id)  
    {  
        foreach (CategoryInfo m in lsCate)  
        {  
            if (m.cateid == id)  
            {  
                return m;
            }
        }
        return null;
    }

    /// 得到父级数组  
    public IList get_parent(int myid)  
    {  
        IList lsnew = new List();
        CategoryInfo m = getOne(myid);
        if (m == nullreturn lsnew;
        int pid = m.parentid;
        if (pid == 0return lsnew;
        pid = getOne(pid).parentid;
        foreach (CategoryInfo mcate in lsCate)  
        {  
            if (mcate.parentid == pid)  
            {  
                lsnew.Add(mcate);
            }
        }
        return lsnew;

    }

    /// 得到子级分类  
    public IList get_child(int myid)  
    {  
        IList lsnew = new List();

        foreach (CategoryInfo mcate in lsCate)  
        {  
            if (mcate.parentid == myid)  
            {  
                lsnew.Add(mcate);
            }
        }
        return lsnew;
    }

    /// 得到树型结构  
    public string get_tree(int myid, string str, int sid, string adds, string str_group)  
    {  
        int number = 1;
        IList 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;
    }

    /// 同上一类方法,jquery treeview 风格,可伸缩样式(需要treeview插件支持)  
    public string get_treeview(int myid, string effected_id, string str, string str2, int showlevel, string style, int currentlevel, bool recursion)  
    {  
        IList child = get_child(myid);

        string effected = "id=\"" + effected_id + "\"";
        string placeholder = "";
        if (!recursion) htmlret += "" + style + "\">";
        foreach (CategoryInfo m in child)  
        {  
            IList 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 ? "" + m.cateid + "\">" : "+ 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 + 1true);
                }
                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 ? " " : "";
        }
        if (!recursion)htmlret += "";
        return htmlret;
    }
}
微信小程序扫码登陆

文章评论

1495人参与,0条评论