Skip to content

Instantly share code, notes, and snippets.

@gnosis23
Created August 9, 2018 15:14
Show Gist options
  • Save gnosis23/87e04dd802582d6c0a7a27064f576d17 to your computer and use it in GitHub Desktop.
Save gnosis23/87e04dd802582d6c0a7a27064f576d17 to your computer and use it in GitHub Desktop.
React Table
import React, {Fragment} from 'react';
import {Row, Col, Table, Button, Checkbox, Dropdown, Menu} from 'antd';
export default class GreatTable extends React.Component {
state = {
visible: false,
columns: [],
};
componentWillReceiveProps(nextProps) {
this.setState({columns: nextProps.columns})
}
handleVisibleChange = (flag) => {
this.setState({ visible: flag });
};
handleChecked = (v, i) => {
const nextColumns = this.state.columns.map((col, id) => {
return id === i ? {...col, visible: !col.visible} : col
});
this.setState({columns: nextColumns})
};
render() {
const {columns, ...rest} = this.props;
const nextColumns = this.state.columns.filter(column => column.visible);
const menu = (
<Menu onClick={() => {}}>
{this.state.columns.map((option, idx) => {
return (
<Menu.Item key={option.key}>
<Checkbox
checked={option.visible}
onChange={e => this.handleChecked(e.target.checked, idx)}
> {option.title}
</Checkbox>
</Menu.Item>
)
})}
</Menu>
);
return (
<Fragment>
<Row type="flex" justify="end">
<Col span={2} offset={20}>
<Dropdown
overlay={menu}
placement="bottomLeft"
visible={this.state.visible}
onVisibleChange={this.handleVisibleChange}
>
<Button style={{margin: 5}}>列表选项</Button>
</Dropdown>
</Col>
</Row>
<Row>
<Table {...rest} columns={nextColumns} />
</Row>
</Fragment>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment