antd 自定義展開表格

or_so發表於2018-12-03

遇到一個需要自定義文案位置來展開antd表格的問題,antd是支援點選提供的icon進行展開和關閉的, 但是產品非不用呢,就要改,我勸產品不要去改人家的東西,產品非不聽呢,然後就有了下面的程式碼

import { Table } from 'antd';

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: 'test',
      visibily: false,
      expandedRowKeys: [],
    };
  }
// 這裡是重點
  open = (record) => {
    const { id: key } = record;
        const filtered = this.state.expandedRowKeys;
        if (this.state.expandedRowKeys.includes(key)) {
            filtered.splice(filtered.findIndex(element => element === key), 1);
        } else {
            filtered.push(key);
        }
        this.setState({
            expandedRowKeys: filtered,
        });
  }
  render() {
    const { value,expandedRowKeys } = this.state;

    const columns = [
  { title: 'Name', dataIndex: 'name', key: 'name' },
  { title: 'Age', dataIndex: 'age', key: 'age' },
  { title: 'Address', dataIndex: 'address', key: 'address' },
  { title: 'Action', dataIndex: '', key: 'x', render: (record) => <a onClick={() => this.open(record)}>檢視</a> },
];

const data = [
  { id: 1, name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', description: 'My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.' },
  { id: 2, name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', description: 'My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park.' },
  { id: 3, name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', description: 'My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park.' },
];

    return (
      <div>
        <Table
          columns={columns}
          rowKey={(record) => record.id}
          expandedRowRender={record => <p style={{ margin: 0 }}>{record.description}</p>}
          dataSource={data}
          rowClassName="aa" // 這個是把可展開圖示的icon給visibility掉
          expandedRowKeys={expandedRowKeys}
          footer={() => <span style={{ display: 'flex', justifyContent: 'flex-end' }}>or_so©版權所有</span>}
        />
      </div>
    );
  }
}
ReactDOM.render(<Demo />, mountNode);
css 部分 
.aa {
如果不生效,在css程式碼前加入:global覆蓋掉預設的icon
  .ant-table-row-expand-icon-cell {
    .ant-table-row-expand-icon {
      visibility: hidden;
    }
  }
}
程式碼全貼上吧
複製程式碼

antd 自定義展開表格

相關文章