I have assembled a DynamicNodeList with items from different places in the site structure. Now I want to output them grouped as <ul /> list with a header that contais the parent name/title of the items. Anyone got a solution for this?
Not sure if GroupBy is supported or not, but you could probably do something like this (not tested!) to get round it, where list is your DynamicNodeList
I did. I have them Grouped. However, here is my dillema, I am getting multiple nodes with the same CreatorNames. I only want the Creator names to show once in my list, not to show up for each node they have authored.
More or less I am combining a list of Creator Names from multiple nodes and want them all in one list. But I only want their name to show up once in the list. Not multiple times as they do now when I use the Groupby and ForEach.
My code is below.
<h3>@Dictionary.DWTAuthors</h3> <ul> @foreach(var item in @Model.NodeById(1089).Descendants("ScienceResearchUpdatesPost").GroupBy("CreatorName")){ foreach(var node in @item) { <li>@node.CreatorName</li> } } </ul>
So you actually want a distinct list of CreateNames? You probably want Disinct() but this isn't easy to implement. So, you can "hack it" using a bit of LINQ and GroupBy...
var allNodes = Model.NodeById(1089).Descendants("ScienceResearchUpdatesPost").OrderBy("CreatorName");
var distinct = allNodes.Items.GroupBy(x => x.CreatorName).Select(grp => grp.First());
Is there something like 'GroupBy'
Hi,
I have assembled a DynamicNodeList with items from different places in the site structure. Now I want to output them grouped as <ul /> list with a header that contais the parent name/title of the items. Anyone got a solution for this?
Sample list:
item 1 (parent a), item 2 (parent b), item 3 (parent b), item 4 (parent b), item 5 (parent a)
Output:
Parent a
item 1
item 5
Parent b
item 2
item 3
item 4
Not sure if GroupBy is supported or not, but you could probably do something like this (not tested!) to get round it, where list is your DynamicNodeList
@Alex: Thanks.
The code I had so far did almost the same (appearantly I 'm not that dumb after all :)) Key to get it working was in the use of @:
Now that there is GroupBy in Umbraco. How do you do that>
Check the Razor Cheat Sheet for syntax...
I did. I have them Grouped. However, here is my dillema, I am getting multiple nodes with the same CreatorNames. I only want the Creator names to show once in my list, not to show up for each node they have authored.
More or less I am combining a list of Creator Names from multiple nodes and want them all in one list. But I only want their name to show up once in the list. Not multiple times as they do now when I use the Groupby and ForEach.
My code is below.
<h3>@Dictionary.DWTAuthors</h3>
<ul>
@foreach(var item in @Model.NodeById(1089).Descendants("ScienceResearchUpdatesPost").GroupBy("CreatorName")){
foreach(var node in @item) {
<li>@node.CreatorName</li>
}
}
</ul>
Thanks in advance.
Well, I found my solution. I picked apart the example from above. If you are interested.
string parentCurrent = "";
foreach(dynamic node in @Model.NodeById(1089).Descendants("ScienceResearchUpdatesPost").OrderBy("CreatorName"))
{
if (parentCurrent != node.CreatorName)
{
<li>@node.CreatorName</li>
}
parentCurrent = node.CreatorName;
}
@:</ul>
So you actually want a distinct list of CreateNames? You probably want Disinct() but this isn't easy to implement. So, you can "hack it" using a bit of LINQ and GroupBy...
You can also just do like I described here:
http://our.umbraco.org/forum/templating/templates-and-document-types/56965-GroupBy-Issue#comment206450
is working on a reply...