按钮事件

图片.png

使用 Ninext 自定义按钮

借助 Ninext,您可以通过在“仅在以下条件下显示”触发器中定义一个名为 onUpdate 的函数,来修改 Ninox 按钮的外观。该函数必须返回一个对象,用于描述要修改的视觉元素,并且脚本必须以一个布尔值结束,用以指明按钮是否应当显示。

采用这种方法,您可以:

  • 更改按钮的标题。
  • 更新按钮的工具提示(或标题)。
  • 使用预定义的类更改按钮的颜色。
  • 显示徽章以突出重要信息。

JSON 属性速览

属性说明示例 / 用法
caption按钮上显示的文字。"Send" / "Cancel"
tooltiptitle鼠标悬停时以工具提示形式显示的文字。"Click to send"
buttonColor应用于按钮的颜色类(例如 "blue"、"red"、"grey")。"blue"
badge描述要在按钮上显示的徽章的对象。详见下文
badge.caption徽章上显示的文字、数字或符号。"3" or "!"
badge.color徽章颜色(默认或自定义)。"red" or "#4970ff"

总体流程

  1. 定义 onUpdate 函数
    在按钮“仅在以下条件下显示”触发器的代码中,声明一个名为 onUpdate 的函数。该函数返回一个对象(JSON 格式),描述要修改的元素。
  2. 返回一个布尔值以决定可见性
    触发器必须以返回一个布尔值(truefalse)结束,以指明按钮是否应当显示。
  3. 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 时显示其值。
  • 按钮无条件显示。

要点

  • 最后必须返回布尔值
    如果末尾不返回 truefalse,按钮将不可见。