Copied to clipboard

Flag this post as spam?

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


  • Mads Vester 4 posts 74 karma points
    Feb 14, 2021 @ 16:14
    Mads Vester
    0

    Umbraco query builder

    Hi, I am new to Umbraco (+code :-) and have a question about the query builder. It works fine and I get a list of teasers rendered out on the frontend. However I want to filter out the teaser for the current page. How can I do that?: This is the output from the query builder:

    @{ var selection = Umbraco.Content(Guid.Parse("e9e95459-24af-4e83-8bbd-959a6c4cad32")) .Children() .Where(x => x.IsVisible()) .OrderBy(x => x.Name); }

    Thanks in advance

  • Thomas Kassos 54 posts 265 karma points
    Feb 14, 2021 @ 23:01
    Thomas Kassos
    0

    Hi Mads,

    If your nodes are like that

    Home

    -Page (document type alias"page")

    • Child 1

    • Child 2

    • Child 3

    -Page 2 (document type alias "page2")

    • Child 1

    • Child 2

    • Child 3

    And if you are on the page 1 and you want page1 children or if you are on the page 2 and you want page2 children then you can do that

    @{
    var selection = Model.Children();
    }
    

    If you are on the home page and you want the nodes of the page1 then you can do that

    @{
    var selection = Model.FirstChild(x => x.IsDocumentType("page1")).Children();
    }
    

    Hope that helps.

    Thanks, T

  • Mads Vester 4 posts 74 karma points
    Feb 15, 2021 @ 07:41
    Mads Vester
    0

    Hi Thomas,

    Thanks for your quick reply! If I am on a children page of page 1. I right now have all the children of page 1 as teasers on this children page- That works fine (I use a blocklist block with the query).

    But I want all the children of page 1 except the children page that the user is on, since it does not make sense for the user to click on a teaser for a page that they are currently on. So I need to filter out the current children page, so that I can use the same blocklist block (with the same query builder) for each children page on page 1. (on page 2 I have another blocklist block with a new query builder). ...So on Page 1:Children 1 page the user is presented with a list of Page1:Children 2 and 3

    ...and on Page 1: Children 2 page the user is presented with a list of Page 1: Children 1 and 3.

    Hope it makes sense.

    Best regards Mads Vester

  • Thomas Kassos 54 posts 265 karma points
    Feb 15, 2021 @ 08:56
    Thomas Kassos
    0

    Hi Mads,

    I think I get it now what you want to achieve. Personally I like better to see the page I am on with an active class so I have 2 different things for you to try

    @{
        var selection = Umbraco.Content(Guid.Parse("e9e95459-24af-4e83-8bbd-959a6c4cad32"))
        .Children()
        .Where(x => x.IsVisible())
        .OrderBy(x => x.Name);
    
    
    if (selection != null && selection.Any())
    {
        //add an active class if is the current page
        <ul>
            @foreach (var item in selection)
            {
                var isCurrentPage = Model.Id == item.Id;
                <li>
                    <a href="@(isCurrentPage == false ? item.Url: string.Empty)" class="@(isCurrentPage ? "active" : string.Empty)">@item.Name</a>
                </li>
            }
        </ul>
    
    
        //exclude the current page from the list
        <ul>
            @foreach (var item in selection)
            {
                var isCurrentPage = Model.Id == item.Id;
    
                if (isCurrentPage == false)
                {
                    <li>
                        <a href="@item.Url">@item.Name</a>
                    </li>
                }
            }
        </ul>
    }
    }
    

    Or exclude the page in your selection, so you dont have to change your code

    var selection = Umbraco.Content(Guid.Parse("e9e95459-24af-4e83-8bbd-959a6c4cad32"))
        .Children()
        .Where(x => x.IsVisible() && x.Id != Model.Id)
        .OrderBy(x => x.Name);
    

    Let me know if that helped

  • Mads Vester 4 posts 74 karma points
    Feb 15, 2021 @ 09:43
    Mads Vester
    0

    Thanks again. I have tried both solutions, but I get a Runtime error on both :-/

    Best regards Mads Vester

  • Thomas Kassos 54 posts 265 karma points
    Feb 15, 2021 @ 10:10
    Thomas Kassos
    0

    Hi Mads,

    What's the error?

    Thanks, T

Please Sign in or register to post replies

Write your reply to:

Draft