for DataTables-Editor-Server, is it possible to use an Enum with SearchPaneOptions?

for DataTables-Editor-Server, is it possible to use an Enum with SearchPaneOptions?

parcivalparcival Posts: 28Questions: 8Answers: 0

(.NET) My application stores enum values in a table and I'd like to use either SearchPaneOptions or SearchBuilderOptions to allow my users to build filters on that column, but I don't see a way in the documentation for this. Is this possible currently?
For example, I have this enum

public enum DeviceStatus{
NotChecked = 0,
Failed = 1,
Partial = 2,
Compliant = 3
}

The corresponding table column stores only the int, and so I'd like to offer the users the filter options by the name which is then searched using the number.

Thanks!

This question has an accepted answers - jump to answer

Answers

  • parcivalparcival Posts: 28Questions: 8Answers: 0

    I poked around in the Editor-Server repo, and got something basic working by modifying SearchPaneOptions.cs:
    Add a private variable to store a new dictionary:

    private Dictionary<string, string> _fromEnum = new Dictionary<string, string>();
    

    Add a new method to accept the enum:

    public SearchPaneOptions FromEnum<T, TValue>()
    {
        _fromEnum = Enum.GetValues(typeof(T))
            .Cast<T>()
            .ToDictionary( e => Convert.ChangeType(e, typeof(TValue)).ToString(),e => e.ToString());
        return this;
    }
    

    Right after where var rows = q.Exec().FetchAll(); lives, add this loop

    if (_fromEnum.Count > 0)
    {
        foreach (var row in rows)
        {
            row["label"] = _fromEnum[row["label"].ToString()] ;
        }
    }
    

    I then made a new enum for Sites:

    public enum SitesEnum
    {
        Edinburgh = 1,
        London = 2,
        Paris = 3,
        NewYork = 4,
        Singapore = 5,
        LosAngeles = 6
    }
    

    and plugged this into the SearchPanes sample controller:

    .Field(new Field("users.site")
        .SearchPaneOptions(new SearchPaneOptions().FromEnum<SitesEnum, int>())
    )
    

    Clearly this is some rough code, but it does work here. Would it be worthwhile to improve on this (especially converting the enum to a dictionary<string, string>) and submitting that as a pull request?

    Thanks!

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Interesting one! Thanks for raising this. I hadn't really thought about using SearchPanesOptions as anything other than a source from a database. Perhaps new SearchPaneOptions( enum ) should be allowed, or new SearchPaneOptions( List<string, string> ).

    I like it - yes, I'd take a PR which added that.

    Allan

  • parcivalparcival Posts: 28Questions: 8Answers: 0

    Hi Allan! I probably spent too much time on this, but I just submitted a PR. I ended up adding the same functionality to SearchBuilder as well as options. For options, I also did a "remove duplicates for manual entries. Old behavior was to present duplicate items to the user.

    Let me know if there's anything I should add. This is my first real PR, so please be gentle :smile:

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Amazing work! A better PR than I've seen from many seasoned pros. Thank you for taking the time to do that!

    Allan

  • parcivalparcival Posts: 28Questions: 8Answers: 0

    Happy to contribute! When would this show up in the Nuget package? Just so I know when I can discontinue my own compiled version.

    For others who may be looking for this, here's a quick example on usage:

    SearchBuilderOptions, SearchPaneOptions and Options have a .FromEnum<T>() method. This allows you to translate application enum values that are in the database from the number to the name and present the name to the user. This will create an option list like this:

    // From enum
    // Note: In order to include a space in the name, 
    //       you can use the Description attribute
    public enum SitesEnum
    {
        Edinburgh = 1,
        [Description("Los Angeles")]
        LosAngeles = 2,
        London = 3,
    }
    
    // In Editor-NET configuration
    .Field(new Field("users.site")
        .Options(new Options()
            .AddFromEnum<SitesEnum>()
        )
    )
    
    // The options response to the browser:
    // note: The labels are sorted alphabetically.
    // value: 1, label: "Edinburgh"
    // value: 3, label: "London"
    // value: 2, label: "Los Angeles"
    

    If your list of options from the enum is stored in the database as text, you can set a parameter to use the name of the enum to store in the database:

    public enum Salutations
    {
        Sir,
        [Description("Ma'am")]
        Madam
    }
    
    // Changing the parameter to false from the default 
    .Field(new Field("users.title")
        .Options(new Options()
                .AddFromEnum<Salutations>(false)
        )
    )
    
    // The options response to the browser:
    // note: The name itself is used, and not the description
    // value: "Madam", label: "Ma'am"
    // value: "Sir", label: "Sir"
    

    For Options you can chain the .AddFromEnum with .Add and also a database source. Duplicates will be removed:

    .Options(new Options()
        .AddFromEnum<SitesEnum>()
        .Add("Unknown", 0)
        .Table("sites")
        .Value("id")
        .Label("name")
    )
    

    For SearchBuilderOptions and SearchPaneOptions, only items in the database are returned to the browser. If entries in the database do not have a corresponding entry in the enum, the database value is used instead.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    When would this show up in the Nuget package?

    With the next release, which I expect to be 2.3.0 near the end of the month.

    Allan

Sign In or Register to comment.