Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 936 posts 2571 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 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 11, 2021 @ 15:31
    Lee Kelleher
    102

    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.

  • Stefano Beretta 101 posts 246 karma points
    Jan 13, 2023 @ 14:12
    Stefano Beretta
    1

    Thank you for this lifesaver snippet!

    I hate to ask for more, but maybe could you also provide a snippet to get the configuration for the editor? I'd like to know the values of the fileds like "max, min" and "allow multiple".

    Thank you so much !!!

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 13, 2023 @ 14:55
    Lee Kelleher
    2

    Hi Stefano,

    Following on from the previous code snippet (above), once you have the dataType.Configuration as a Dictionary<string, object>, you can get the list-editor's configuration options from the "listEditor" key.

    e.g. something similar to this...

    if (dataType.Configuration is Dictionary<string, object> config &&
        config.TryGetValue("listEditor", out var obj2) == true &&
        obj2 is Newtonsoft.Json.Linq.JArray array2 &&
        array2.Count > 0 &&
        array2[0] is Newtonsoft.Json.Linq.JObject item2)
        {
            // do what you need to with `item2`
            // e.g. `item2["enableMultiple"]`
        }
    

    I hope this helps.

    Cheers,
    - Lee

  • Claushingebjerg 936 posts 2571 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