Copied to clipboard

Flag this post as spam?

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


  • jon 2 posts 82 karma points
    Oct 29, 2019 @ 09:13
    jon
    0

    ordering child nodes in parent by user dropdown selection umbraco

    I am attempting to organise a blog list page by a user dropdown selection (either ascending or descending order based on datePublished value).

    @{
            var selection = Umbraco.Content(Guid.Parse("74c222ad-f087-4c53-9523-60de11657a0a"))
                .Children()
                .Where(x => x.IsVisible())
                .OrderByDescending(x => x.Value("datePublished"));
        }
                            <div>
                                <select>
                                    <option label="Date: Newest to Oldest" value="1">Date: Newest to Oldest</option>
                                    <option label="Date: Oldest to Newest" value="2">Date: Oldest to Newest</option>
                                </select>
                            </div>
    
                            <ul>
                                @foreach (var item in selection)
                                {
                                    <li>
                                        @if (item.HasValue("newsImage"))
                                                    {
                                                            {
                                                                var image = item.Value<IPublishedContent>("newsImage");
                                                                <a href="@item.Url"><img class="block__image" alt="Alt - @item.Name" src="@image.Url"></a>
                                                            }
                                                    }
                                    </li>
                                }
                            </ul>
    

    I have managed to render the child nodes in descending order but I can't think of how best to solve this problem.

    I have reduced the code to keep it simple but any help would be greatly appreciated.

  • MB 97 posts 365 karma points
    Oct 29, 2019 @ 09:58
    MB
    100

    Possibly... date as a data-attribute on the LI items, and then sort them via jscript onchange of the select. You'll find lots of examples on the interwebs.

  • jon 2 posts 82 karma points
    Oct 29, 2019 @ 13:26
    jon
    0

    Perfect, thank you. This has been wracking my head for the past day now. I was trying to find a solution using the razor syntax and went down a rabbit hole.

  • 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