api.exec()
설명
내부 이벤트를 트리거할 수 있습니다
사용법
api.exec(
event: string,
config: object
): Promise<any>;
매개변수
event- (필수) 발생시킬 이벤트config- (필수) 매개변수가 포함된 config 객체 (발생시킬 이벤트 참조)
액션
정보
Pivot 이벤트의 전체 목록은 여기에서 확인할 수 있습니다
예제
아래 예제에서는 api.exec() 메서드를 통해 delete-field 이벤트가 트리거됩니다. 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);