api.exec()
描述
允许触发内部事件
用法
api.exec(
event: string,
config: object
): Promise<any>;
参数
event- (必填)要触发的事件config- (必填)包含参数的配置对象(参见要触发的事件)
操作
信息
Pivot 事件的完整列表可在此处查看
示例
在下面的示例中,delete-field 事件通过 api.exec() 方法触发。最后一个字段将从 values 区域中移除。api.getState() 方法用于获取 Pivot config 的当前状态。点击按钮后将触发该事件。
// 创建 Pivot
const table = new pivot.Pivot("#root", {
fields,
data: dataset,
config: {
rows: ["studio", "genre"],
columns: [],
values: [
{
field: "title",
method: "count"
},
{
field: "score",
method: "max"
}
]
}
});
// 调用 API 方法:从 config 的 values 中移除指定值
function removeLastField() {
if (table.api) {
const state = table.api.getState();
const config = state.config;
const count = config.values.length;
if (count) {
const lastValue = config.values[count - 1];
table.api.exec("delete-field", {
area: "values",
id: lastValue.id, // 自动生成的 config.values 中已添加项目的 ID
});
}
}
}
const button = document.createElement("button");
button.addEventListener("click", removeLastField);
button.textContent = "Remove";
document.body.appendChild(button);