html - Recursively outputting an ul-list with c# -
i'm trying output result table html ul-list. bind result list<>. in it's simpliest way can this. (i'm simulating binding important know sort columns in order)
list<link> linklist = new list<link>(); linklist.add(new link() { id = 1, parentid = null, title = "sport" }); linklist.add(new link() { id = 2, parentid = 1, title = "football" }); linklist.add(new link() { id = 3, parentid = 1, title = "handball" }); linklist.add(new link() { id = 4, parentid = 1, title = "golf" });
after call recursive method.
public htmlstring navigation { get; set; } navigation = createnavigation(linklist); protected static htmlstring createnavigation1(list<link> linklist, string result = "", int? index = null) { int maxindex = linklist.count() - 1; if (!index.hasvalue) index = maxindex; else index--; link selectedlink; if (index != 0) { selectedlink = linklist[index.value]; if (index != maxindex) { if (linklist[index.value + 1].parentid != selectedlink.parentid) { return new htmlstring("<li id=\"" + selectedlink.id + "\">" + selectedlink.title + "<ul>" + createnavigation1(linklist, result, index) + "</ul></li>"); } else { return new htmlstring(createnavigation1(linklist, result, index) + "<li id=\"" + selectedlink.id + "\">" + selectedlink.title + "</li>"); } } else { return new htmlstring(createnavigation1(linklist, result, index) + "<li id=\"" + selectedlink.id + "\">" + selectedlink.title + "</li>"); } } else { selectedlink = linklist[index.value]; if (linklist[index.value + 1].parentid.hasvalue && linklist[index.value + 1].parentid.value == selectedlink.id) { return new htmlstring("<ul><li>" + selectedlink.title + "<ul>" + result + "</ul></li></ul>"); } else { return new htmlstring("<ul><li>" + selectedlink.title + "</li>" + result + "</ul>"); } } }
when single step think runs should when @ output don't understand whats going wrong. result
<ul><li>sport<ul></ul></li></ul><li id="2">football</li><li id="3">handball</li><li id="4">golf</li>
my wishing should this
<ul><li>sport<ul><li id="2">football</li><li id="3">handball</li><li id="4">golf</li></ul></li></ul>
how come result appended after string , not inside it.
<ul><li>" + selectedlink.title + "</li>" + append here!(result) + "</ul>"
where think incorrect?
thanks!
i didn't check wrong code check implementation instead.
public string gettree(list<link> linklist) { var sb = new stringbuilder(); sb.append("<ul>"); foreach (var link in linklist) { if (!link.parentid.hasvalue) { sb.append("<li>"); sb.append(link.title); sb.append(getchildren(linklist, link.id)); sb.append("</li>"); } } sb.append("</ul>"); return sb.tostring(); } private string getchildren(list<link> linklist, int parentid) { var sb = new stringbuilder(); var childlist = m in linklist m.parentid == parentid orderby m.id select m; if (childlist.count() > 0) { sb.append("<ul>"); foreach (var link in childlist) { sb.append("<li>"); sb.append(link.title); sb.append(getchildren(linklist, link.id)); sb.append("</li>"); } sb.append("</ul>"); } return sb.tostring(); } navigation = new htmlstring(gettree(linklist));
Comments
Post a Comment