Copied to clipboard

Flag this post as spam?

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


  • jools 15 posts 79 karma points
    Jan 29, 2018 @ 09:34
    jools
    0

    Display number of Children for a specific name Node

    Hi

    Im using this code to display the number of children underneath a specific node. At the moment it is doing so via the node's ID. Is there a way to set it so it goes off the Node's Name instead?

    code:

    var MYnode = Umbraco.TypedContent(1562); var MYchildAmount = MYnode.Children.Count();

    Currently @MYchildAmount jobs

    Thanks

  • Steve Morgan 1350 posts 4460 karma points c-trib
    Jan 29, 2018 @ 12:12
    Steve Morgan
    0

    Hi Jools,

    Something like should work - you get the website root node and then work down from there:

    var siteNode = Umbraco.TypedContentAtRoot().First();
    var locationsNode = siteNode.Children.Where(x => x.Name == "Locations").FirstOrDefault();
    var jobs = locationsNode.Children.Where(x => x.DocumentTypeAlias == "job").Count();
    

    I'm guessing jobs are in a location (this might be department or whatever so change this to suit). The last like counts the jobs - if you only have jobs under these containers you could remove the where check on the docType.

    This is a simple example that does no null checking. You probably should check if the locations node exists on the count.

        var jobs = locationsNode != null ? locationsNode.Children.Where(x => x.DocumentTypeAlias == "propertyArea").Count() : 0;
    
  • jools 15 posts 79 karma points
    Jan 29, 2018 @ 15:22
    jools
    1

    Thanks Steve - that works great.

    Im just trying to develop it further using a loop, but being a designer my razor is very limited.

    <div class="box">
         <h2>IT Jobs</h2>
         <div class="job-info">
              <div class="count">There are currently @ITchildAmount jobs</div>
              <a href="/it-jobs/">View jobs</a>
         <div>
         <p class="no-jobs">Sorry no jobs available</p>
    <div>
    

    So all i want to do, is if the @ITchildAmount = 0 then hide the 'job-info' div and show the 'no-jobs' div instead.

    Trying to get a for loop going but doing something wrong.

    Might you be able to point me in the right direction?

    Thanks again

  • jools 15 posts 79 karma points
    Jan 29, 2018 @ 16:14
    jools
    1

    Actually scratch that I’ve cracked it and got it working :)

  • Steve Morgan 1350 posts 4460 karma points c-trib
    Jan 29, 2018 @ 18:17
    Steve Morgan
    0

    Good job - glad you sorted it!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies