Copied to clipboard

Flag this post as spam?

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


  • Hawthorn 10 posts 122 karma points
    Sep 18, 2017 @ 11:13
    Hawthorn
    0

    Filter Children by Multinode Tree picker contents

    I have a directory of companies, which have 2 properties which are got from multinode tree pickers, Country and Activity. The contents of these are displayed as a list of checkboxes, and I need to be able to filter the directory according to what is checked.

    I am having difficulty getting my head around this. Code is as follows - (with lots of things like styling and accordions and pagination stripped out so there might be tags and brackets missing - the page does work as is without filtering)

    I have 2 lists which get the parameters coming in and then combine to one list of what needs to be filtered against. I just don't know how to query the items in showItems helper function to only show these companies. ETA - Should I be able to write something like:

       var items = node.Children.Where(x => (x.IsDocumentType(Rnm_StakeholderCompany.ModelTypeAlias)) && x.IsVisible() && (x.GetPropertyValue<IEnumerable<String>>("Country").Contains("1493")));
    

    I'm getting "Exception Details: System.ArgumentNullException: Value cannot be null. "on this line

    (using uskinned Source theme if that makes a difference)

      @inherits UmbracoViewPage<USNSourceWeb.App_Code.USNModels.USNBaseViewModel>
    @using USNStarterKit.USNHelpers;
    @using System.Globalization;
    
    @{
        Layout = "USNMaster.cshtml";
        Rnm_StakeholderDirectory currentPage = (Rnm_StakeholderDirectory)Model.Content;
    
        // Get filter Terms - 2 filter lists joined together
        string activityName = !String.IsNullOrEmpty(Request.QueryString["a"]) ? Request.QueryString["a"] : "";
        List<string> afilterList = activityName.Split(',').ToList();
    
        string countryName = !String.IsNullOrEmpty(Request.QueryString["c"]) ? Request.QueryString["c"] : "";
        List<string> cfilterList = countryName.Split(',').ToList();
    
        afilterList.AddRange(cfilterList);
        foreach(var str in afilterList)
        {
            <p>@str</p>
        }
    
    
        Umbraco.TypedMedia().Where(Property("Country")(afilterList));
        IPublishedContent activityList = Umbraco.TypedMedia(Rnm_StakeholderCompany.GetModelPropertyType<string>("Activity"));
    
    
    
    
        int pageSize = int.TryParse(currentPage.GetProperty("pageSize").Value.ToString(), out pageSize) ? pageSize : 15;
        int page = int.TryParse(Request.QueryString["page"], out page) ? page : 1;
    
    
        IEnumerable<IPublishedContent> posts = currentPage.Children.Where(x => (x.IsDocumentType(Rnm_StakeholderCompany.ModelTypeAlias)) && x.IsVisible());
        //if (activityName != null)
        //{
        //posts = currentPage.Children.Where(x => (x.GetPropertyValue<string>("Activity").Split(",".ToCharArray()).Contains("Trade")) && x.IsVisible());
    
        //}
    
        @*foreach (var onepage in posts)
        {
            <span>@onepage.GetPropertyValue("Activity").ToString().Split(",".ToCharArray())</span>
        }*@
    
    int postCount = posts.Count();
    var totalPages = (int)Math.Ceiling((double)postCount / (double)pageSize);
    
    IPublishedContent parent = currentPage.AncestorOrSelf(1);
    IEnumerable<IPublishedContent> activitylist = parent.Children().Where(x => (x.IsDocumentType(ActivityList.ModelTypeAlias)) && x.IsVisible());
    IPublishedContent activities = activitylist.First();
    
    IEnumerable<IPublishedContent> countrylist = parent.Children().Where(x => (x.IsDocumentType(CountryList.ModelTypeAlias)) && x.IsVisible());
    IPublishedContent countries = countrylist.First();
    
    }
    
                @Html.Partial("USNMiscPageElements/USN_StandardPageHeaderSection", Model)
    
    
    <!-- CONTENT -->
    <section class="content">
            @{
    
    
                if (posts.Count() > 0)
                {
                    <div class="container">
    
    
                                <form method="get" action="@Request.Url.ToString()">
    
                                    <p>
                                        FILTER LIST BY: ACTIVITY/INTEREST 
                                                    <ul class="directory-filter">
                                                        @foreach (IPublishedContent item in activities.Children().OrderBy("SortOrder"))
                                                        {
                                                            string selected = "";
                                                            foreach (string str in afilterList)
                                                            {
                                                                if (item.Name == str)
                                                                {
                                                                    selected = "checked";
                                                                }
                                                            }
                                                            <li><input type="checkbox" name="a" value="@item.Id" @selected />@item.Name</li>
                                                        }
                                                    </ul>
    
                                                        COUNTRY 
                                                    <ul class="directory-filter">
                                                        @foreach (IPublishedContent item in countries.Children().OrderBy("SortOrder"))
                                                        {
                                                            string selected = "";
                                                            foreach (string str in afilterList)
                                                            {
                                                                if (item.Name == str)
                                                                {
                                                                    selected = "checked";
                                                                }
                                                            }
                                                            <li><input type="checkbox" name="c" value="@item.Id" @selected />@item.Name</li>
                                                        }
                                                    </ul>
    
    
                         <button type="submit" class="btn btn-sm c1-bg c6-text">Submit</button>
                        </form>
                    </div>
            <div class="directory-grid col-md-9 col-xs-12">
                    @showItems(@currentPage, (pageSize), page)    
            </div>
    
    
    
    
    
                // pagination
    
            }
    
    
        }
        <!--// CONTAINER standard -->
    
    </section>
    
    <!--// CONTENT -->
    <!-------   EACH INDIVIDUAL ITEM  ------->
    
    @helper showItems(IPublishedContent node, int count, int page)
    {
    var items = node.Children.Where(x => (x.IsDocumentType(Rnm_StakeholderCompany.ModelTypeAlias)) && x.IsVisible()).Skip((page - 1) * count).Take(count);
    
    //var items = node.Children.Where(x => (x.IsDocumentType(Rnm_StakeholderCompany.ModelTypeAlias)) && x.IsVisible() && (x.GetPropertyValue<IEnumerable<IPublishedContent>>("Country").ToString() == "Austria")).Skip((page - 1) * count).Take(count);
    
    
    //var careersList = Umbraco.TypedContentAtRoot().DescendantsOrSelf("CareersList").FirstOrDefault();
    //var careerItems = careersList.Children.Where("Visible");
    
    //var items = node.Children.Where(x => (x.GetPropertyValue<string>("Country")=="Austria"));
    
    foreach (var item in items)
    {
        string itemTitle = item.HasValue("onPageTitle") ? itemTitle = item.GetProperty("onPageTitle").Value.ToString() : item.Name;
    
    
            <div class="row directory-company">                
                <div class="col-xs-10">
                    <p><a href="@item.Url">@itemTitle</a></p>
                </div>
    
            </div>
    
        var people = item.Children.Where(x => (x.IsDocumentType(PersonListing.ModelTypeAlias)) && x.IsVisible());
        foreach (var person in people)
        {
            string personTitle = person.HasValue("onPageTitle") ? personTitle = person.GetProperty("onPageTitle").Value.ToString() : person.Name;
                <div class="row directory-person">
                    <div class="col-md-5 col-xs-12 col-md-offset-1">
                        <p><a href="@person.Url">@personTitle</a></p>
                    </div>
    
                </div>
            }
    }
    
    }
    
  • Liam Dilley 103 posts 271 karma points
    May 13, 2020 @ 03:19
    Liam Dilley
    0

    This is a very old post but did you ever figure this out. Umbraco 8 and I am getting the same things with no real solutions that work that I can see.

  • AWatters 6 posts 75 karma points
    May 13, 2020 @ 08:13
    AWatters
    0

    I see from your other post that you found a solution. I was going to say that I ended up making a function:

     public static IEnumerable<IPublishedContent> AndIsLinkedToCountry(this IEnumerable<IPublishedContent> content, List<int> countryIds)
    
        {
            foreach (IPublishedContent c in content)
            {
                foreach (IPublishedContent country in c.GetPropertyValue<IEnumerable<IPublishedContent>>("Country"))
                {
                    if (countryIds.Contains(country.Id))
                    {
                        yield return c;
                    }
                }
            }
        }
    

    And then in the view:

    IEnumerable<IPublishedContent> items = currentPage.Children.Where(x => (x.IsDocumentType(Rnm_StakeholderCompany.ModelTypeAlias)) && x.IsVisible());
    if (cfilterList.Count() > 0)
    {
        items = items.AndIsLinkedToCountry(cfilterList);
    }
    

    Will have to check out your solution too.

  • Liam Dilley 103 posts 271 karma points
    May 14, 2020 @ 01:41
    Liam Dilley
    0

    I saw your example and others where people tried to make a string or an array of the items and check in there but could not get it to work in Umbraco 8. The solution was to check the multinodepicker directly but because it may be empty sometimes and can not match an ID it through either an object error or value null error. Checking to ensure not null first in a where then running through works well.

  • Edgar Rasquin 255 posts 700 karma points
    Mar 25, 2021 @ 16:56
    Edgar Rasquin
    0

    Sorry for asking a probably silly question.

    But did you place your function in a class?

    I dont get the concept of spreading code logic in in views and.. classes, etc...

  • AWatters 6 posts 75 karma points
    Mar 25, 2021 @ 18:13
    AWatters
    2

    I put my function in a class, yes:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text;
    using System.Threading.Tasks;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    
    namespace MySite.App_Code.USNHelpers
    {
        public static class MyHelpers
        {       
    
            public static IEnumerable<IPublishedContent> AndIsLinkedToCountry(this IEnumerable<IPublishedContent> content, List<int> countryIds)
    
            {
                foreach (IPublishedContent c in content)
                {
                    foreach (IPublishedContent country in c.GetPropertyValue<IEnumerable<IPublishedContent>>("Country"))
                    {
                        if (countryIds.Contains(country.Id))
                        {
                            yield return c;
                        }
                    }
                }
            }
    }
    }
    

    in a file called App_Code/MyHelpers.cs and then called it from the View like so:

    @using MySite.App_Code.MyHelpers;
    
    .....
    
    if (cfilterList.Count() > 0)
    {
        items = items.AndIsLinkedToCountry(cfilterList);
    }
    

    Sometimes I'd have Models, Logic, Controllers etc but not in this case. HTH?

Please Sign in or register to post replies

Write your reply to:

Draft