共用方式為


ToggleSwitch 格式化配量

ToggleSwitch 是一個簡單的格式片段,用來表示在 capabilities.json 檔案中,的 bool 物件類型。

切換開關模塊的螢幕快照。

範例:ToggleSwitch 實作

在此範例中,我們會示範如何使用格式模型工具來建立 ToggleSwitch 片段。

Capabilities 物件

將下列 JSON 片段插入 capabilities.json 檔案中。

{
  // ... same level as dataRoles and dataViewMappings
  "objects": {
    "labels": {
      "properties": {
        "show": {
          "type": {
            "bool": true
          }
        }
      }
    }
  }
}

格式化模型類別

將下列代碼段插入配置檔。

import { formattingSettings } from "powerbi-visuals-utils-formattingmodel";

class LabelsCardSetting extends formattingSettings.SimpleCard {
    name: string = "labels"; // same as capabilities object name
    displayName: string = "Labels";

    public showLabels: formattingSettings.ToggleSwitch = new formattingSettings.ToggleSwitch({
        name: "show", // same as capabilities property name
        displayName: "Show",
        value: true
    });
    
    public slices: formattingSettings.Slice[] = [ this.showLabels ];
}

export class VisualSettings extends formattingSettings.Model {
    public labels: LabelsCardSetting = new LabelsCardSetting();
    public cards: formattingSettings.SimpleCard[] = [this.labels];
}

其他選項

ToggleSwitch 切片也可用作頂層卡片的切換。

頂層 ToggleSwitch 區塊的螢幕快照。

若要將 ToggleSwitch 設為最上層,請從 切片中移除 切片,並將以下這一行新增至卡片設定類別:

topLevelSlice: formattingSettings.ToggleSwitch = this.showLabels;

您的最終格式化設定檔應該符合此範例:

import { formattingSettings } from "powerbi-visuals-utils-formattingmodel";

class LabelsCardSetting extends formattingSettings.SimpleCard {
    name: string = "labels"; // same as capabilities object name
    displayName: string = "Labels";

    public showLabels: formattingSettings.ToggleSwitch = new formattingSettings.ToggleSwitch({
        name: "show", // same as capabilities property name
        displayName: "Show",
        value: true
    });
    
    topLevelSlice: formattingSettings.ToggleSwitch = this.showLabels;
    public slices: formattingSettings.Slice[] = [];
}

export class VisualSettings extends formattingSettings.Model {
    public labels: LabelsCardSetting = new LabelsCardSetting();
    public cards: formattingSettings.SimpleCard[] = [this.labels];
}