Copied to clipboard

Flag this post as spam?

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


  • curtismccaw 27 posts 87 karma points
    Mar 31, 2023 @ 20:17
    curtismccaw
    0

    Sharing multi tree node picker value across partials

    Hi,

    I have a staff listing section on my site for all members of the organisation. Each staff member can be assigned to an office based on selection through a multi tree node picker property.

    For example, person A works at Office A, Person B also works at Office A. Person C works at Office B and so on.

    I then have a landing page for each office and would like to dynamically loop through the staff listing and find the people who work at that office.

    E.g. on Office A landing page, I would see 2 people based on my example above.

    What would be the best way to approach this? Staff Listing, with people listed inside.

    On staff profile, admin can select which office they belong to. Many thanks

  • curtismccaw 27 posts 87 karma points
    Mar 31, 2023 @ 20:37
    curtismccaw
    0
    <h3>Meet our people</h3>
    
    var OfficeId = Model.Id;
        <p>@OfficeId</p>
        @* loop through people and find any that have united kingdom as office *@
    
    @{
    var selection = Umbraco.Content(Guid.Parse("9d4127b2-fbbf-4e38-b38e-a09adcf3657c"))
    .ChildrenOfType("staffMember")
    .Where(x => x.IsVisible())
    .OrderBy(x => x.Name);
     }
      <ul>
        @foreach (var item in selection)
        {
        <li>
            <a href="@item.Url()">@item.Name()</a>
            @item.Value("Position")
            @item.Value("EmailAddress")
            @item.Value("ContactNumber")
    
             @{
            var office = item.Value<IEnumerable<IPublishedContent>>("office");
            foreach (var o in office)
            {
                <p>Office: @o.Name</p>
                <p>Office ID: @o.Id</p>
                <a href="@o.Parent.UrlSegment/@o.UrlSegment">View @o.Name office</a>
                <p>Region: @o.Parent.Name</p>
                <a href="@o.Parent.UrlSegment">@o.Parent.Name</a>
                <hr>
            }
        }
        </li>
    }
    

    So far I have this, I can loop through all people and get their office - but I need to somehow only get staff that match the current office Id i'm viewing. In this case: Id 1131.

  • curtismccaw 27 posts 87 karma points
    Apr 03, 2023 @ 08:48
    curtismccaw
    0

    Anyone?

  • Fiona 16 posts 197 karma points
    Apr 03, 2023 @ 11:12
    Fiona
    100

    Maybe you can add a match condition to the where in the selection variable to only get the staffMembers that have the right office selected? Something like:

    .Where(x => x.IsVisible() && x.Value<IEnumerable<IPublishedContent>>("office").Any(y => y.Key == Model.Key))?
    

    Not sure if you have to use the key in this case or if y.Id == OfficeId would work as well.

    Edit: caution that in the code line above x.Value<IEnumerable<IPublishedContent>>("office") might come back as null and will throw you an error on the Any(). Might be better to add a null-check.

  • curtismccaw 27 posts 87 karma points
    Apr 03, 2023 @ 12:22
    curtismccaw
    0

    Thanks! I've been able to work with something similar and that's working. Thanks for your help.

    Many thanks

Please Sign in or register to post replies

Write your reply to:

Draft