Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Brendan Rice 538 posts 1101 karma points
    Apr 15, 2012 @ 16:45
    Brendan Rice
    0

    Group nodes by month

    I am building an event listing page and need to be able to group events by month as shown in the screen mock-up below:

    http://www.testsite.3sixtywebdesign.co.uk/corrymeela/events.html

    Is this possible with razor and what is the best way of going about it?

  • Douglas Ludlow 210 posts 366 karma points
    Apr 20, 2012 @ 17:05
    Douglas Ludlow
    0

    The easiest way to group your events by month is to place them into month folders. That's currently what we do. We've even gotten to the point where when an event is created, it is automatically placed in the corresponding "month" folder.

    But if you'd prefer not to use that approach, you could sort them, loop through them and then group them by date programatically. There is an example on this forum of how you can group items together. Here a snippet of what we've used in the past for this approach:

    @{
    dynamic events = Model.Children.OrderBy("eventDatePropertyName");
    string month = "";

    foreach(dynamic node in events)
    {
    // Get the events month
    string currentMonth = node.eventDatePropertyName.ToString("MMMM");

    if (currentMonth != month)
    {
    if(month == "")
    {
    @currentMonth
    @:<ul>
    }
    else
    {
    @:</ul>
    @currentMonth
    @:<ul>
    }

    month = currentMonth;
    }
    <li>@node.eventDatePropertyName.ToString("MM/dd/yyyy") - @node.Name</li>
    }
    @:</ul>
    }

    Hope that's helpful!

Please Sign in or register to post replies

Write your reply to:

Draft