Copied to clipboard

Flag this post as spam?

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


  • Sander Houttekier 114 posts 163 karma points
    Jul 31, 2011 @ 19:31
    Sander Houttekier
    0

    how would i sort nodes in a razor template

    hi,

     

    i am working on a project, wich has a news and events page
    both of these document types are stored in a NewsAndEventsContainer.
    each in its own container, news in the NewsContainer, events in the Events container

    now they need to be listed in 1 list,
    so i started like this:

        @inherits umbraco.MacroEngines.DynamicNodeContext
        @using umbraco.MacroEngines
        @using SolexLibrary
        @using System.Linq
        @using uComponents.Core
        @using uComponents.Core.uQueryExtensions 

        var container = Model.AncestorOrSelf("language").Descendants("newsAndEventsFolder").Items[0];
        @RenderPage("~/macroScripts/RenderSidebar.cshtml", container)
        <div id="news-and-events">
          @RenderPage("~/macroScripts/NewsEventsNav.cshtml", container)
          @{        
            var neItems = uQuery.GetCurrentNode().GetDescendantNodes().Where(n => n.NodeTypeAlias=="newsitem" || n.NodeTypeAlias=="event").GetAllDataTypesSorted("startDateTime desc");
            foreach(dynamic Item in neItems)
            {
              @RenderPage("~/macroScripts/NewsEventNode.cshtml", Item)
            }
          }  
        </div>

    all of this works, except when i added the `.GetAllDataTypesSorted("startDateTime desc")` i get this message

    > ~\macroScripts\634477368846046803_NewsEventsPage.cshtml(19):
    > error CS1061: 'System.Collections.Generic.IEnumerable' does not
    > contain a definition for 'GetAllDataTypesSorted' and no extension
    > method 'GetAllDataTypesSorted' accepting a first argument of type
    > 'System.Collections.Generic.IEnumerable)

    however, according to this site the only reference i need is the uQueryExtentions  so i have no clue as to how i can sort these nodes on a date time property in the best possible way :)

  • Toni Becker 146 posts 425 karma points
    Jul 31, 2011 @ 20:44
    Toni Becker
    0

    you can do it like this:

    ################# define my var:

    @{
    var startNode = Model.someDocType.OrderBy("CreateDate desc");

    Thats how i'm using it, here*s an example from my current project:
    @using umbraco.MacroEngines
    @using System.Linq
    @using System.Xml.Linq

    @{
        var article = Model.AncestorOrSelf(1).Descendants("Article");
    }

    @foreach(dynamic n in article.Take(8).OrderBy("CreateDate"))  //Because the nearest date should be first or you write ("CreateDate desc")
            {
                if(n.articlePic.ToString() != "")
                {
              var crop = n.articlePic.Find("@name","cat small").url;
              var myDate = n.CreateDate.ToString("MMMM dd yyyy");
              var excerpt = umbraco.library.StripHtml(@n.contentBody.ToString());
              var dateToGet = n.createDate.ToString("ddMMMyyyy");
              var dateActual = DateTime.Now.ToString("ddMMMyyyy");
             
               
                   
                           
                            <li>
                            <div class="image">
                            <a href="@n.Url"><img src="@crop" alt="category pic"></a>
                            </div><!--image-->
                            <div class="details">
                            <h5><a href="@n.Url">@MyHelpers.Truncate(excerpt, 61)</a></h5>
                            <span class="date">@myDate</span>
                            </div><!--details-->
                            </li>
              
                   }
            }

  • Sander Houttekier 114 posts 163 karma points
    Jul 31, 2011 @ 21:47
    Sander Houttekier
    0

    Reason i used uQuery was to aggregate both the event and newsitem docType

    any idea how you would use that in your situation without lots of tricky stuff :)

    hm maybe it's easier than i thought, 

    i could just do .Descendants().Where()  nodetypealias = 1 or 2 ...

    let me try, i'll get back to you ... but this seems like its going to solve my problem



    side question:
    i notice you use  Take(8) just to take the first 8 most likely
    do you have funcionality like paging or "read more" then?

    because it seems like you do your take(8) always from the same starting collection
    (i was just wondering because its highly possible that i will have to figure out something like continues 'fetch more articles' in that list...
    with ajax most likely, so i will have to figure out a clean way to fetch more items not having to load them all at once in the beginning.

    now its not a problem but in a few months loading all would seem harsh. when a user does not need them all :)

    anyway, first the sorting
    thanks! 

  • Toni Becker 146 posts 425 karma points
    Jul 31, 2011 @ 22:16
    Toni Becker
    0

    Hmm you don't need uql in Razor. You can cast your nodes into a new List<> this can look like this:

    @{
                   
    var newNodeList =newList<DynamicNode>();
                   
    foreach(dynamic node in@Model.AncestorOrSelf(1).DescendantsOrSelf().OrderBy("CreateDate desc"))  //here you allready have your sorting:=)
    {
                           
    if(node.NodeTypeAlias=="YourFirstDocType"|| page.NodeTypeAlias=="SecondDocType")
                           
    {
                                    newNodeList
    .Add(nodes);
                           
    }
                   
    }
           
    }

            @foreach(dynamic node in newNodeList.Take(5)){
                   
    <span>@page.Name</span>
           
    }
    For the other Code i will have a look tomorrow.

    So long.

  • Toni Becker 146 posts 425 karma points
    Jul 31, 2011 @ 22:17
    Toni Becker
    0

    Have to corret the first Line. It should be:

    var newNodeList = new List<DynamicNode>();

Please Sign in or register to post replies

Write your reply to:

Draft