按钮事件

使用 Ninext 自定义按钮
借助 Ninext,您可以通过在“仅在以下条件下显示”触发器中定义一个名为 onUpdate 的函数,来修改 Ninox 按钮的外观。该函数必须返回一个对象,用于描述要修改的视觉元素,并且脚本必须以一个布尔值结束,用以指明按钮是否应当显示。
采用这种方法,您可以:
- 更改按钮的标题。
- 更新按钮的工具提示(或标题)。
- 使用预定义的类更改按钮的颜色。
- 显示徽章以突出重要信息。
JSON 属性速览
| 属性 | 说明 | 示例 / 用法 |
|---|---|---|
| caption | 按钮上显示的文字。 | "Send" / "Cancel" |
| tooltip 或 title | 鼠标悬停时以工具提示形式显示的文字。 | "Click to send" |
| buttonColor | 应用于按钮的颜色类(例如 "blue"、"red"、"grey")。 | "blue" |
| badge | 描述要在按钮上显示的徽章的对象。 | 详见下文 |
| badge.caption | 徽章上显示的文字、数字或符号。 | "3" or "!" |
| badge.color | 徽章颜色(默认或自定义)。 | "red" or "#4970ff" |
总体流程
- 定义 onUpdate 函数
在按钮“仅在以下条件下显示”触发器的代码中,声明一个名为 onUpdate 的函数。该函数返回一个对象(JSON 格式),描述要修改的元素。 - 返回一个布尔值以决定可见性
触发器必须以返回一个布尔值(true或false)结束,以指明按钮是否应当显示。 - Ninext 应用这些修改
当记录被打开或刷新时,Ninext 会执行 onUpdate 函数,读取返回的对象,并动态地将外观更改应用到按钮上。
实现
“仅在以下条件下显示”触发器中的单个代码块
"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为正时附带数字指示)、工具提示、按钮颜色,并显示徽章。 - 最后一段代码返回一个布尔值,用于决定按钮是否应当显示。
最简示例(仅徽章)
"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,按钮将不可见。
