Appearance
Repeat
Repeat控件用于控制循环,可以使用Break控件提前结束循环。
业务属性
属性 | 含义 | 说明 | 输入限定 | 示例值 |
---|---|---|---|---|
enable | 是否启用该控件,未启用时不编译 | 是否编译 | 选择值 | 启用 |
text | 与控件关联的文本 | 设计页控件显示 | 常量 | repeat |
expression | 判断表达式 | Lua判断表达式 | Lua脚本 | var1>var2 |
判断表达式
完全遵循Lua语言的语法,其中不等于
、逻辑与
、逻辑或
和逻辑非
分别用~=
、and
、or
和not
表示,在判断表达式
中变量的表示无需使用#{}
占位符,直接书写变量名即可。
与While控件相反,Repeat控件是当表达式值为真
时结束
循环,而While控件是当表达式值为真
时进入
循环。
同时Repeat控件的执行顺序也与While控件相反,Repeat控件是先执行循环体再进行表达式判断,而While控件是先进行表达式判断再执行循环体。
lua
repeat
--code
until ...
repeat
--code
until ...
使用示例
先执行循环体
设置判断表达式为:true
循环体内的输出控件输出:循环体内
循环体外的输出控件输出:循环体外
如下图:

使用curl请求接口:
$ curl http://localhost:6636/api/repeat
$ curl http://localhost:6636/api/repeat
接口返回:
json
{
"resCode": "0",
"resMsg": "success",
"data": "循环体内"
}
{
"resCode": "0",
"resMsg": "success",
"data": "循环体内"
}
由结果可知,第一次进入循环体没有经过判断表达式的判断,即是先执行循环体再进行表达式判断。
循环体执行实例
使用LuaScript控件定义一个数组r1
,用于存放输出参数:
lua
local r1={}
local r1={}

将入参body
的属性currentValue
和endValue
分别当作循环变量和循环结束值,判断表达式输入:
body.currentValue>=body.endValue

使用TableInsert控件往r1
数组插入循环变量值:
body.currentValue

使用LuaScript控件对循环变量值自增 1:
lua
body.currentValue=body.currentValue+1
body.currentValue=body.currentValue+1

使用Return控件输出结果r1
:
json
{"r1":"#{r1}"}
{"r1":"#{r1}"}

使用curl请求接口:
$ curl http://localhost:6636/api/repeat -d "{\"currentValue\":0,\"endValue\":5}"
$ curl http://localhost:6636/api/repeat -d "{\"currentValue\":0,\"endValue\":5}"
接口返回:
json
{
"resCode": "0",
"resMsg": "success",
"data": {
"r1": [0,1,2,3,4]
}
}
{
"resCode": "0",
"resMsg": "success",
"data": {
"r1": [0,1,2,3,4]
}
}
当循环变量值自增到5时,条件表达式判断为真,循环结束。