Copied to clipboard

Flag this post as spam?

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


  • David Hammer 41 posts 67 karma points
    Mar 07, 2014 @ 15:08
    David Hammer
    0

    List of all childrens children

    Hi guys!

    Right now I print a list of the first year's children seeing as thats how far I've come.

    The Content structure looks like this:

    Articles

    2013

    Article 1

    Article 2

    2014

    Article 1

    Article 2

    And the code I have so far looks like this:

    var root = Model.AncestorOrSelf(1).Down(1);
    var selection = root.Children.OrderBy("homepageArticleDate desc");
    @foreach (var item in selection) 
    {
        here I do stuff..
    }
    

    Now what I need to do is run through all of the years (I have from 2002-2014) and show their children (a hard way to say every single article ever written).

    And can I then still sort them from their descending date?

    Every help I can get would help, thank you !

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 07, 2014 @ 16:08
    Fuji Kusaka
    0

    Hi David,

    You could do something like this 

    List<DynamicNode> root = Model.AncestorOrSelf(1).Descendants("articleDocumentType").OrderBy("homepageArticleDate desc").Items;

    foreach(var item in root) {
    <li>@item.Name
    @if(page.Children.Where(x=>x.GetProperty("umbracoNaviHide").Value !="1").Any()){ foreach(var article in page.Children){ @article.Name } }
    </li>

    Hope this helps

    //fuji

  • David Hammer 41 posts 67 karma points
    Mar 14, 2014 @ 11:10
    David Hammer
    0

    Hey fuji

    Sorry it has taken me so long to write you back.. I have never quite understood but for some reason when I use List

    error CS0246: The type or namespace name 'DynamicNode' could not be found (are you missing a using directive or an assembly reference?)
    

    I've tried to google it, but I have had no luck in getting to know what that means, or how to solve it :(

    My code now looks like this:

    @{
        List<DynamicNode> root = Model.AncestorOrSelf(1).Descendants("homepageArticle").OrderBy("homepageArticleDate desc").Items;
        var selection = root.Children;
    }
    @foreach (var item in selection)
    {
        I do something..
    }
    

    Please help me :) thank you

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 14, 2014 @ 11:33
    Fuji Kusaka
    0

    hi David,

    Remember to add the 

    @using umbraco.MacroEngines;
  • David Hammer 41 posts 67 karma points
    Mar 14, 2014 @ 11:39
    David Hammer
    0

    Thanks for the quick answer man, great to have friendly people like you around!!!

    I added the using but now I get the following error instead:

    error CS1061: 'System.Collections.Generic.List' does not contain a definition for 'Children' and no extension method 'Children' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)
    

    What may be the reason for this, do you have any idea?

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 14, 2014 @ 12:20
    Fuji Kusaka
    0

    Remove the var selection = root.children

     

    @using umbraco.MacroEngines;
    @using umbraco.cms.businesslogic.media;
    @using umbraco.BusinessLogic;
    @inherits umbraco.MacroEngines.DynamicNodeContext 
        @{
             List<DynamicNode> root = Model.AncestorOrSelf(1).Descendants("homepageArticle").OrderBy("homepageArticleDate desc").Items;
                foreach (var item in root)
                    {
                        @item.Name
                    }
    
        }
    
    
    
  • David Hammer 41 posts 67 karma points
    Mar 14, 2014 @ 12:37
    David Hammer
    0

    Hey Fuji

    I tried what you suggested, but I now receive another error, how lucky I am :D

    This is the code for the full ArticleList.cshtml macro I have so far:

    @using umbraco.MacroEngines;
    @using umbraco.cms.businesslogic.media;
    @using umbraco.BusinessLogic;
    @inherits umbraco.MacroEngines.DynamicNodeContext 
    
    @{
        List<DynamicNode> root = Model.AncestorOrSelf(1).Descendants("HomepageArticle").OrderBy("homepageArticleDate desc").Items;
    
    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 = 20;
    }
    
    if (!int.TryParse(Request.QueryString["page"], out page))
    {
        page = 1;
    }
    
    /* This is your basic query to select the nodes you want */
    
    var olderArticles = root;
    
    int totalArticles = olderArticles.Count();
    int totalPages = (int)Math.Ceiling((double)totalArticles / (double)pageSize);
    
    /* Bounds checking */
    
    if (page > totalPages)
    {
        page = totalPages;  
    }
    else if (page < 1)
    {
        page = 1;
    }
    }
    
    <ul>
    @foreach (var item in olderArticles.Skip((page - 1) * pageSize).Take(pageSize))
    {
      if (item.HasValue("homepageArticleImage"))
      {
          <li class="article-list">   
            <div class="row">
              <div class="col-md-4 text-right">
                <a href="@item.Url"><img src="@item.Media("homepageArticleImage", "umbracoFile")"></a>
              </div>
              <div class="col-md-8">
                <h8 class="darkblue"><a href="@item.Url">@item.homepageArticleTitle</a></h8><br />
                <i>af @item.homepageArticleByline </i> <br />
                <p>@item.homepageArticleTeaser </p>
                <div style="font-size: 11px">@item.homepageArticleDate.ToShortDateString()</div>
              </div>
            </div> 
            <div class="breaker"></div>
          </li>
        }else 
        {
          <li class="article-list" style="background-color: #f5f5f5; padding: 20px; margin-bottom: 10px;">   
            <div class="row">
              <div class="col-md-12">
                <h8 class="darkblue"><a href="@item.Url">@item.homepageArticleTitle</a></h8><br />
                <i>af @item.homepageArticleByline </i> <br />
                <p>@item.homepageArticleTeaser </p>
                <div style="font-size: 11px">@item.homepageArticleDate.ToShortDateString()</div>
              </div>
            </div>
          </li>
        }
    }
    </ul>
    <div class="text-center">
      <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>         
    </div>
    

    This produces the following error:

         error CS1061: 'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'homepageArticleTitle' and no extension method 'homepageArticleTitle' accepting a first     argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are you missing a using directive or an assembly reference?)
    

    I really hope you can help.. I am trying everything, and it just seems like umbraco is against me :(

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 14, 2014 @ 12:45
    Fuji Kusaka
    0

    Hi David,

    Looks like you are working with a paging now right ? 

    In your first post you wanted to display article 1 & 2 is any is visible. Did you get this part working ?

     

  • David Hammer 41 posts 67 karma points
    Mar 14, 2014 @ 13:00
    David Hammer
    0

    Hey, yeah I have had it working with paging, but it could only take the first year.

    It was included in the macro all along, but that's not where i complains.. its working fine if I do like this:

    var root = Model.AncestorOrSelf(1).Down(1);
    var selection = root.Children.OrderBy("homepageArticleDate desc");
    @foreach (var item in selection) 
    {
         here I do stuff..
    }
    

    But yeah It only prints all the children of one of the years, and I need all children of all of the years in one big list

    I imagine that is goes wrong with the root = xx, But Ive tried all your suggestions and about 10 other and nothing works..

  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 14, 2014 @ 13:25
    Fuji Kusaka
    0

    Sorry am getting a bit confused, you basically want to display all children (Article) from year 2013, 2014 etc .. ? 

    But when you are on each individual year you want to make a paging right? 

  • David Hammer 41 posts 67 karma points
    Mar 14, 2014 @ 13:34
    David Hammer
    0

    I'm sorry I might not have explained my self well enough :/

    I have approx. 4.000 articles spread out under years in the content, like 2002,2003,2004 and so on..

    I would like it to be like this:

    You go to "Articles"

    There will be a sub menu like: 2002,2003,2004 and so on, and it then shows all the Articles from 2014 with pagination on it, when you enter, and have pagination accordingly on every other year's article list...

    Does that make any sense? and if so, can you help :D?

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Mar 14, 2014 @ 15:42
    Peter Gregory
    0

    Hi David

    So just to clarify and and sound like a stuck record.

    You would like the page to show a menu of the years... When you get to the page you see all articles listed, and paged, and in date descending order.

    If you click on one of the submenu date items you only get that years items, paged and in date descending order? Sort of like a filter?

    Or am I totally wrong?

  • David Hammer 41 posts 67 karma points
    Mar 15, 2014 @ 10:14
    David Hammer
    0

    You are exactly right!

    Can you help me with that?, How should I go about it? does it need more than one macro or?

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Mar 17, 2014 @ 03:06
    Peter Gregory
    0

    Hey David yes you will need 2 razor scripts to do this. I am. One that will form the navigation and one that will be your listing.

  • David Hammer 41 posts 67 karma points
    Mar 18, 2014 @ 11:17
    David Hammer
    0

    Hey Peter,

    Thanks for your time, it really is much appreciated!

    What I have so far:

    I've made two razor scripts. ArticleListMenu and ArticleList

    The ArticleListMenu is fairly straight forward for now:

    var root = Model.AncestorOrSelf(1).DescendantsOrSelf("ArticleArchive").First();
    
    foreach(var page in root.Children)
    {
      <a href="@page.Url">@page.Name</a>
    }
    

    The ArticleList is a bit more complex (for me anyway :):

    @{
      var root = Model.AncestorOrSelf(1).DescendantsOrSelf("ArticleArchiveYear").First();
      var articles = root.Children.OrderBy("homepageArticleDate desc, homepageArticleDate");
    
      int pageSize; // How many items per page
      int page; // The page we are viewing
    
      if (!int.TryParse(Parameter.PageSize, out pageSize)){ pageSize = 20; }
      if (!int.TryParse(Request.QueryString["page"], out page)){ page = 1; }
    
      int totalArticles = articles.Count();
      int totalPages = (int)Math.Ceiling((double)totalArticles / (double)pageSize);
    
      if (page > totalPages) { page = totalPages; }
      else if (page < 1) { page = 1; }
    }
    <ul>
      @foreach (var item in articles.Skip((page - 1) * pageSize).Take(pageSize))
      {
        if (item.HasValue("homepageArticleImage"))
        {
          <li class="article-list">   
            <div class="row">
              <div class="col-md-4 text-right">
                <a href="@item.Url"><img src="@item.Media("homepageArticleImage", "umbracoFile")"></a>
              </div>
              <div class="col-md-8">
                <h8 class="darkblue"><a href="@item.Url">@item.homepageArticleTitle</a></h8><br />
                <i>af @item.homepageArticleByline </i> <br />
                <p>@item.homepageArticleTeaser </p>
                <div style="font-size: 11px">@item.homepageArticleDate.ToShortDateString()</div>
              </div>
            </div> 
            <div class="breaker"></div>
          </li>
        }else {
          <li class="article-list" style="background-color: #f5f5f5; padding: 20px; margin-bottom: 10px;">   
            <div class="row">
              <div class="col-md-12">
                <h8 class="darkblue"><a href="@item.Url">@item.homepageArticleTitle</a></h8><br />
                <i>af @item.homepageArticleByline </i> <br />
                <p>@item.homepageArticleTeaser </p>
                <div style="font-size: 11px">@item.homepageArticleDate.ToShortDateString()</div>
              </div>
            </div>
          </li>
        }
      }
    </ul>
    <div class="text-center">
      <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>         
    </div>
    

    The menu of all years with articles in them, are displayed 2014 - 2013 - 2012, and so on, as it should. I've gotten it to print out the first year of the list in the ArticleList razor script, as I had before, with pagination (almost).

    Now here is where it gets tricky for me:

    How do I link every ArchiveYear to a list of that year's content, showing the list with pagination of course?

    And also; What is wrong with my pagination? It shows a list of 20 articles, and a pagination of 20 pages from 1-20 as links, but when I click that pagenumber - it shows the same list as page 1, on all the other 19 pages, as if it can't prober load the other articles..

    Thanks again for taking the time to read this!!!

  • sergio 1 post 71 karma points
    Dec 21, 2016 @ 23:25
    sergio
    0

    hey man, did you resolve this?? i need something quite similar

Please Sign in or register to post replies

Write your reply to:

Draft