按鈕事件

image.png

以 Ninext 自訂按鈕

有了 Ninext,您可以在「Display only if」觸發器中定義一個名為 onUpdate 的函式,藉此變更 Ninox 按鈕的外觀。這個函式必須回傳一個物件,描述要修改的視覺元素;而整段腳本必須以一個布林值作結,用來指出該按鈕是否應該顯示。

以這種方式,您可以:

  • 變更按鈕的標題文字。
  • 更新按鈕的提示文字(tooltip 或 title)。
  • 使用預先定義的類別變更按鈕顏色。
  • 顯示徽章以凸顯重要資訊。

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 函式
    在按鈕「Display only if」觸發器的程式碼中,宣告一個名為 onUpdate 的函式。這個函式會回傳一個物件(JSON 格式),描述要修改的元素。
  2. 回傳布林值以控制顯示與否
    觸發器必須以回傳一個布林值(truefalse)作結,用以指出該按鈕是否應該顯示。
  3. Ninext 套用這些修改
    當記錄被開啟或重新整理時,Ninext 會執行 onUpdate 函式,讀取回傳的物件,並動態地將外觀變更套用到按鈕上。

實作

在「Display only if」觸發器中的單一程式碼區塊

"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,該按鈕將不會顯示。