19

我正在使用 ant 设计表组件。我有“操作”列,我不希望 onRowClick 事件在此列中触发。

如何做呢?

http://codepen.io/liron_e/pen/zZjVKZ?editors=001

const { Table, Modal } = antd;

const confirm = (id) => {
  Modal.confirm({
    title: 'Confirm',
    content: 'Bla bla ...',
    okText: 'OK',
    cancelText: 'Cancel',
  });
};

const info = (id) => {
  Modal.info({
    title: 'Info',
    content: 'Bla bla ...',
    okText: 'OK',
    cancelText: 'Cancel',
  });
};

const columns = [
  {
    key: 'status',
    title: 'text',
    dataIndex: 'text'
  }, {
    key: 'actions',
    title: 'actions',
    dataIndex: 'id',
    render: (id) => {
      return (
        <span>
          <a href="#" onClick={() => confirm(id)}>
            Clone
          </a>
          <span className="ant-divider" />
          <a href="#" onClick={() => confirm(id)}>
            Replace
          </a>
        </span>
      );
    }
  }
];

 const dataSource = [
   {
     id: '1',
     text: 'Hello'
   },{
     id: '123',
     text: 'adsaddas'
   },{
     id: '123344',
     text: 'cvbbcvb'
   },{
     id: '5665',
     text: 'aasddasd'
   },
 ];


ReactDOM.render(
  <div>
    <Table 
      columns={columns}
      onRowClick={() => this.info()}
      dataSource={dataSource}
    />
  </div>
, mountNode);

正如您在按下行时可以尝试的那样,信息模式将打开。当按下某些操作时,信息确认模式会打开,我希望只有确认模式会打开

谢谢 (:

4

4 回答 4

22

在您的渲染功能中:

render: (id) => {
  return (
    <span>
      <a href="#" onClick={(e) => { 
           e.stopPropagation();      
           confirm(id);
          }}>
        Clone
      </a>
      <span className="ant-divider" />
      <a href="#" onClick={() => confirm(id)}>
        Replace
      </a>
    </span>
  );
}
于 2019-04-26T14:41:47.537 回答
2

只需在您的动作处理程序中停止传播:

<span> <a href="#" onClick={() => confirm(id)}> Clone </a> <span className="ant-divider" /> <a href="#" onClick={() => confirm(id)}> Replace </a> </span>

于 2017-03-29T09:09:30.907 回答
1
<Menu.Item onClick={(e)=>{ 
            e.domEvent.stopPropagation();
            handleUpdate(id)}}>
            Edit
 </Menu.Item>
于 2021-03-26T05:56:24.020 回答
0

使用 react Link,我们可以anchor通过停止事件传播并在特定列上触发事件来获得默认链接功能。

  • “反应路由器”:“^5.2.0”,
  • 从“react-router-dom”导入{链接};
  render: (value, record) => {

 // Conditional checks if needs to handle on particular set of column 

    return (
     <Link to={'TARGET_LINK_HERE'}
          target="_blank" rel="noopener noreferrer"
          onClick={(event) => {
            event.stopPropagation(); // prevent event to propogate to parent to have row click which is default functionality
         }}>{value}</Link>
    ) 
   }
于 2022-02-22T07:54:18.770 回答