Skip to content

如何在cell内自定义多个按钮,并添加点击事件 #1473

Closed Answered by lijinke666
Carnia asked this question in Q&A
Discussion options

You must be logged in to vote

S2 目前主要定位是看数, 对于传统 DOM table 最后一列放置编辑按钮 这种场景不是很适合, 推荐你用 antd

如果用 S2 实现类似的效果, 由于本身基于 Canvas, 只能基于坐标计算, 会比较麻烦, 以在 DataCell 中添加文字为例, 提供一种方案:

  1. 将文字改成居左或居中 (默认是居右)
  theme: {
    dataCell: {
      text: {
        textAlign: 'center',
      },
    },
  }
  1. 自定义你想添加文字对应的单元格, 比如数值, 列头等
  2. 重写 drawTextShape, 分别绘制文字, 计算坐标, 绑定事件
  3. 如果你想绘制 [按钮], 而不是 [文字], 那么只能绘制一个图片
class TestDataCell extends DataCell {
  drawTextShape() {
    const textStyle = this.getTextStyle();
    const position = this.getTextPosition();
    const textA = 'A';
    const textB = 'B';

    const margin = 5;

    const textAWidth = measureTextWidth(textA, textStyle);
    const textBWidth = measureTextWidth(textB, textStyle);

    this.actu…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Carnia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants