Copied to clipboard

Flag this post as spam?

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


  • Danny Blatant 91 posts 358 karma points
    Oct 07, 2014 @ 17:57
    Danny Blatant
    0

    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"

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Oct 08, 2014 @ 15:32
    Jeroen Breuer
    0

    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

  • Danny Blatant 91 posts 358 karma points
    Oct 08, 2014 @ 16:33
    Danny Blatant
    0

    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?

    @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'?

  • Danny Blatant 91 posts 358 karma points
    Oct 08, 2014 @ 17:54
    Danny Blatant
    100

    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;
              }
          }
      }
    
  • Christophe Vaught 21 posts 113 karma points
    May 15, 2018 @ 19:01
    Christophe Vaught
    1

    If you are using a Macro. Make sure to set your Macro Properties.

    You can access them simply like this.

    var keyValue = Model.MacroParameters["key"].ToString();
    var LabelValue = Model.MacroParameters["label"].ToString();
    

    "Whoever produces the most simple answer wins".

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    May 15, 2018 @ 22:09
    Hendy Racher
    0

    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)

  • Christophe Vaught 21 posts 113 karma points
    May 16, 2018 @ 14:22
    Christophe Vaught
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft