使用 C# 以编程方式将分层数据转换为 HTML 无序列表
我的数据看起来像这样。
| id | name | depth | itemId |
+-----+----------------------+-------+-------+
| 0 | ELECTRONICS | 0 | NULL |
| 1 | TELEVISIONS | 1 | NULL |
| 400 | Tube | 2 | NULL |
| 432 | LCD | 3 | 1653 |
| 422 | Plasma | 3 | 1633 |
| 416 | Portable electronics | 3 | 1595 |
| 401 | MP3 Player | 3 | 1249 |
| 191 | Flash | 2 | NULL |
| 555 | CD Players | 3 | 2198 |
| 407 | 2 Way Radio | 3 | 1284 |
| 388 | I've a problem with | 3 | 1181 |
| 302 | What is your bill pa | 3 | 543 |
| 203 | Where can I find my | 3 | 299 |
| 201 | I would like to make | 3 | 288 |
| 200 | Do you have any job | 3 | 284 |
| 192 | About Us | 3 | NULL |
| 199 | What can you tell me | 4 | 280 |
| 198 | Do you help pr | 4 | 276 |
| 197 | would someone help co| 4 | 272 |
| 196 | can you help ch | 4 | 268 |
| 195 | What awards has Veri | 4 | 264 |
| 194 | What's the latest ne | 4 | 260 |
| 193 | Can you tell me more | 4 | 256 |
| 180 | Site Help | 2 | NULL |
| 421 | Where are the | 3 | 1629 |
| 311 | How can I access My | 3 | 557 |
| 280 | Why isn't the page a | 3 | 512 |
要将上述数据根据深度转换为无序列表,我使用以下代码
int lastDepth = -1;
int numUL = 0;
StringBuilder output = new StringBuilder();
foreach (DataRow row in ds.Tables[0].Rows)
{
int currentDepth = Convert.ToInt32(row["Depth"]);
if (lastDepth < currentDepth)
{
if (currentDepth == 0)
{
output.Append("<ul class=\"simpleTree\">");
output.AppendFormat("<li class=\"root\"><span><a href=\"#\" title=\"root\">root</a></span><ul><li class=\"open\" ><span><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
}
else
{
output.Append("<ul>");
if(currentDepth==1)
output.AppendFormat("<li><span>{0}</span>", row["name"]);
else
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
}
numUL++;
}
else if (lastDepth > currentDepth)
{
output.Append("</li></ul></li>");
if(currentDepth==1)
output.AppendFormat("<li><span>{0}</span>", row["name"]);
else
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
numUL--;
}
else if (lastDepth > -1)
{
output.Append("</li>");
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
}
lastDepth = currentDepth;
}
for (int i = 1; i <= numUL+1; i++)
{
output.Append("</li></ul>");
}
myliteral.text=output.ToString();
,但生成的无序列表似乎没有正确形成(我正在使用它构建树)。例如“Site Help”,id '180 ' 应该显示为 id '1' 的“电视”的直接子级,使用我的代码显示为 id '191' 的 'Flash' 的直接子级。所以除了考虑深度之外,我决定还要考虑 itemid 以便正确获取树视图。表中 itemId 不等于 null 的那些行不应该有子节点(即,它们是树中的叶节点),并且所有其他节点都可以有子节点。
请有人帮助我根据我的深度、itemid 列构建适当的无序列表?
Update:
“itemid”列指的是另一个表中存在的项目的 ID。此列仅有助于识别项目是否有任何子项目(即,在本例中,我的数据中的“名称”在下有任何其他无序列表)它)。
I've data which looks something like this.
| id | name | depth | itemId |
+-----+----------------------+-------+-------+
| 0 | ELECTRONICS | 0 | NULL |
| 1 | TELEVISIONS | 1 | NULL |
| 400 | Tube | 2 | NULL |
| 432 | LCD | 3 | 1653 |
| 422 | Plasma | 3 | 1633 |
| 416 | Portable electronics | 3 | 1595 |
| 401 | MP3 Player | 3 | 1249 |
| 191 | Flash | 2 | NULL |
| 555 | CD Players | 3 | 2198 |
| 407 | 2 Way Radio | 3 | 1284 |
| 388 | I've a problem with | 3 | 1181 |
| 302 | What is your bill pa | 3 | 543 |
| 203 | Where can I find my | 3 | 299 |
| 201 | I would like to make | 3 | 288 |
| 200 | Do you have any job | 3 | 284 |
| 192 | About Us | 3 | NULL |
| 199 | What can you tell me | 4 | 280 |
| 198 | Do you help pr | 4 | 276 |
| 197 | would someone help co| 4 | 272 |
| 196 | can you help ch | 4 | 268 |
| 195 | What awards has Veri | 4 | 264 |
| 194 | What's the latest ne | 4 | 260 |
| 193 | Can you tell me more | 4 | 256 |
| 180 | Site Help | 2 | NULL |
| 421 | Where are the | 3 | 1629 |
| 311 | How can I access My | 3 | 557 |
| 280 | Why isn't the page a | 3 | 512 |
To convert the above data into unordered list based on depth, I'm using the following code
int lastDepth = -1;
int numUL = 0;
StringBuilder output = new StringBuilder();
foreach (DataRow row in ds.Tables[0].Rows)
{
int currentDepth = Convert.ToInt32(row["Depth"]);
if (lastDepth < currentDepth)
{
if (currentDepth == 0)
{
output.Append("<ul class=\"simpleTree\">");
output.AppendFormat("<li class=\"root\"><span><a href=\"#\" title=\"root\">root</a></span><ul><li class=\"open\" ><span><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
}
else
{
output.Append("<ul>");
if(currentDepth==1)
output.AppendFormat("<li><span>{0}</span>", row["name"]);
else
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
}
numUL++;
}
else if (lastDepth > currentDepth)
{
output.Append("</li></ul></li>");
if(currentDepth==1)
output.AppendFormat("<li><span>{0}</span>", row["name"]);
else
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
numUL--;
}
else if (lastDepth > -1)
{
output.Append("</li>");
output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
}
lastDepth = currentDepth;
}
for (int i = 1; i <= numUL+1; i++)
{
output.Append("</li></ul>");
}
myliteral.text=output.ToString();
But the resulting unordered list doesnt seem to be forming properly(using which i am constructing a tree).For example "Site Help" with id '180' is supposed to appear as a direct child of "Televisions" with id '1',is appearing as a direct child of 'Flash' with id '191' using my code.so in addition to considering depth,I've decided to consider itemid as well in order to get the treeview properly.Those rows of the table with itemId not equal to null are not supposed to have a child node(i.e.,they are the leaf nodes in the tree) and all the other nodes can have child nodes.
Please could someone help me in constructing a proper unordered list based on my depth,itemid columns?
Update:
The 'itemid' column refers to the id of an item which is present in another table.This column just helps in identifying if an item has any sub items(i.e., 'name' in my data in this case has any other unordered lists under it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来初始代码没有考虑从“你能告诉我更多吗”节点到“站点帮助”节点所需的备份级别数。
斯图尔特.
It looks like the initial code wasn't taking account of the number of levels back up you needed to go from the "Can you tell me more" node to the "Site Help" node.
Stuart.