.Net - Add blank row option to Field.Options()

.Net - Add blank row option to Field.Options()

jaydonoghuejaydonoghue Posts: 3Questions: 1Answers: 0

Hi, love the new (easier) way to build up an option list from a database table in .Net land

Would it be possible to add a new option to add a blank row to the list of options? this is handy for dropdown lists. I've tweaked a local version to get this working if there is any interest.

FIeld.cs
private parameters

private bool _optsAddBlank;

Options methods

        public Field Options(string table, string value, string label, Action<Query> condition = null, Func<Dictionary<string, object>, string> format = null, bool addblank = false)
        {
            _optsFn = null;
            _optsTable = table;
            _optsValue = value;
            _optsLabel.Add(label);
            _optsCond = condition;
            _optsFormat = format;
            _optsAddBlank = addblank;

            return this;
        }

        public Field Options(string table, string value, IEnumerable<string> label, Action<Query> condition = null, Func<Dictionary<string, object>, string> format = null, bool addblank = false)
        {
            _optsFn = null;
            _optsTable = table;
            _optsValue = value;
            foreach (var l in label)
            {
                _optsLabel.Add(l);
            }
            _optsCond = condition;
            _optsFormat = format;
            _optsAddBlank = addblank;
            return this;
        }
internal List<Dictionary<string, object>> OptionsExec(Database db)
        {
            if (_optsFn != null)
            {
                return _optsFn();
            }

            if (_optsTable == null)
            {
                return null;
            }

            var formatter = _optsFormat ??
              (row => string.Join(" ", _optsLabel.Select(val => row[val] as string).ToArray()));

            var fields = new List<string>(_optsLabel) { _optsValue };
            var rows = db.SelectDistinct(_optsTable, fields, _optsCond)
                .FetchAll();

            var allrows = rows.Select(row => new Dictionary<string, object>
            {
                {"value", row[_optsValue]},
                {"label", formatter(row)}
            }).OrderBy(x => x["label"]).ToList();

            if (_optsAddBlank)
            {
                allrows.Insert(0,new Dictionary<string,object>() 
                {
                    {"value",""},
                    {"label","" }
                });
            }

            return allrows;
        }

Replies

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Thanks for the request - I can certainly see that it would be a useful feature. My one concern about adding it in is that it would also require a validator that will reject that empty value in case the user submits it.

    However, this is something I will look into adding in future.

    Allan

  • jaydonoghuejaydonoghue Posts: 3Questions: 1Answers: 0

    Thanks for considering. For my scenario, I want the blank value to be passed in (its an optional field) and use NULL into the database.

    Using .SetFormatter(Format.NullEmpty()) resolves that issue.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    An update on this for anyone who finds this discussion though the search options - Editor 1.5.4 introduced a placeholder option to the -field select type that makes this really easy on the client-side.

    Allan

This discussion has been closed.