Copied to clipboard

Flag this post as spam?

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


  • Katie 25 posts 90 karma points
    Nov 29, 2017 @ 10:37
    Katie
    0

    Getting only prevalues that are currently used in a content node

    I am currently building a site that has filters for the customer to filter the products. I am getting all the prevalues, but I was wondering if there is a way to filter them, so I only display the prevalues that are actually used in the back-end. Otherwise I am displaying long lists of options, where half of them won't return any results if selected.

    XPathNodeIterator preValueRootElementIteratorAge = umbraco.library.GetPreValues(1183);
        preValueRootElementIteratorAge.MoveNext();
        XPathNodeIterator preValueIteratorAge = preValueRootElementIteratorAge.Current.SelectChildren("preValue", "");
    

    Thank you

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Nov 29, 2017 @ 16:19
    Steve Morgan
    100

    Hi Katie,

    How many products are there? If it's a fairly small amount I would be tempted to get all products and then select this value, dedupe and order by name. Even if there's a few you could just use the code below in a cached partial to improve speed.

    Also using umbraco.library.GetPreValues hits the database I think rather than using the cache so can cause issues on high load sites.

    I'd suggest something like:

    @{
        var products = Model.Content.Children();
    
        var dropDownValues = products.Where(x => x.HasValue("testDropDown")).Select(x => x.GetPropertyValue<string>("testDropDown")).ToList();
        dropDownValues = dropDownValues.Distinct().OrderBy(x => x).ToList();
        <select>
            @foreach (var curValue in dropDownValues)
            {
                <option value="@curValue">@curValue</option>
            }
        </select>
    }
    

    You might need to @using System.Linq at the top of your razor script.

    HTH

    Steve

  • Katie 25 posts 90 karma points
    Dec 01, 2017 @ 13:26
    Katie
    0

    Thank you, that has really helped. I will have around 1500 products. I have tried your solution and it works well, but I only have 485 products loaded at the moment, so will test for speed once all are in the system.

    Thanks

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Dec 01, 2017 @ 13:44
    Steve Morgan
    0

    In that case add it as a partial and cache it or a macro if you prefer those.

    See here for details

    https://our.umbraco.org/documentation/reference/templating/mvc/partial-views

Please Sign in or register to post replies

Write your reply to:

Draft