鍍金池/ 問答/HTML/ 關(guān)于antd表格問題 Table

關(guān)于antd表格問題 Table

項目上用到了antd中的Table,數(shù)據(jù)較多時想要弄成滾動,添加scroll這方法時,發(fā)現(xiàn)table中的樣式自動錯亂了,有什么辦法能解決呢

用官網(wǎng)demo試了下,樣式也是錯亂的

const { Table, Button } = antd;
const data = [{
  key: '1',
  name: 'John Brown',
  age: 32,
  address: 'New York No. 1 Lake Park',
}, {
  key: '2',
  name: 'Jim Green',
  age: 42,
  address: 'London No. 1 Lake Park',
}, {
  key: '3',
  name: 'Joe Black',
  age: 32,
  address: 'Sidney No. 1 Lake Park',
}, {
  key: '4',
  name: 'Jim Red',
  age: 32,
  address: 'London No. 2 Lake Park',
}];

class App extends React.Component {
  state = {
    filteredInfo: null,
    sortedInfo: null,
  };
  handleChange = (pagination, filters, sorter) => {
    console.log('Various parameters', pagination, filters, sorter);
    this.setState({
      filteredInfo: filters,
      sortedInfo: sorter,
    });
  }
  render() {
    let { sortedInfo, filteredInfo } = this.state;
    sortedInfo = sortedInfo || {};
    filteredInfo = filteredInfo || {};
    const columns = [{
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      filters: [
        { text: 'Joe', value: 'Joe' },
        { text: 'Jim', value: 'Jim' },
      ],
      filteredValue: filteredInfo.name || null,
      onFilter: (value, record) => record.name.includes(value),
      sorter: (a, b) => a.name.length - b.name.length,
      sortOrder: sortedInfo.columnKey === 'name' && sortedInfo.order,
    }, {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      sorter: (a, b) => a.age - b.age,
      sortOrder: sortedInfo.columnKey === 'age' && sortedInfo.order,
    }, {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
      filters: [
        { text: 'London', value: 'London' },
        { text: 'New York', value: 'New York' },
      ],
      filteredValue: filteredInfo.address || null,
      onFilter: (value, record) => record.address.includes(value),
      sorter: (a, b) => a.address.length - b.address.length,
      sortOrder: sortedInfo.columnKey === 'address' && sortedInfo.order,
    }];
    return (
      <div>
        <Table scroll={{ x: true, y: 100 }} columns={columns} dataSource={data} onChange={this.handleChange} />
      </div>
    );
  }
}

ReactDOM.render(<App />, mountNode);

沒加滾動方法table是正常的

圖片描述

添加滾動方法后,發(fā)現(xiàn)樣式錯亂
scroll={{ x: true, y: 100 }}

圖片描述

回答
編輯回答
夢一場

clipboard.png
官網(wǎng)上說的,要指定列的寬度哦

2017年12月26日 19:03
編輯回答
離人歸

你好,你的問題解決了嗎?我也遇到了這個問題

2018年2月7日 16:46
編輯回答
貓小柒

給每一列指定寬度可以解決這個問題

2017年12月8日 10:22