logo

S2

  • Manual
  • API
  • Examples
  • Playground
  • ChangeLog
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quick Start
  • Basic Tutorial
    • Base Concept
    • Sheet Type
      • Pivot Mode
        Updated
      • Table Mode
    • Conditions
      Updated
    • Totals
    • Sort
      • Custom Sort
        Updated
      • Group Sort
        Updated
    • Theme
      Updated
    • Tooltip
      Updated
    • Internationalization
    • 数据格式化
      New
    • Multi Line
      New
    • Pagination
      New
  • Advanced Tutorial
    • Advanced Tutorial
      • Link
      • Render Chart In Cell
      • Draw Chart With @antv/g2
    • Custom
      • Customize Hook
        Updated
      • Customize Tree
        New
      • Customize Icon
      • Customize Cell Alignment
      • Customize Cell Size
        Updated
      • Customize Order
      • Custom Collapse/Expand Nodes
    • Interaction
      • Basic Interaction
      • Custom Interaction
      • Hide Columns
      • Cell Resize
      • Merge Cell
      • Scroll
      • Copy
        New
      • Highlight and select cell
        New
    • Analyze Component
      • Introduction
        New
      • Advanced Sort
        Updated
      • Switcher
      • Export
        Updated
      • Pagination
      • Drill Down
    • Sheet Component
      • Editable Mode
      • Strategy Sheet
        Updated
    • HD Adapter
    • Get Instance
    • Get Cell Data
      Updated
    • Table adaptive
    • AntV/G Plugins
      New
    • Pivot Chart
      Experimental
    • Vue 3.0
  • Extended Reading
    • Data Process
      • Pivot
      • Table
    • Layout
      • Pivot
      • Table
    • Performance
      Updated
  • Contribution
  • FAQ
  • S2 2.0 Migration Guide

Custom Interaction

Previous
Basic Interaction
Next
Hide Columns

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Don't worry if the built-in interactions fail to cover actual usage scenarios. You can use the interaction events provided by S2Event to perform any permutation and combination to customize the interaction. Here is an example of double-clicking to hide the column header in the schedule .

1. Custom interaction class

This is the basic format of a custom interaction class:

Inherit BaseEvent to get the current table instance this.spreadsheet , implement the bindEvents method, combine a series of methods provided by this.spreadsheet , and customize the interaction. Finally, the default interaction and custom interaction will be registered when the table is initialized.

import { BaseEvent } from '@antv/s2';
// 继承 BaseEvent, 可以拿到 this.spreadsheet
class HiddenInteraction extends BaseEvent {
bindEvents() {
}
}

Listen列头double-click event: S2Event.COL_CELL_DOUBLE_CLICK

import { BaseEvent, S2Event } from '@antv/s2';
class HiddenInteraction extends BaseEvent {
bindEvents() {
// 列头双击时
this.spreadsheet.on(S2Event.COL_CELL_DOUBLE_CLICK, (event) => {
// 获取当前单元格
const cell = this.spreadsheet.getCell(event.target);
// 获取当前单元格元数据
const meta = cell.getMeta();
// 隐藏当前列
this.spreadsheet.interaction.hideColumns([meta.field]);
});
}
}

2. Register custom interaction

import { TableSheet } from '@antv/s2';
const s2Options = {
width: 600,
height: 300,
interaction: {
customInteractions: [
{
// 交互的唯一标识,需要保证和已有交互不冲突
key: 'MyInteraction',
interaction: MyInteraction,
},
],
}
};
const s2 = new TableSheet(container, s2DataConfig, s2Options);
s2.render();

3. Multiple custom interactions

Of course, you can register any number of custom interactions, for example, you don't want the browser's default menu to appear when the user right-clicks the table

import { TableSheet } from '@antv/s2';
class ContextMenuInteraction extends BaseEvent {
bindEvents() {
this.spreadsheet.on(S2Event.GLOBAL_CONTEXT_MENU, (event) => {
// 禁止弹出右键菜单
event.preventDefault();
});
}
}
const s2Options = {
width: 600,
height: 300,
interaction: {
customInteractions: [
{
key: 'MyInteraction',
interaction: MyInteraction,
},
{
key: 'ContextMenuInteraction',
interaction: ContextMenuInteraction,
},
],
}
};
const s2 = new TableSheet(container, s2DataConfig, s2Options);
s2.render();