Copied to clipboard

Flag this post as spam?

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


  • Tinyy 4 posts 24 karma points
    Oct 02, 2014 @ 07:33
    Tinyy
    0

    Next/Prev Article Scrolling

    Hi! I am very new to this level of coding but have never got this stumped before...hoping you can help...

    I am trying to basically dynamically populate next and previous buttons on each event page. The problem arises when i have 2 events with the same event date. I have tried adding a second filter using createdate but I'm stuck in a loop somewhere...

    First time I have ever posted to a forum but I guess you would need to see the code? I'm sure its very over complicated and long winded as it is...

    @* Next Event *@
        var myList = new List<umbraco.MacroEngines.DynamicNode>();
    
                foreach (dynamic nextPage in @Model.AncestorOrSelf(2).DescendantsOrSelf().Where("eventDate >= @0", currentPageDate).OrderBy("eventDate desc")) {
                    if (@nextPage.Name != "Events" && @nextPage.Name != @Model.Name)
                    {       
                        if (@nextPage.eventDate > currentPageDate)
                        {
                            if (@nextPage.eventDate >= DateTime.Now)
                            {
                                    nextURL = @nextPage.NiceUrl;
    
                            }
    
                        }
                        else if (@nextPage.eventDate == currentPageDate)
                        {
                            foreach (dynamic nextPageSame in @Model.AncestorOrSelf(2).DescendantsOrSelf().Where("eventDate = @0", currentPageDate).OrderBy("eventVen").Take(1))
                            {
                            <p>@nextPage.Name</p>
                            }
                        }
                    }
                }
        

     

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Oct 02, 2014 @ 10:25
    Steve Morgan
    0

    Can you just explain what you need - your code does look a little confused :)  You have a list of events and when viewing one of these events you want a next and previous button to cycle through the available events (ordered by their eventDate) but only for events that have not yet passed and obviously not linking to the one your current on?  So if there are no previous events which haven't passed this should not be output?

    Or do you want to output all forthcoming events? I ask as in one section you set nextURL but we don't see what you do with it and then in the other you start another loop?!?

     

  • Tinyy 4 posts 24 karma points
    Oct 03, 2014 @ 08:48
    Tinyy
    0

    Morning Steve, thanks for replying...first time posting to a forum so I was just pressing F5 constantly to see a reply! :)

    Exactly what you have said. A next and previous button to cycle through events ordered by their eventDate for events that have not yet passed.

    So to create the next button, I would find the next event by eventDate, as long winded and as the code is, it does work to an extent. The problem is when I get to an eventDate where there is more than 1 event. It just gets into a loop between them. I tried creating the second foreach statement to get all events of the same date then order them by eventVen (Event Venue) to seperate them and exit the loop.

    Hope you can steer me in the right direction.

     

     

     

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Oct 03, 2014 @ 11:15
    Steve Morgan
    0

    Hi,

    Try this code - you'll defeinitely need to test it as I've just quickly made it up.  You'll also need to change two things to match the names of your document type aliases... The EventContainer (EventParent or EventHome?) and the eventItems - if your document type alias is eventItem or just event then this would be eventItems or events  (the plural is important!).

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{  
        var homepage = CurrentPage.AncestorsOrSelf(1).First();
        var eventContainer = homepage.EventContainer.First();   // change EventContainer to match the name of your event parent
        DateTime curDate = DateTime.Now;
        int prevId = 0;
        int nextId = 0;
        bool foundSelf = false;
    
        // change eventItems to be the pluralised version of your eventItem doc type alias  
        foreach( var curEvent in eventContainer.eventItems.Where("Visible && EventDate >= @0", curDate).OrderBy("EventDate desc") )
        {
            // check for self
            if(!foundSelf && curEvent.Id == CurrentPage.Id) {
                foundSelf = true;   
            }
            // keep setting the previous until we find the cur event.   
            if(!foundSelf && curEvent.Id != CurrentPage.Id) {
                prevId = curEvent.Id;
            }
            // if we've foundself and it's not this event but next is still zero then this is our next one.
            if(foundSelf && curEvent.Id != CurrentPage.Id && nextId == 0)
            {
                nextId = curEvent.Id;
            }
        }
        var prevNode = Umbraco.Content(prevId);
        var nextNode = Umbraco.Content(nextId);
    
        // check for zero!
            if (prevId != 0) {
                <h4><a href="@prevNode.Url">Prev: @prevNode.Name - @prevId</a></h4>
            }
        if (nextId != 0) {
            <h4><a href="@nextNode.Url">Next: @nextNode.Name - @nextId</a></h4>
        }
    } 
  • Tinyy 4 posts 24 karma points
    Oct 08, 2014 @ 15:20
    Tinyy
    0

    Hey Steve

    Thanks for this, I now understand your approach to this,  I am still stuck on an error that the namespace name 'Macros' does not exist. Once I figure it all out I will post the final solution.

    Thanks

     

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Oct 08, 2014 @ 16:22
    Steve Morgan
    0

    What version of Umbraco are you using - this is for V7? Have you got this as a Partial View Macro?

  • Tinyy 4 posts 24 karma points
    Oct 09, 2014 @ 08:35
    Tinyy
    0

    Ah, that will be why. I'm on 4.9. Didn't think to mention that... :(

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Oct 09, 2014 @ 11:59
    Steve Morgan
    0

    Tinyy - there are quite a few changes with v4.9 -> V7 razor so be careful just applying scripts you find in the forums here :) If you have any further problems be sure to put this in the description as it will affect the answer.

    This should hopefully do what you want in v4.9 - I'm assuming you've created a Razor script with the corresponding Macro successfully. I had to install v4.9 to remind myself how this all worked so I hope it helps :)  Ensure you remove the v7 @inherits reference at the top that I had in the last v7 script - the script below is complete and needs no references in v4.9 - also, as a seperate issue, please check that you've applied the security steps for this old version of Umbraco. http://umbraco.com/follow-us/blog-archive/2013/5/1/security-update-two-major-vulnerabilities-found.aspx

    @{
        var homepage = @Model.AncestorsOrSelf(1).First();
        var eventContainer = homepage.EventParent.First();   // change EventParent to match your event parent doc type alias
        DateTime curDate = DateTime.Now;
        int prevId = 0;
        int nextId = 0;
        bool foundSelf = false;
    
        // remember to change Events to be the plural of your doc type for the event
        foreach( var curEvent in eventContainer.Events.Where("Visible && EventDate >= @0", curDate).OrderBy("EventDate desc") )
        {
    
            // check for self
            if(!foundSelf && curEvent.Id == Model.Id) {
                foundSelf = true;   
            }
            // keep setting the previous until we find the cur event.   
            if(!foundSelf && curEvent.Id != Model.Id) {
                prevId = curEvent.Id;
            }
            // if we've foundself and it's not this event but next is still zero then this is our next one.
            if(foundSelf && curEvent.Id != Model.Id && nextId == 0)
            {
                nextId = curEvent.Id;
            }
        }
        var prevNode = Library.NodeById(prevId);
        var nextNode = Library.NodeById(nextId);
    
        // check for zero!
            if (prevId != 0) {
                <h4><a href="@prevNode.Url">Prev: @prevNode.Name - @prevId</a></h4>
            }
        if (nextId != 0) {
            <h4><a href="@nextNode.Url">Next: @nextNode.Name - @nextId</a></h4>
        }
    } 
    
    
    
    
Please Sign in or register to post replies

Write your reply to:

Draft