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?
(.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
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:
Add a new method to accept the enum:
Right after where
var rows = q.Exec().FetchAll();
lives, add this loopI then made a new enum for Sites:
and plugged this into the SearchPanes sample controller:
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!
Interesting one! Thanks for raising this. I hadn't really thought about using
SearchPanesOptions
as anything other than a source from a database. Perhapsnew SearchPaneOptions( enum )
should be allowed, ornew SearchPaneOptions( List<string, string> )
.I like it - yes, I'd take a PR which added that.
Allan
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
Amazing work! A better PR than I've seen from many seasoned pros. Thank you for taking the time to do that!
Allan
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
andOptions
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: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:
For Options you can chain the
.AddFromEnum
with.Add
and also a database source. Duplicates will be removed:For
SearchBuilderOptions
andSearchPaneOptions
, 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.With the next release, which I expect to be 2.3.0 near the end of the month.
Allan