Numbering Item on Hierarchical List C#
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a hierarchical list of type Element
. Each Element
has a ParentElement
and a Collection of ChildElements
.
I have written a recursive HTML helper to loop through the hierarchical list and print out each Element into a table row. I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example
Substructure has no child elements
Superstructure has 3 ChildElements
- Upper Floors, Frame and Roof
Upper Floors and Frame have no child elements
Roof has 2 ChildElements
- Roof Coverings and Roof Structure
I have the following HTML on a view that calls the recursive helper.
<table class="table table-bordered">
<thead>
<tr>
<td>Reference</td>
<td>Name</td>
</tr>
</thead>
<tbody>
@ShowTreeTable(Model.Elements, 1)
</tbody>
</table>
The recursive helper is as follows.
@helper ShowTreeTable(List<Entities.Entities.Element> elements, int level)
for (int i = 0; i < elements.Count(); i++)
<tr>
<td>@level</td>
<td>@elements[i].Name</td>
</tr>
if (elements[i].ChildElements.Any())
@ShowTreeTable(elements[i].ChildElements.ToList(), level + 1)
I had a more elaborate helper while trying to do this but it wasn't close so I stripped it back to this.
With the current example the results are coming out like so.
c# recursion tree hierarchical
|
show 1 more comment
I have a hierarchical list of type Element
. Each Element
has a ParentElement
and a Collection of ChildElements
.
I have written a recursive HTML helper to loop through the hierarchical list and print out each Element into a table row. I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example
Substructure has no child elements
Superstructure has 3 ChildElements
- Upper Floors, Frame and Roof
Upper Floors and Frame have no child elements
Roof has 2 ChildElements
- Roof Coverings and Roof Structure
I have the following HTML on a view that calls the recursive helper.
<table class="table table-bordered">
<thead>
<tr>
<td>Reference</td>
<td>Name</td>
</tr>
</thead>
<tbody>
@ShowTreeTable(Model.Elements, 1)
</tbody>
</table>
The recursive helper is as follows.
@helper ShowTreeTable(List<Entities.Entities.Element> elements, int level)
for (int i = 0; i < elements.Count(); i++)
<tr>
<td>@level</td>
<td>@elements[i].Name</td>
</tr>
if (elements[i].ChildElements.Any())
@ShowTreeTable(elements[i].ChildElements.ToList(), level + 1)
I had a more elaborate helper while trying to do this but it wasn't close so I stripped it back to this.
With the current example the results are coming out like so.
c# recursion tree hierarchical
3
So what are the results?
– J. van Langen
Nov 15 '18 at 9:22
1
Dangit, that is like the 3rd Post in a row where the poster is not telling us what is happening or not happening. Aka what they want help on.
– Christopher
Nov 15 '18 at 9:23
@Christopher "I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example (proceeds to provide image of exactly what I want)
– Bad Dub
Nov 15 '18 at 9:27
@J.vanLangen I've added an image of what the current results are based on the provided example code.
– Bad Dub
Nov 15 '18 at 9:28
@BadDub: The problem is taht each itteration, you are creating another table row rather then adding to the existing one. Just look at the HTML you end up emitting to see how wrong your code is. The plumbing tags must be unique.
– Christopher
Nov 15 '18 at 9:45
|
show 1 more comment
I have a hierarchical list of type Element
. Each Element
has a ParentElement
and a Collection of ChildElements
.
I have written a recursive HTML helper to loop through the hierarchical list and print out each Element into a table row. I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example
Substructure has no child elements
Superstructure has 3 ChildElements
- Upper Floors, Frame and Roof
Upper Floors and Frame have no child elements
Roof has 2 ChildElements
- Roof Coverings and Roof Structure
I have the following HTML on a view that calls the recursive helper.
<table class="table table-bordered">
<thead>
<tr>
<td>Reference</td>
<td>Name</td>
</tr>
</thead>
<tbody>
@ShowTreeTable(Model.Elements, 1)
</tbody>
</table>
The recursive helper is as follows.
@helper ShowTreeTable(List<Entities.Entities.Element> elements, int level)
for (int i = 0; i < elements.Count(); i++)
<tr>
<td>@level</td>
<td>@elements[i].Name</td>
</tr>
if (elements[i].ChildElements.Any())
@ShowTreeTable(elements[i].ChildElements.ToList(), level + 1)
I had a more elaborate helper while trying to do this but it wasn't close so I stripped it back to this.
With the current example the results are coming out like so.
c# recursion tree hierarchical
I have a hierarchical list of type Element
. Each Element
has a ParentElement
and a Collection of ChildElements
.
I have written a recursive HTML helper to loop through the hierarchical list and print out each Element into a table row. I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example
Substructure has no child elements
Superstructure has 3 ChildElements
- Upper Floors, Frame and Roof
Upper Floors and Frame have no child elements
Roof has 2 ChildElements
- Roof Coverings and Roof Structure
I have the following HTML on a view that calls the recursive helper.
<table class="table table-bordered">
<thead>
<tr>
<td>Reference</td>
<td>Name</td>
</tr>
</thead>
<tbody>
@ShowTreeTable(Model.Elements, 1)
</tbody>
</table>
The recursive helper is as follows.
@helper ShowTreeTable(List<Entities.Entities.Element> elements, int level)
for (int i = 0; i < elements.Count(); i++)
<tr>
<td>@level</td>
<td>@elements[i].Name</td>
</tr>
if (elements[i].ChildElements.Any())
@ShowTreeTable(elements[i].ChildElements.ToList(), level + 1)
I had a more elaborate helper while trying to do this but it wasn't close so I stripped it back to this.
With the current example the results are coming out like so.
c# recursion tree hierarchical
c# recursion tree hierarchical
edited Nov 15 '18 at 9:26
Bad Dub
asked Nov 15 '18 at 9:18
Bad DubBad Dub
5301429
5301429
3
So what are the results?
– J. van Langen
Nov 15 '18 at 9:22
1
Dangit, that is like the 3rd Post in a row where the poster is not telling us what is happening or not happening. Aka what they want help on.
– Christopher
Nov 15 '18 at 9:23
@Christopher "I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example (proceeds to provide image of exactly what I want)
– Bad Dub
Nov 15 '18 at 9:27
@J.vanLangen I've added an image of what the current results are based on the provided example code.
– Bad Dub
Nov 15 '18 at 9:28
@BadDub: The problem is taht each itteration, you are creating another table row rather then adding to the existing one. Just look at the HTML you end up emitting to see how wrong your code is. The plumbing tags must be unique.
– Christopher
Nov 15 '18 at 9:45
|
show 1 more comment
3
So what are the results?
– J. van Langen
Nov 15 '18 at 9:22
1
Dangit, that is like the 3rd Post in a row where the poster is not telling us what is happening or not happening. Aka what they want help on.
– Christopher
Nov 15 '18 at 9:23
@Christopher "I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example (proceeds to provide image of exactly what I want)
– Bad Dub
Nov 15 '18 at 9:27
@J.vanLangen I've added an image of what the current results are based on the provided example code.
– Bad Dub
Nov 15 '18 at 9:28
@BadDub: The problem is taht each itteration, you are creating another table row rather then adding to the existing one. Just look at the HTML you end up emitting to see how wrong your code is. The plumbing tags must be unique.
– Christopher
Nov 15 '18 at 9:45
3
3
So what are the results?
– J. van Langen
Nov 15 '18 at 9:22
So what are the results?
– J. van Langen
Nov 15 '18 at 9:22
1
1
Dangit, that is like the 3rd Post in a row where the poster is not telling us what is happening or not happening. Aka what they want help on.
– Christopher
Nov 15 '18 at 9:23
Dangit, that is like the 3rd Post in a row where the poster is not telling us what is happening or not happening. Aka what they want help on.
– Christopher
Nov 15 '18 at 9:23
@Christopher "I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example (proceeds to provide image of exactly what I want)
– Bad Dub
Nov 15 '18 at 9:27
@Christopher "I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example (proceeds to provide image of exactly what I want)
– Bad Dub
Nov 15 '18 at 9:27
@J.vanLangen I've added an image of what the current results are based on the provided example code.
– Bad Dub
Nov 15 '18 at 9:28
@J.vanLangen I've added an image of what the current results are based on the provided example code.
– Bad Dub
Nov 15 '18 at 9:28
@BadDub: The problem is taht each itteration, you are creating another table row rather then adding to the existing one. Just look at the HTML you end up emitting to see how wrong your code is. The plumbing tags must be unique.
– Christopher
Nov 15 '18 at 9:45
@BadDub: The problem is taht each itteration, you are creating another table row rather then adding to the existing one. Just look at the HTML you end up emitting to see how wrong your code is. The plumbing tags must be unique.
– Christopher
Nov 15 '18 at 9:45
|
show 1 more comment
1 Answer
1
active
oldest
votes
I got this working by manipulating the the tree in C# before it's put in the view.
First I got the root elements and set their reference property to their position in the list (+1).
var roots = this.Elements.Where(x => !x.ParentElementId.HasValue).ToList();
this.Elements.Where(x => !x.ParentElementId.HasValue).ToList()
.ForEach(x => x.Reference = (roots.IndexOf(x) + 1).ToString());
I then wrote another recursive method to update all the child element references. The tricky part on this was to update the all the children parent element before they where given their own reference. If this wasn't done all the child elements contained a reference to a parent element that didn't have its updated reference.
private void ShowTree(List<Entities.Entities.Element> elements)
for (int i = 0; i < elements.Count(); i++)
if (elements[i].ParentElementId.HasValue)
elements[i].Reference = $"elements[i].ParentElement.Reference.i+1";
if (elements[i].ChildElements.Any())
foreach (var child in elements[i].ChildElements)
child.ParentElement = elements[i];
this.ShowTree(elements[i].ChildElements.ToList());
This combined with the HTML code in the OP resulted in the correct hierarchical references.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316019%2fnumbering-item-on-hierarchical-list-c-sharp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I got this working by manipulating the the tree in C# before it's put in the view.
First I got the root elements and set their reference property to their position in the list (+1).
var roots = this.Elements.Where(x => !x.ParentElementId.HasValue).ToList();
this.Elements.Where(x => !x.ParentElementId.HasValue).ToList()
.ForEach(x => x.Reference = (roots.IndexOf(x) + 1).ToString());
I then wrote another recursive method to update all the child element references. The tricky part on this was to update the all the children parent element before they where given their own reference. If this wasn't done all the child elements contained a reference to a parent element that didn't have its updated reference.
private void ShowTree(List<Entities.Entities.Element> elements)
for (int i = 0; i < elements.Count(); i++)
if (elements[i].ParentElementId.HasValue)
elements[i].Reference = $"elements[i].ParentElement.Reference.i+1";
if (elements[i].ChildElements.Any())
foreach (var child in elements[i].ChildElements)
child.ParentElement = elements[i];
this.ShowTree(elements[i].ChildElements.ToList());
This combined with the HTML code in the OP resulted in the correct hierarchical references.
add a comment |
I got this working by manipulating the the tree in C# before it's put in the view.
First I got the root elements and set their reference property to their position in the list (+1).
var roots = this.Elements.Where(x => !x.ParentElementId.HasValue).ToList();
this.Elements.Where(x => !x.ParentElementId.HasValue).ToList()
.ForEach(x => x.Reference = (roots.IndexOf(x) + 1).ToString());
I then wrote another recursive method to update all the child element references. The tricky part on this was to update the all the children parent element before they where given their own reference. If this wasn't done all the child elements contained a reference to a parent element that didn't have its updated reference.
private void ShowTree(List<Entities.Entities.Element> elements)
for (int i = 0; i < elements.Count(); i++)
if (elements[i].ParentElementId.HasValue)
elements[i].Reference = $"elements[i].ParentElement.Reference.i+1";
if (elements[i].ChildElements.Any())
foreach (var child in elements[i].ChildElements)
child.ParentElement = elements[i];
this.ShowTree(elements[i].ChildElements.ToList());
This combined with the HTML code in the OP resulted in the correct hierarchical references.
add a comment |
I got this working by manipulating the the tree in C# before it's put in the view.
First I got the root elements and set their reference property to their position in the list (+1).
var roots = this.Elements.Where(x => !x.ParentElementId.HasValue).ToList();
this.Elements.Where(x => !x.ParentElementId.HasValue).ToList()
.ForEach(x => x.Reference = (roots.IndexOf(x) + 1).ToString());
I then wrote another recursive method to update all the child element references. The tricky part on this was to update the all the children parent element before they where given their own reference. If this wasn't done all the child elements contained a reference to a parent element that didn't have its updated reference.
private void ShowTree(List<Entities.Entities.Element> elements)
for (int i = 0; i < elements.Count(); i++)
if (elements[i].ParentElementId.HasValue)
elements[i].Reference = $"elements[i].ParentElement.Reference.i+1";
if (elements[i].ChildElements.Any())
foreach (var child in elements[i].ChildElements)
child.ParentElement = elements[i];
this.ShowTree(elements[i].ChildElements.ToList());
This combined with the HTML code in the OP resulted in the correct hierarchical references.
I got this working by manipulating the the tree in C# before it's put in the view.
First I got the root elements and set their reference property to their position in the list (+1).
var roots = this.Elements.Where(x => !x.ParentElementId.HasValue).ToList();
this.Elements.Where(x => !x.ParentElementId.HasValue).ToList()
.ForEach(x => x.Reference = (roots.IndexOf(x) + 1).ToString());
I then wrote another recursive method to update all the child element references. The tricky part on this was to update the all the children parent element before they where given their own reference. If this wasn't done all the child elements contained a reference to a parent element that didn't have its updated reference.
private void ShowTree(List<Entities.Entities.Element> elements)
for (int i = 0; i < elements.Count(); i++)
if (elements[i].ParentElementId.HasValue)
elements[i].Reference = $"elements[i].ParentElement.Reference.i+1";
if (elements[i].ChildElements.Any())
foreach (var child in elements[i].ChildElements)
child.ParentElement = elements[i];
this.ShowTree(elements[i].ChildElements.ToList());
This combined with the HTML code in the OP resulted in the correct hierarchical references.
answered Nov 15 '18 at 16:09
Bad DubBad Dub
5301429
5301429
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316019%2fnumbering-item-on-hierarchical-list-c-sharp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
So what are the results?
– J. van Langen
Nov 15 '18 at 9:22
1
Dangit, that is like the 3rd Post in a row where the poster is not telling us what is happening or not happening. Aka what they want help on.
– Christopher
Nov 15 '18 at 9:23
@Christopher "I want to be able to label each of the Elements in the table based on their position in the hierarchical list. For example (proceeds to provide image of exactly what I want)
– Bad Dub
Nov 15 '18 at 9:27
@J.vanLangen I've added an image of what the current results are based on the provided example code.
– Bad Dub
Nov 15 '18 at 9:28
@BadDub: The problem is taht each itteration, you are creating another table row rather then adding to the existing one. Just look at the HTML you end up emitting to see how wrong your code is. The plumbing tags must be unique.
– Christopher
Nov 15 '18 at 9:45