Copied to clipboard

Flag this post as spam?

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


  • Johan 188 posts 380 karma points
    May 02, 2014 @ 15:44
    Johan
    0

    Reorder nodes

    I'm using MVC razor and i loop out news in the order of the dates. I have a list of IDs of featured posts.

    var getNews = Model.Content.GetPropertyValue<string>("featuredNews", true).Split(',').ToList();
    

    I want to loop out the featured posts first in the order of the list and then the rest of the News nodes.

    What i plan to do is loop out the featured News nodes and then loop out all my nodes after but exclude the node if its ID matches one of the IDs in my featuredNews list.

    Is there a better way to do it? Can you build your own node set? This is what i'm working with:

    var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);
        if (startNode.Children.Where("Visible").Any())
        {
            foreach (var page in startNode.Children.Where("Visible").OrderBy("CreateDate desc"))
            {
                ...
                // Loop out content
                ...
            }
        }    
    

    Update:

    This is what i've got so far and it works. But i'd like the code to be less embarrassing.

    @if (Model.MacroParameters["startNodeID"] != null)
    {
        var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);
        var getNews = Model.Content.GetPropertyValue<string>("featuredNews", true).Split(',').ToList();
    
    // Loop out the featured news posts first
        if (Model.Content.HasValue("featuredNews", true))
        {
            foreach (var nodeId in getNews)
            {
                var page = Umbraco.Content(nodeId);
                @RenderNewsPuff(page)
            }
        }
    
    // Then i loop out the rest of the News Posts but exclude the featured ones i just looped out
        if (startNode.Children.Where("Visible").Any())
        {
            foreach (var page in startNode.Children.Where("Visible"))
            {
                bool skipNode = false;
                foreach(var newsID in getNews){
                    if(newsID == page.Id.ToString()) {
                        skipNode = true;
                    }
                }
                if(!skipNode) {
                    @RenderNewsPuff(page)
                }
            }
        }
    }
    
  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    May 02, 2014 @ 22:55
    Jeavon Leopold
    100

    Hi Johan,

    How about something like this:

    @if (Model.MacroParameters["startNodeID"] != null)
    {   
        var startNode = Umbraco.TypedContent(Model.MacroParameters["startNodeID"]);
        var featuredNewsIds = Model.Content.GetPropertyValue<string>("featuredNews", true).Split(',').Select(int.Parse).ToList();
    
        var allChildren = startNode.Children.Where("Visible");
    
        var myCollection = allChildren.Where(x => featuredNewsIds.Contains(x.Id)).Concat(allChildren.Where(x => !featuredNewsIds.Contains(x.Id)).OrderBy("CreateDate desc"));
    
            foreach (dynamic node in myCollection)
            {
                @RenderNewsPuff(node)
            }
    }
    

    If you want RenderNewsPuff to use the dynamic model you can change the foreach to foreach (dynamic node in myCollection)

    Jeavon

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    May 03, 2014 @ 00:04
    Jeavon Leopold
    1

    Edit above as I forgot the order by

  • Johan 188 posts 380 karma points
    May 08, 2014 @ 19:40
    Johan
    0

    Cool! I had to read up on how the lambda expressions worked but it works perfectly :). Isn't it using the dynamic model already?

  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    May 08, 2014 @ 19:55
    Jeavon Leopold
    0

    Brilliant, oh yeah, I think my comment update switched it to the dynamic model!

    For strongly typed model it would be:

        foreach (var node in myCollection)
        {
            @RenderNewsPuff(node)
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft