Copied to clipboard

Flag this post as spam?

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


  • qrayg 24 posts 92 karma points
    Sep 15, 2016 @ 16:04
    qrayg
    0

    Group Events by Month/Year etc.

    I'm trying to group my events list by month so it looks something like this:

    <ul>
      <li>September
        <ul>
          <li>Event 1</li>
          <li>Event 2</li>
          <li>Event 3</li>
        </ul>
      </li>
      <li>October
        <ul>
          <li>Event 1</li>
          <li>Event 2</li>
          <li>Event 3</li>
        </ul>
      </li>
      <li>November
        <ul>
          <li>Event 1</li>
          <li>Event 2</li>
          <li>Event 3</li>
        </ul>
      </li>
    </ul>
    

    Is this possible to do with the getEvents function?

    I have an old event calendar system I wrote many years in XSLT that does this, but I can't figure out how to do this in Razor syntax.

  • Ole Martin Bakke 112 posts 624 karma points
    Sep 16, 2016 @ 07:28
    Ole Martin Bakke
    0

    Hi, because getEvent returns a list of items you can sort it and group it. To get you list you can do something like this.

    @foreach (var year in Calendar.getEvents(...).GroupBy(x => x.startDate.Year))
        {
          @year.Key        
            foreach(var month in year.GroupBy(x => x.startDate.Month)){
                @month.First().startDate.ToString("MMMM")
                @foreach(var ce in month){
                           @ce.content.Name 
                }
            }
        }
    

    or like this

    @foreach (var items in Calendar.getEvents(...).GroupBy(x => new { x.startDate.Year, x.startDate.Month }))
    {
        @items.First().startDate.ToString("MMMM yyyy")
        foreach (var ce in items)
        {
            @ce.content.Name
        }
    }
    
  • qrayg 24 posts 92 karma points
    Sep 16, 2016 @ 19:53
    qrayg
    0

    Ole,

    Your example works awesome as long as I do not try to use the int nodeId parameter in the getEvents call. Example when I do this I get an error:

    @foreach (var items in Calendar.getEvents(DateTime.Now.Date, DateTime.Now.AddMonths(2), "calDate", CurrentPage.Id, false).GroupBy(x => new { x.startDate.Year, x.startDate.Month }))
    

    The CurrentPage.Id triggers a dynamic node list which GroupBy pukes on.

    I'm trying to use your awesome DataType to allow the customer to insert as many Events Calendar groups as they want. If I take out the CurrentPage.Id (to limit the event list to just each specific section) all the events sections list out all the events on the entire site (not ideal in my case).

    Is there a reason why the getEvents call with the int nodeId parameter requires the dynamic node?

    Or is there a better way for me to accomplish my goal.

    Again, thanks for all your help.

  • Ole Martin Bakke 112 posts 624 karma points
    Sep 19, 2016 @ 06:44
    Ole Martin Bakke
    0

    All the methods should return a List

    getEvents(DateTime startDate, DateTime endDate, string propertyType, DynamicPublishedContent startNode, bool splitNoneRecurring = true)
    

    Then you just use CurrentPage instead of CurrentPage.Id

  • qrayg 24 posts 92 karma points
    Sep 19, 2016 @ 12:13
    qrayg
    0

    I've tried that. I get this error when I try to use the method that contains the startNode option:

    Compiler Error Message: CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

    Source Error:

    <div class="container">
      <ul class="events">
        @foreach (var items in Calendar.getEvents(DateTime.Now.Date, DateTime.Now.AddMonths(2), "calDate", CurrentPage, false).GroupBy(x => new { x.startDate.Year, x.startDate.Month })){
          <li>
            <h4 class="event-month"><i class="fa fa-fw fa-calendar"></i> @items.First().startDate.ToString("MMMM yyyy")</h4>
    

    If I take out the startNode option it does not error and pulls in events. I just need it to start from a specific node and not pull from the entire site. I have customers that need multiple calendar/events systems in the same site.

    In your source code on GitHub all of the methods return a list EXCEPT for the one asking for a nodeID... that returns a dynamic node... which is causing my lambda expression error. Apparently you cannot do a GroupBy on a dynamic node.

    I'm still learning (struggling) with Razor so maybe there's a better way to do what I need... I'm open to suggestions.

    Again, thanks for your help and your awesome datatype.

  • Ole Martin Bakke 112 posts 624 karma points
    Oct 22, 2016 @ 15:33
    Ole Martin Bakke
    0

    Hi, sorry for the late reply. I got this working defining an variable containing the events, and then group the variable.

        List
  • qrayg 24 posts 92 karma points
    Nov 15, 2016 @ 21:21
    qrayg
    0

    Ole, thanks for trying to help me... I'm still a little stuck (I also updated to the latest version as of this post 0.1.7.5.1)

    I tried your code example and I get the following error:

    Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments
    

    I also tried this:

    List<string> events =...
    

    And I get this error:

    'string' does not contain a definition for 'startDate' and no extension method 'startDate' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
    
  • Ole Martin Bakke 112 posts 624 karma points
    Nov 16, 2016 @ 09:56
    Ole Martin Bakke
    0

    Seems like my answer is rendered wrong inside the pre-tag, the list-type was mistaken as a html-tag.

    It shoud be

    List<CalendarEvent> events = Calendar.getEvents(DateTime.Now.Date, DateTime.Now.AddMonths(2), "calendar", CurrentPage.Id, false);
    
  • qrayg 24 posts 92 karma points
    Nov 16, 2016 @ 13:16
    qrayg
    0

    YES!

    That did it. Thank you soooooo much, I really appreciate it.

Please Sign in or register to post replies

Write your reply to:

Draft