按钮事件

在示例数据库的“ButtonEvent”表中,记录的各个字段(“caption”、“the color”、“Tooltip”、“badge.caption”、“badge.color”)通过下文介绍的 onUpdate 函数,设置“Bonjour”按钮的标题、颜色、工具提示和徽章。
使用 Ninext 自定义按钮
借助 Ninext,您可以通过在按钮的“Display field only, if”公式中定义一个名为 onUpdate 的函数,来修改 Ninox 按钮的外观。该函数必须返回一个对象,用于描述要修改的视觉元素,并且脚本必须以一个布尔值结束,用以指明按钮是否应当显示。
采用这种方法,您可以:
- 更改按钮的标题。
- 更新按钮的工具提示(或标题)。
- 从 Ninox 的三种按钮样式中选择按钮的颜色:
"blue"(蓝色背景)、"red"(粉色背景、红色文字)和"grey"(白色背景、蓝色边框,就像上面的“Bonjour”按钮)。 - 显示徽章以突出重要信息。
此功能可以在 Ninext 设置的“界面增强”部分中停用:按钮事件 (onUpdate)(默认启用)。
JSON 属性速览
| 属性 | 说明 | 示例 / 用法 |
|---|---|---|
| caption | 按钮上显示的文字。 | "Send" / "Cancel" |
| tooltip 或 title | 鼠标悬停时以工具提示形式显示的文字。 | "Click to send" |
| buttonColor | 按钮的颜色:只能是 "blue"、"red" 或 "grey"。其他任何值都会被忽略,按钮保持原来的颜色。 | "blue" |
| badge | 描述要在按钮上显示的徽章的对象。 | 详见下文 |
| badge.caption | 徽章上显示的文字、数字或符号。它始终是文本:请用 text() 转换数字。空文本会隐藏徽章。 | "3" 或 "!" |
| badge.color | 徽章的背景颜色(默认为红色)。徽章的文字始终为白色。 | "red" 或 "#4970ff" |
总体流程
- 定义 onUpdate 函数
在按钮的“Display field only, if”公式中,声明一个名为 onUpdate 的函数。该函数返回一个对象(JSON 格式),描述要修改的元素。它的参数是按钮在 Ninox 中定义的标题和颜色:{ caption, buttonColor }。 - 返回一个布尔值以决定可见性
公式必须以返回一个布尔值(true或false)结束,以指明按钮是否应当显示。 - Ninext 应用这些修改
当记录被打开或刷新时,Ninext 会执行 onUpdate 函数,读取返回的对象,并动态地将外观更改应用到按钮上。
实现
“Display field only, if”公式中的单个代码块
Ninox
"Declaration of the onUpdate function that customizes the button";
function onUpdate(buttonValues : any) do
let count := this.Count;
{
caption: if count > 0 then
"Send (" + count + ")"
else
"Send"
end,
tooltip: "Click to send your request",
buttonColor: if count > 0 then "red" else "blue" end,
badge: {
caption: if count > 0 then text(count) else "" end,
color: "red"
}
}
end;
"Determination of the button's visibility";
this.Count >= 0说明:
- onUpdate 函数构建一个对象,用于修改按钮标题(当
Count为正时附带数字指示)、工具提示、按钮颜色,并显示徽章。 - 最后一段代码返回一个布尔值,用于决定按钮是否应当显示。
最简示例(仅徽章)
Ninox
"Declaration of the onUpdate function to display only a badge";
function onUpdate(buttonValues : any) do
let newItems := this.newItems;
{
badge: {
caption: if newItems > 0 then text(newItems) else "" end,
color: "red"
}
}
end;
"The button is always displayed";
true说明:
- 仅返回 badge 属性,用于在
newItems大于 0 时显示其值。 - 按钮无条件显示。
要点
- 最后必须返回布尔值
如果末尾不返回true或false,按钮将不可见。 - 徽章标签始终是文本
只有当badge.caption是非空文本时,Ninext 才会显示徽章。数字(3而不是"3")会让徽章保持隐藏:请使用text()。 - 单独求值的函数
Ninext 只从公式中提取onUpdate函数,然后在记录的上下文中单独对它求值。因此,它看不到“Display field only, if”中其他地方声明的变量或其他函数:请在onUpdate内部声明它所需要的一切。它可以读取记录的字段(this.Count),也可以调用数据库的全局函数。
