Copied to clipboard

Flag this post as spam?

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


  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Dec 22, 2015 @ 23:04
    Nicholas Westby
    1

    Setting Default Selection in nuPickers?

    I just created a DotNetDataSource for boolean values (e.g., "true" and "false"). I created a radio button picker with that data source, which worked fine.

    However, I then wanted to default to "true" whenever a content editor comes across a new property of that data. I couldn't find a way to do that. It seems that the radio buttons are unset by default and there is no way (via code or configuration) to default them.

    Am I missing something? Is there a way to set a default option?

    I wouldn't think it should matter, but here's my dot net data source for your reference:

    public class ConfigurableBool : IDotNetDataSource
    {
        [DotNetDataSource(
            Title = "Default Value",
            Description = @"Enter the value that will be the default (i.e., ""True"" or ""False"").")]
        public string DefaultValue { get; set; }
    
        [DotNetDataSource(
            Title = "True Label",
            Description = @"The label to use for the ""True"" item.")]
        public string TrueLabel { get; set; }
    
        [DotNetDataSource(
            Title = "False Label",
            Description = @"The label to use for the ""False"" item.")]
        public string FalseLabel { get; set; }
    
        public IEnumerable<StringPair> GetEditorDataItems(int contextId)
        {
            var trueLabel = string.IsNullOrWhiteSpace(TrueLabel) ? "True" : TrueLabel;
            var falseLabel = string.IsNullOrWhiteSpace(FalseLabel) ? "False" : FalseLabel;
            var defaultValue = DefaultValue ?? string.Empty;
    
            var items = new StringPair[]
            {
                new StringPair(true.ToString(), trueLabel),
                new StringPair(false.ToString(), falseLabel)
            };
            return items
                .Select((Item, Index) => new
                {
                    Item,
                    Index
                })
                .OrderBy(x => x.Item.Key.InvariantEquals(DefaultValue) ? 0 : 1)
                .ThenBy(x => x.Index)
                .Select(x => x.Item).ToArray();
        }
    }
    
  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Jan 04, 2016 @ 09:58
    Hendy Racher
    0

    Hi Nicholas,

    Hadn't considered being able to specify default values, that'd be a good feature to add to the DotNetDataSource attribute: https://github.com/uComponents/nuPickers/issues/124

    Thanks, Hendy

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Jan 04, 2016 @ 18:15
    Nicholas Westby
    0

    Cool. As mentioned in the ticket, it would be more ideal to be able to dynamically specify the default value (rather than hardcoding it via an attribute).

Please Sign in or register to post replies

Write your reply to:

Draft