logo

S2

  • 使用文档
  • API 文档
  • 图表示例
  • 在线体验
  • 更新日志
  • 所有产品antv logo arrow
  • 2.x
  • 简介
  • 快速上手
  • 基础教程
    • 基本概念
      Updated
    • 表形态
      • 透视表
        Updated
      • 明细表
        Updated
    • 字段标记
      Updated
    • 小计总计
    • 排序
      • 基础排序
        Updated
      • 组内排序
        Updated
    • 主题配置
      Updated
    • Tooltip
      Updated
    • 数据格式化
      New
    • 多行文本
      New
    • 国际化
    • 分页
      New
  • 进阶教程
    • 单元格渲染类型
      • 链接
      • 图片
        New
      • 视频
        New
      • 迷你图表
      • 结合@antv/g2
      • 自定义渲染
    • 自定义
      • 自定义 Hook
        Updated
      • 自定义行列头分组
        New
      • 自定义 Icon
        Updated
      • 自定义单元格对齐方式
        Updated
      • 自定义单元格宽高
        Updated
      • 自定义排序操作
        Updated
      • 自定义折叠/展开节点
        New
    • 交互
      • 基础交互
        Updated
      • 自定义交互
      • 隐藏列头
        Updated
      • 行列宽高调整
        Updated
      • 合并单元格
      • 滚动
        Updated
      • 复制与导出
        New
      • 高亮/选中单元格
        New
    • 分析组件
      • 简介
        New
      • 高级排序
        Updated
      • 维度切换
        Updated
      • 导出
        Updated
      • 分页
        Updated
      • 维度下钻
        Updated
    • 表格组件
      • 编辑表
      • 趋势分析表
        Updated
    • 高清适配
      Updated
    • 获取表格实例
    • 表格自适应
    • 获取单元格数据
      Updated
    • 注册 AntV/G 插件
      New
    • 透视组合图
      Experimental
    • Vue 3.0 组件 (停止维护)
  • 扩展阅读
    • 数据流处理
      • 透视表
      • 明细表
    • 布局流程
      • 透视表
      • 明细表
    • 性能介绍
      Updated
  • 贡献指南
  • 常见问题
  • S2 2.0 升级指南

自定义交互

上一篇
基础交互
下一篇
隐藏列头

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

如果内置交互未能覆盖实际的使用场景,不用担心。你可以使用 S2Event 所提供的交互事件,进行任意排列组合,自定义交互。这里以 明细表双击隐藏列头 的示例说明。

1. 自定义交互类

这是一个自定义交互类的基本格式:

继承 BaseEvent 拿到当前表格实例 this.spreadsheet, 实现 bindEvents 方法,结合 this.spreadsheet 提供的 一系列方法,自定义交互,最后表格初始化时会注册默认交互,和自定义交互。

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

监听 列头 双击事件: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. 注册自定义交互

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

3. 多个自定义交互

当然,你可以注册任意数量的自定义交互,比如你不想让用户在表格右键时出现 浏览器默认的菜单

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