如果属性的类型是枚举,那么在设计器中会以下拉列表的形式提供给用户编辑。
public class MyPluginCellType : CellType { public UserType MyProperty1 { get; set; } } public enum UserType { Student, Teacher, Worker }
在设计器中效果如下:
需要注意的是,在C#代码中的枚举生成到JavaScript 中实际上是一个整数,默认情况下整数的值就是枚举出现的顺序。
所以在JavaScript代码中需要如下处理:
class MyPluginCellType extends Forguncy.Plugin.CellTypeBase { createContent() { const propValue = this.CellElement.CellType.MyProperty; let text = ""; switch (propValue) { case 0: text = "学生" break; case 1: text = "教师" break; case 2: text = "工人" break; default: } return $("<div>" + text + "<div>"); } } Forguncy.Plugin.CellTypeHelper.registerCellType("MyPlugin.MyPluginCellType, MyPlugin", MyPluginCellType);
为了提升代码的可读性性,可以替换为一下等价代码处理枚举类型属性:
class MyPluginCellType extends Forguncy.Plugin.CellTypeBase { userType = { 0: "学生", 1: "教师", 2: "工人", } createContent() { const propValue = this.CellElement.CellType.MyProperty; const text = this.userType[propValue]; return $("<div>" + text + "<div>"); } } Forguncy.Plugin.CellTypeHelper.registerCellType("MyPlugin.MyPluginCellType, MyPlugin", MyPluginCellType);
自定义枚举项目显示名称, 可以通过标注 Description 实现。
public class MyPluginCellType : CellType { public UserType MyProperty1 { get; set; } } public enum UserType { [Description("学生")] Student, [Description("教师")] Teacher, [Description("工人")] Worker }
效果: