如何在Business Central 365中迭代枚举标题

如何解决如何在Business Central 365中迭代枚举标题

枚举是/将成为Business Central 365的选件替代品。最近,我有一个机会可以使用它,并且可以弄湿我的脚。似乎经常发生这种情况,您所需的功能中约有80%随时可用,而其余的20%则需要的工作量更多。

对于枚举,您将获得名称的文本列表和关联的序数值的整数列表,但不会获得标题列表。在下面的部分示例Enum FourStates中,Default,OH和TX是名称,0、1和2是Ordinals和空格,Ohio和Texas是标题。请注意,序数是定义的数值,而不是索引。在下面的示例中,完全有效的序数可以是1、5和7。

value(0; Default) { Caption = ' '; }
value(1; OH) { Caption = 'Ohio'; }
value(2; TX) { Caption = 'Texas'; }

如果将“表”或“页面”字段定义为枚举,则标题将显示在下拉列表中。要获取标题,可以使用Format(EnumType :: Name),但我们需要迭代给定枚举的所有标题。

解决方法

在浏览了一些博客和文档之后,下面是我发现的内容的摘要。

  1. 首先,存在一个主要限制,因为字幕只能在Enum类型的上下文中处理,并且至少到目前为止,Business Central 365 SaaS解决方案不支持通用Enum(例如,类似EnumRef RecordRef和FieldRef)。这意味着必须在逐个枚举的基础上创建字幕的文本列表。

  2. 但是,一旦创建了标题列表,然后使用[文本]的名称,顺序和标题列表的组合,您就可以编写适用于任何枚举的通用代码。

  3. 一个难题,当您在VS Code中使用TEnum代码片段时,它会将第一个元素的默认值设置为0。我们已经学习了将Default设置为上面的部分代码,或者将其更改为1。这样做的原因是因为如果有可能每个人都可以通过StrMenu命令使用一个枚举,则StrMenu默认为0作为Cancel返回值。在这种情况下,您永远不能选择0序数,因为ti将被视为已取消。

  4. 要创建在代码中使用的Enum的实例,请创建一个类似于MyChoices的Var变量:Enum“ Defined Choices”;。如果您还定义了MyChoice:Enum:“ Defined Choices”,则可以将它们与类似MyChoice = MyChoices :: FirstChoice then等的内容进行比较。

  5. 下面是一些具有一些不同Enum样式的示例代码,以及允许您为字幕创建[文本]列表的方法。同样,必须为每个特定的Enum进行编码。 一种。在VS Code中,使用AL Go创建一个新的HelloWorld应用 b。更改app.json文件的名称和发布者,我将其命名为EnumHelper 注意:我们总是定义一个发布者,因为您不能在SaaS中筛选默认发布者 C。用下面的代码替换HelloWorld.al文件中的所有代码 注意:为简化起见,下面的所有内容都在同一文件中 d。该代码是“会计科目表”页面的PageExtension,并在OnOpenPage触发器上运行 这样一来,无需大量设置代码即可轻松调用该代码。

  6. 这是允许您创建字幕列表的代码。变量myOrdinal是一个整数,而Flagged是一个已定义的Enum。请注意Enum :: EnumName,类似于Page :: PageName或Database :: TableName。

     foreach myOrdinal in Flagged.Ordinals do begin
         // Enum definition,NOT an enum instance.
         captions.Add(Format(Enum::Flagged.FromInteger(myOrdinal)));
     end;
    
  7. 所有代码(抱歉,格式不正确)

枚举50200 FourStates { 可扩展=真;

value(0; Default) { Caption = ' '; }
value(1; OH) { Caption = 'Ohio'; }
value(2; TX) { Caption = 'Texas'; }
value(3; NC) { Caption = 'North Carolina'; }
value(4; IA) { Caption = 'Iowa'; }
value(5; MO) { Caption = 'Missouri'; }

}

枚举50201已标记 { 可扩展=真;

value(0; Default) { Caption = ' '; }
value(1; Bold) { Caption = 'Bold'; }
value(2; ITalic) { Caption = 'Italid '; }
value(4; Underline) { Caption = 'Underline'; } 
value(8; BoldItalic) { Caption = 'Bold & Italic'; }
value(16; BoldUnderline) { Caption = 'Bold & Underline '; }
value(32; ItalicUnderline) { Caption = 'Italic & Underline'; }
value(64; All3Options) { Caption = 'All 3 Options'; }

}

枚举50202随机 { 可扩展=真;

value(0; Default) { Caption = ' '; }
value(7; Good) { Caption = 'The Good'; }
value(5; Bad) { Caption = 'The Bad'; }
value(11; Ugly) { Caption = 'The Ugly'; }

}

枚举50203 ProcessFlowOptions { 可扩展=真;

value(0; Default) { Caption = ' '; }
value(1; Flagged) { Caption = 'Flagged'; }
value(2; Randomized) { Caption = 'Randomized'; }
value(4; FourStates) { Caption = 'FourStates'; }

}

pageextension 50200“帐户图表EH”扩展了“帐户图表” { 变种 //枚举实例变量。 myFlagged:枚举已标记; myFourStates:枚举FourStates; myRandomized:随机枚举;

trigger OnOpenPage();
begin
    case UserID.ToLower() of
        'larry':
            Message('Hello Larry,this is an extension for October testing.');
        'vicki':
            Message('Good morning Vicki,this is an extension for October testing.');
        else
            if Confirm('Hello %1 from EnumHelper.\\Click Yes to process or no to cancel.',true,UserID) then begin
                ProcessEnumerations();
            end;
    end;
end;

local procedure ProcessEnumerations()
var
    allLines: TextBuilder;
    randomCaptions: List of [Text];
    flaggedCaptions: List of [Text];
    fourStatesCaptions: List of [Text];
begin
    GetEnumCaptions(randomCaptions,flaggedCaptions,fourStatesCaptions);
    IterateEnumNamesOrdinalsAndCaptions(allLines,randomCaptions,fourStatesCaptions);
    Message(allLines.ToText());
end;

local procedure GetEnumCaptions(randomCaptions: List of [Text]; flaggedCaptions: List of [Text]; fourStatesCaptions: List of [Text])
begin
    GetCaptions(randomCaptions,ProcessFlowOptions::Randomized);
    GetCaptions(flaggedCaptions,ProcessFlowOptions::Flagged);
    GetCaptions(fourStatesCaptions,ProcessFlowOptions::FourStates);
end;

local procedure IterateEnumNamesOrdinalsAndCaptions(allLines: TextBuilder; randomCaptions: List of [Text]; flaggedCaptions: List of [Text]; fourStatesCaptions: List of [Text])
begin
    IterateEnumNamesOrdinalsAndCaptions('Flagged Enum',allLines,myFlagged.Names,myFlagged.Ordinals,flaggedCaptions);
    IterateEnumNamesOrdinalsAndCaptions('Randomized Enum',myRandomized.Names,myRandomized.Ordinals,randomCaptions);
    IterateEnumNamesOrdinalsAndCaptions('FourStates Enum',myFourStates.Names,myFourStates.Ordinals,fourStatesCaptions);
end;

local procedure IterateEnumNamesOrdinalsAndCaptions(title: Text; allLines: TextBuilder; enumNames: List of [Text]; enumOrdinals: List of [Integer]; enumCaptions: List of [Text])
var
    i: Integer;
    enumLine: TextBuilder;
    enumLines: TextBuilder;
begin
    allLines.AppendLine(title);
    allLines.appendLine();
    for i := 1 to enumNames.Count do begin
        Clear(enumLine);
        enumLine.AppendLine('EnumName: ''' + enumNames.Get(i) + ''',');
        enumLine.AppendLine('EnumOrdinal: ' + Format(enumOrdinals.Get(i)) + ',');
        enumLine.AppendLine('EnumCaption: ''' + enumCaptions.Get(i) + '''.');
        //enumLine.AppendLine('EnumName: ''' + enumNames.Get(i) + ''',EnumOrdinal: ' + ordinal + ',EnumCaption: ''' + enumCaptions.Get(i) + '''');
        enumLines.AppendLine(enumLine.ToText());
    end;
    allLines.AppendLine(enumLines.ToText());
end;

local procedure GetCaptions(captions: List of [Text]; processFlowOption: Enum ProcessFlowOptions)
var
    myOrdinal: Integer;
    myProcessFlowOptions: Enum ProcessFlowOptions;
begin
    // Load captions by iterating specific Enums.
    case processFlowOption of
        myProcessFlowOptions::Flagged:
            begin
                foreach myOrdinal in Flagged.Ordinals do begin
                    // Enum definition,NOT an enum instance.
                    captions.Add(Format(Enum::Flagged.FromInteger(myOrdinal)));
                end;
            end;
        myProcessFlowOptions::Randomized:
            begin
                foreach myOrdinal in Randomized.Ordinals do begin
                    // Enum definition,NOT an enum instance.
                    captions.Add(Format(Enum::Randomized.FromInteger(myOrdinal)));
                end;
            end;
        myProcessFlowOptions::FourStates:
            begin
                foreach myOrdinal in FourStates.Ordinals do begin
                    // Enum definition,NOT an enum instance.
                    captions.Add(Format(Enum::FourStates.FromInteger(myOrdinal)));
                end;
            end;
    end;
end;

}

享受

,

我为BC14实施了更好的解决方法。这也应该适用于较新的版本,但是我仅在BC14上进行了测试。

var
  RecRef: RecordRef;
  FRef: FieldRef;
  OptionNames: List of [Text];
  OptionCaptions: List of [Text];
  i: Integer;

RecRef.GetTable(Rec); // Some record
FRef := RecRef.Field(20); // Some field of type Enum
OptionNames := FRef.OptionMembers().Split(',');
OptionCaptions := FRef.OptionCaption().Split(',');
for i := 1 to OptionNames.Count() do begin
  Evaluate(FRef,OptionNames.Get(i));
  Message('Ordinal = %1\Name = %2\Caption = %3',format(FRef,9),OptionNames.Get(i),1)); // or OptionCaptions.Get(i)
end;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-