Copied to clipboard

Flag this post as spam?

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


  • BEWD 89 posts 301 karma points
    May 12, 2016 @ 17:26
    BEWD
    0

    Filtering Partial View based on the page?

    Hi

    I have a structure that looks like this

    Home 
    -Landing Page 1
    -Landing Page 2
    -Landing Page 3
    -Landing Page 4
    -News Area
    --News Article 1
    --News Article 2
    --News Article 3
    --News Article 4
    

    The News Article pages have a checkbox list (location) and each one is tagged with either (Landing Page 1, Landing Page 2, Landing Page 3, Landing Page 4 or All).

    So for example when you view the Landing Page 2 page I only want to display the News Articles that have been tagged with Landing Page 2 (eg, have the checkbox ‘Landing Page 2’ option checked) OR the ‘All’ Checkbox checked.

    The Partial View I have is below but this obviously just displays all articles on all pages:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{var selection = CurrentPage.Site().FirstChild("newsArea").Children("newsItem").Where("Visible").OrderBy("CreateDate desc");}
    
    <ul id="NewsList">
    @foreach(var item in selection){
            <li data-value="@item.location">
                <a href="@item.Url" >@item.Name</a>
            </li>
        }
    </ul> 
    

    Does anyone know of a way that I can filter what is displayed? Ideally I would like to say something like, display where location = pageName or ‘All’ but I can’t figure out how to do this or at least I cant get it to work?

    Is it do-able or am I going at in the wrong way?

    Ben

  • David Peck 687 posts 1863 karma points c-trib
    May 12, 2016 @ 21:03
    David Peck
    0

    I'd use the nupickers (xml checkboxes) as the data type for your location picker, and on the data type configure it to use a relation. That way you can use the relation service to select just the articles you need.

    The complexity is the all option. I've got a feeling the nupicker can be configured to add an all checkbox that just ticks other checkbook, if so that might be enough. If not you could either have a content item to represent all and just grab all news articles related to that or you could possible listen to content saved events and create your all relations then.

    If you don't want to use relations then try lucene. If your property is a csv of locations the you could do a 'where locations contains ,1001, or locations contains ,1045,' sort of query.

    I'd go relations personally.

  • BEWD 89 posts 301 karma points
    May 12, 2016 @ 21:17
    BEWD
    0

    So its not possible 'out the box' easily?

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    May 12, 2016 @ 21:40
    Nicholas Westby
    0

    I wouldn't recommend using checkboxes at all. Instead, I'd recommend using a multinode tree picker (MNTP). On your news articles, you can have a MNTP property that allows the user to select which landing page nodes the article applies to. If they select none, it would imply that it applies to all of them. Alternatively, you could have a separate checkbox property, "Applies to All Landing Pages".

    Then, on your landing page, you'd scan for any news articles that have the checkbox checked or that have picked the current landing page.

    An alternative would be to invert the selection. That is, each landing page could have a multinode tree picker that allows the user to select which articles to display on that page. That might be the more performant option, depending on your needs.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    May 13, 2016 @ 08:35
    Douglas Robar
    100

    Checkboxes aren't the best choice in this situation. You' really want to have a way to get to the selected item(s) directly and quickly. Definitely consider the options David and Nicholas suggest.

    If you haven't seen it, the built-in Related Links (https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/related-links) can be very useful in this situation though the good old MNTP is also super handy. Both are very configurable for the best user experience and writing a bit of Razor to display the chosen items isn't too complex.

    And as Nicholas mentioned, consider if it makes more sense to have content editors select the news articles from a landing page, or the landing page from a news article.

    The beauty of Umbraco is that you can craft the user experience and the website logic to be exactly what you need in less time than looking through a bunch of modules only to find none of them does exactly what you want.

    Once you decide on which property editor to use and where it belongs (on the news items or the landing pages), shout if you have any trouble getting the Razor to work. One tip: if you are using the MNTP, check out the built-in snippet in Umbraco for them when creating your Partial View to give you the key logic ready for your tweaks.

    cheers,
    doug.

  • BEWD 89 posts 301 karma points
    May 13, 2016 @ 16:25
    BEWD
    0

    Thanks All, I will look at changing the approach i'm taking

Please Sign in or register to post replies

Write your reply to:

Draft