Copied to clipboard

Flag this post as spam?

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


  • Owain Williams 345 posts 1023 karma points MVP c-trib
    Feb 07, 2019 @ 15:28
    Owain Williams
    0

    Populate a dropdown list with Month / Year of articles

    Hi, Looking for a bit of guidance. I have say 50 news articles on a site, I also have a filter to allow the user to refine their search e.g. filter by category, by topic and by date.

    Just now, I have a list of dates for the past 12 months (current month - 11 months) which is fine but if no news was published last month, January 2019 would still show on the dropdown menu.

    What I want to do is, search all the articles, get their published date then create a dropdown list from that. So, if no news is posted in January, but there was in Dec and Feb, the dropdown list would have Decemeber 2018 and February 2019 only.

    I'm thinking I need to create an array which will build up the drop down, maybe with a filter that says if the article is older than 12 months don't add - otherwise the list could get massive and I'm guessing there would be a performance hit if there are years of articles to wade through.

    Any thoughts, pointers etc would be great.

    O.

  • Bryna 32 posts 165 karma points
    Feb 07, 2019 @ 16:08
    Bryna
    0

    I can see three potential paths to ruminate:

    If the problem is framed from the perspective of search, what you describe may go away if a message displaying 'no results found' might be the easiest road to go. This logic comes from a developer mindest.

    Think in terms of broad to narrow. A user would select a year first. Then you would populate the months with the values for months which there are actually articles. I suppose the same logic could be inverted and based on the month selected you can populate all the years for which articles of the month exist. This logic comes from a SQL mindset.

    Another option would be to filter by a set date range(past month, past 3 months,Eternity,etc). This completely eliminates the need for specific dates and allows you to leverage date arithmetic. This logic comes from a Google(seeing what they appear to do) mindset.

    Hope it makes sense...

  • Owain Williams 345 posts 1023 karma points MVP c-trib
    Feb 07, 2019 @ 16:13
    Owain Williams
    0

    Sorry, not sure if I explained myself very well. The drop down looks a bit like this:

    • Jan 2018
    • Feb 2018
    • March 2018

    What I want to do is, if there are no articles in February, then the dropdown will look like this:

    • Jan 2018
    • March 2018

    But you do raise a new issue that I hadn't thought about, should I be filtering on all 3 dropdown menus all the time e.g.

    When viewing all articles, then no value is set in any of the dropdown menus and all dates are shown. If I then select a category, should I then check to see what dates the category articles have and reflect that in the date dropdown.

    Hmmm, this is growing arms and legs all of sudden. :)

  • Louis Jackson-Rees 91 posts 317 karma points c-trib
    Feb 07, 2019 @ 16:36
    Louis Jackson-Rees
    0

    would it not be a case of something like this?

    var dates = allArticle.Where(x => x.IsVisible).Select(x => x.PublishedDate).Where(x => x > DateTime.Today.AddMonths(-12))
    

    might need some castings but think thats the general idea

  • Owain Williams 345 posts 1023 karma points MVP c-trib
    Feb 15, 2019 @ 08:57
    Owain Williams
    0

    Would that not be executed on ever page load though? Just thinking from a performance point of view.

    O.

  • Louis Jackson-Rees 91 posts 317 karma points c-trib
    Feb 18, 2019 @ 10:30
    Louis Jackson-Rees
    0

    Yeah you would probably want to cache the partial or run the same sort of search / filtering through examine, if performance is an issue.

  • Dave Woestenborghs 3159 posts 10353 karma points MVP 3x admin c-trib
    Feb 18, 2019 @ 10:40
    Dave Woestenborghs
    0

    For optimal performance I would revert to either examine or a xpath query and use a xpath node navigator.

    https://our.umbraco.com/documentation/Reference/Common-Pitfalls/#xpathnodeiterator---for-when-you-need-direct-xml-support

    Use the xpath navigator to get you a list of dates. And use LINQ to get the distinct years and months from that list.

    In all cases I would avoid Descendants api

    Dave

  • Dave Woestenborghs 3159 posts 10353 karma points MVP 3x admin c-trib
    Feb 18, 2019 @ 10:44
    Dave Woestenborghs
    0

    If you need help with that. Just let me know. I'll try to to put together a code example.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft