nuPicker SQL Dropdown - How to get selected Label?
Hi Guys,
hope I'm asking in the right place.
I have a nuPicker.Picker control on a DocType, this is an SQL driven dropdown. Works fine in the backend.
I am iterating the doctypes with a fairly standard strongly typed loop. I can get the value selected with GetPropertyValue
in essence I have values such as this :
Key - Label
1 - Club 1
2 - Club Ex
3 - Club 3
4 - Club Ex
I'm using a unique integer as the key as unique labels can't be guarenteed. However to avoid multiple queries on the database I'd rather like to be able to fetch the Labels for the Picker too.
Can anyone help? is the Picker even aware of it's labels (DataSource values) at the templating level or have I no choice but to query the DB to get them again?
I see what you're saying and I've checked out the EnumDataSource namespace. however I'm using the SQL Dropdown Picker not the Enum Pickers.
I assume that then I'd somehow use the SqlDataSource or SqlDataSourceApiController, but I am at a loss as to how to operate this. Can I give you a code example?
@using
@{
var brands = Umbraco.TypedContent(1137).Descendants("Items"); //gets all the DocTypes of Items in the container.
foreach ( var brand in brands ){
Picker test = brand.GetPropertyValue<Picker>("sqlPicker");
<p>@test.PickedKeys.First().ToString()</p>
}
}
The above correctly prints out the chosen Value of the picker, but not the label. I inspected the Picker object but couldn't find anything.
In the SQL Picker, I have a simple query that pulls my value/label columns as so :
Value = Label
1 = Option 1
2 = Option 2
3 = Option 3
Do you have any ideas? perhaps a hint to help me, as I guess the jist of your answer is to use the DataSource to get the options and then iterate those to match the 'Picked Key'?
Solution, not sure if its the 'right' way or even a good way to do it, but I dug through the nuPicker Source and found some code hints. here's what I'm using, as Jerowen was getting at, we need to create the SqlDataSource and iterate it.
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@using nuPickers
@using nuPickers.Shared.SqlDataSource
@{
var brands = Umbraco.TypedContent(1137).Descendants("Items"); //gets all the DocTypes of Items in the container.
foreach ( var brand in brands ){
Picker test = brand.GetPropertyValue<Picker>("elanBrand");
SqlDataSource sqlDataSource = JObject.Parse(test.GetDataTypePreValue("dataSource").Value).ToObject<SqlDataSource>();
<p>@test.PickedKeys.First()</p>
foreach (var item in sqlDataSource.GetEditorDataItems(0))
{
if (item.Key == test.PickedKeys.First())
{
<p>@item.Label</p>
break;
}
}
}
It might be good if there was a "custom label template" option when configuring the datatype - could cut out the whole macro configuration step then. (chances are, don't need much logic in a custom label)
It would be nice if there was an option to pass a parameter to my SQL statement. I have searched and searched for an answer there.
I need a SQL statement where I can search by a Value that is in the Parent Node. I figured out a workaround but it would be more ideal to pass the value directly to SQL.
nuPicker SQL Dropdown - How to get selected Label?
Hi Guys,
hope I'm asking in the right place.
I have a nuPicker.Picker control on a DocType, this is an SQL driven dropdown. Works fine in the backend.
I am iterating the doctypes with a fairly standard strongly typed loop. I can get the value selected with GetPropertyValue
in essence I have values such as this :
Key - Label 1 - Club 1 2 - Club Ex 3 - Club 3 4 - Club Ex
I'm using a unique integer as the key as unique labels can't be guarenteed. However to avoid multiple queries on the database I'd rather like to be able to fetch the Labels for the Picker too.
Can this be done at all? I read the documentation here : https://github.com/uComponents/nuPickers/wiki/Property-Value-Converter but the helper methods are only relivent in the context of a Media, Node or Member picker, not a DataSource picker...
Can anyone help? is the Picker even aware of it's labels (DataSource values) at the templating level or have I no choice but to query the DB to get them again?
Thanks in advance,
Danny "Blatant"
Hello,
There is a .GetLabel() extension method which you can use if you add the nuPickers.Shared.EnumDataSource namespace.
Here is an example with the enum checkbox list: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Site/Views/Content.cshtml#L122
Jeroen
Hi Jeroen,
I see what you're saying and I've checked out the EnumDataSource namespace. however I'm using the SQL Dropdown Picker not the Enum Pickers.
I assume that then I'd somehow use the SqlDataSource or SqlDataSourceApiController, but I am at a loss as to how to operate this. Can I give you a code example?
The above correctly prints out the chosen Value of the picker, but not the label. I inspected the Picker object but couldn't find anything.
In the SQL Picker, I have a simple query that pulls my value/label columns as so :
Value = Label
1 = Option 1
2 = Option 2
3 = Option 3
Do you have any ideas? perhaps a hint to help me, as I guess the jist of your answer is to use the DataSource to get the options and then iterate those to match the 'Picked Key'?
Solution, not sure if its the 'right' way or even a good way to do it, but I dug through the nuPicker Source and found some code hints. here's what I'm using, as Jerowen was getting at, we need to create the SqlDataSource and iterate it.
If you are using a Macro. Make sure to set your Macro Properties.
You can access them simply like this.
"Whoever produces the most simple answer wins".
It might be good if there was a "custom label template" option when configuring the datatype - could cut out the whole macro configuration step then. (chances are, don't need much logic in a custom label)
It would be nice if there was an option to pass a parameter to my SQL statement. I have searched and searched for an answer there.
I need a SQL statement where I can search by a Value that is in the Parent Node. I figured out a workaround but it would be more ideal to pass the value directly to SQL.
I suppose XML was an option but nahhhhh
is working on a reply...