按鈕事件

示範資料庫的 ButtonEvent 記錄:「Bonjour」按鈕帶有一個紅色的「2」徽章

在示範資料庫的「ButtonEvent」表格中,記錄裡的欄位(「caption」、「the color」、「Tooltip」、「badge.caption」、「badge.color」)透過下文所述的 onUpdate 函式,設定「Bonjour」按鈕的標題、顏色、提示文字與徽章。

以 Ninext 自訂按鈕

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

以這種方式,您可以:

  • 變更按鈕的標題文字。
  • 更新按鈕的提示文字(tooltip 或 title)。
  • 變更按鈕顏色,可從 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"

一般流程

  1. 定義 onUpdate 函式
    在按鈕的「Display field only, if」公式中,宣告一個名為 onUpdate 的函式。這個函式會回傳一個物件(JSON 格式),描述要修改的元素。它會收到按鈕在 Ninox 中所定義的標題與顏色作為參數:{ caption, buttonColor }。
  2. 回傳布林值以控制顯示與否
    公式必須以回傳一個布林值(true 或 false)作結,用以指出該按鈕是否應該顯示。
  3. 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),並呼叫資料庫的全域函式。