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.
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".
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"]`
}
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.
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.
The
DataTypeService.GetDataType()
call can either accept the DataType's ID (int
orGuid
) or the name (string
). IMO theGuid
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.
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 !!!
Hi Stefano,
Following on from the previous code snippet (above), once you have the
dataType.Configuration
as aDictionary<string, object>
, you can get the list-editor's configuration options from the"listEditor"
key.e.g. something similar to this...
I hope this helps.
Cheers,
- Lee
Yup, thats "perfect" ;) - works like a charm!
is working on a reply...