带有开关组件的antd表

时间:2018-09-06 09:36:28

标签: antd

是否可以通过切换ant design表中的开关来获取行信息?

https://codesandbox.io/s/mmvrwy2jkp

2 个答案:

答案 0 :(得分:0)

是的,render函数的第二个参数是记录。 你可以做到

{
  title: 'switch',
  dataIndex: 'age',
  key: 'age',
    render: (e, record) => (< Switch onChange={() => handleSwitchChange(record)} defaultChecked={e} />)
}

答案 1 :(得分:0)

这是我在使用 Ant 设计时处理每个行项目上的开关组件的方式。也许这可以给你一些提示。

表格列

const COLUMN =
     {
          title: 'Status',
          key: 'status',
          dataIndex: 'status',
          // status is the data from api 
          // index is the table index which could be used to get corresponding data
          render: (status, record, index) => {
            const onToggle = (checked) => {
              status = checked;
              onActiveUser(index, status);
            };
            return (
              <Space>
                <Switch defaultChecked={status} onChange={onToggle} />
              </Space>
            );
          },
        },

   const onActiveUser = (index, status) => {
        axios.patch({ id: users[index].id }, { is_active: status })
          .then((response) => {
            console.log(response);
          })
          .catch(() => {
            console.log('Failed!');
          });
      };
相关问题