Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 925 posts 2527 karma points
    Nov 11, 2021 @ 15:08
    Claushingebjerg
    0

    Render dataliste prevalues frontend

    Hi Lee

    Is there a simple razor way to render the all the prevalues of a datalist in the frontend?

    I would like to use the in a form, to filter the selected values chosen by the user on the doc.

  • Lee Kelleher 4004 posts 15699 karma points MVP 12x admin c-trib
    Nov 11, 2021 @ 15:31
    Lee Kelleher
    100

    Hi Claus,

    It's doable, but it's not pretty! 😂

    Firstly, which version of Umbraco? I'm assuming v8.

    Secondly, which type of Data List data-source are you using?

    As there may be a gotcha with the "Umbraco Content" ones, as they were specifically to designed to only work in the backoffice content section. But that depends on the type of XPath query, e.g. $current has no context on the frontend.

    Okay, with that disclaimer aside, here's something I've knocked-up... warning, like I say, it's not pretty.

    @{
        var dataType = Services.DataTypeService.GetDataType(Guid.Parse("91ab7d80-a836-4d29-9ada-2e40d18e0b85"));
        if (dataType.Configuration is Dictionary<string, object> config && config.TryGetValue("dataSource", out var obj1) == true &&
            obj1 is Newtonsoft.Json.Linq.JArray array1 &&
            array1.Count > 0 &&
            array1[0] is Newtonsoft.Json.Linq.JObject item1)
        {
            var configurationEditorUtility = DependencyResolver.Current.GetService<Umbraco.Community.Contentment.DataEditors.ConfigurationEditorUtility>();
            var source = configurationEditorUtility.GetConfigurationEditor<Umbraco.Community.Contentment.DataEditors.IDataListSource>(item1.Value<string>("key"));
            if (source != null)
            {
                var sourceConfig = item1["value"].ToObject<Dictionary<string, object>>();
                var items = source?.GetItems(sourceConfig);
                if (items?.Any() == true)
                {
                    <select>
                        @foreach (var item in items)
                        {
                            <option value="@item.Value">@item.Name</option>
                        }
                    </select>
                }
            }
        }
    }
    

    The DataTypeService.GetDataType() call can either accept the DataType's ID (int or Guid) or the name (string). IMO the Guid is best.

    I wasn't sure what type of HTML input/select you wanted, so the example is a dropdown. Hopefully it's easily tweakable to change to whatever markup you need.

    Hope this helps?

    Cheers,
    - Lee

    PS. If this is useful, I could make a friendlier Razor helper method for this.

  • Claushingebjerg 925 posts 2527 karma points
    Nov 11, 2021 @ 18:42
    Claushingebjerg
    0

    Yup, thats "perfect" ;) - works like a charm!

Please Sign in or register to post replies

Write your reply to:

Draft