x First time here? Check out the FAQ

Learn from 350 other Umbracians at the annual Umbraco Conference - CodeGarden '13.
More than twenty high quality sessions, open spaces, hackathons and social events you'll remember. Not to be missed!
Less than 25 tickets left - get yours now!

  • Avatar53posts24karma

    Display descendants nodes with a list

    blackdog started this topic March 20, 2012 @ 03:55 , this topic was edited at: Wednesday, March 21, 2012 12:32 PM

    Hi to all,

    i'm gabriele from italy and i wrote here many times ago! How are you?

    I'm starting to re-use umbraco with razor... And i 'd like to ask you some tips...

    Now i've a content like this:

    News

    --Year

    ----Node

    I tried to use this razor scripts www.diplo.co.uk/.../using-razor-in-umbraco-47.aspx

    to list the nodes divided per year i use this query but i havenm't the results

    var nodes = Model.Children.Where("Notizie").OrderBy("displayDate desc");

     

    Any suggestions?


  • Replies

  • Avatar694posts811karma
    Comment with ID: 111503
    Rodion Novoselov posted this reply March 21, 2012 @ 10:34

    Hi. The "GroupBy" method could be helpful:

    @{
     var yearGroups =  Model.Children.GroupBy("DisplayDate.Year");
     foreach(var yearGroup in yearGroups) {
            <h2>@ yearGroup.Key</h2>
            foreach(var node in  yearGroup) {
                <p>@node.Name</p>
            }
        }
    }

     


  • blackdog posted this reply March 21, 2012 @ 11:57

    Thanks for your reply... but this query don't returns any result...

    in var yearGroups inside groupby what do yuo mean with displayDate.Year this method where brings the date of my years folder..

    and when start for each i haven't node displayed... Mmmmhhh razor could drive me crazy! :)

     

    Thanks for your response

    Bye

     


  • Avatar694posts811karma
    Comment with ID: 111537
    Rodion Novoselov posted this reply March 21, 2012 @ 12:10

    Hi. I have got the name "displayDate" from your own query. Your code lets to guess that it's supposed to be some custom property of your document type. Isn't that?

    The "Year" is just a property of the System.DateTime .NET type, so .GroupBy("DisplayDate.Year") should group the list of nodes by the year part of "DisplayDate".

     


  • blackdog posted this reply March 21, 2012 @ 12:32

    This is my entire script:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        int pageSize; // How many items per page
        int page; // The page we are viewing

        /* Set up parameters */
        
        if (!int.TryParse(Parameter.PageSize, out pageSize))
        {
            pageSize = 6;
        }

        if (!int.TryParse(Request.QueryString["page"], out page))
        {
            page = 1;
        }

        /* This is your basic query to select the nodes you want */

        var nodes = Model.Descendants.Where("catCount > 2").OrderBy("displayDate desc");
        
        int totalNodes = nodes.Count();
        int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
        
        /* Bounds checking */
        
        if (page > totalPages)
        {
            page = totalPages;  
        }
        else if (page < 1)
        {
            page = 1;
        }
    }

    <h2>Found @totalNodes results. Showing Page @page of @totalPages</h2>

    <ul>
        @foreach (var item in nodes.Skip((page - 1) * pageSize).Take(pageSize))
        {
            <li><a href="@item.Url">@item.TitoloNews</a> (@item.DisplayDate.ToShortDateString())</li>
        }
    </ul>

    <ul class="paging">
        @for (int p = 1; p < totalPages + 1; p++)
        {
            string selected = (p == page) ? "selected" : String.Empty;
            <li class="@selected"><a href="?page=@p" title="Go to page @p of results">@p</a></li>
        }
    </ul>

     

    The content tree is:

    Homepage-

       Notizie (DT folder)
         Year (DT Folder)
            Node (DT page)

     

    when i start this script i have 0 results
    This is my problem!

     

    Thanks for your patience

    gabriele


Please login or Sign up To post replies