123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- import React, { PureComponent, Fragment } from 'react';
- import { Alert, Table, Select, Input, Button, Pagination } from 'antd';
- import PropTypes from 'prop-types';
- import styles from './StandardTableList.less';
- function getSearchField(options) {
- if (options && options.keys && options.keys.length) {
- return options.keys[0].field;
- }
- }
- export default class StandardTableList extends PureComponent {
- static propTypes = {
- loading: PropTypes.bool,
- dataSource: PropTypes.array,
- columns: PropTypes.array,
- header: PropTypes.oneOfType([
- PropTypes.object,
- PropTypes.bool,
- ]),
- footer: PropTypes.oneOfType([
- PropTypes.object,
- PropTypes.bool,
- ]),
- showStatusSelect: PropTypes.bool,
- rowSelectable: PropTypes.bool,
- keepUIState: PropTypes.object,
- };
- static defaultProps = {
- keepUIState: {},
- loading: false,
- dataSource: [],
- columns: [],
- header: false,
- footer: false,
- showStatusSelect: true,
- rowSelectable: true,
- };
- constructor(props) {
- super(props);
- const {
- keepUIState,
- header: {
- basicSearch,
- },
- footer: {
- pagination,
- },
- } = this.props;
- this.state = {
- batchActionKey: keepUIState.batchActionKey,
- searchSelectKey: keepUIState.searchSelectKey || getSearchField(basicSearch),
- searchInputValue: keepUIState.searchInputValue || '',
- selectedKeys: keepUIState.selectedKeys || [],
- selectedStatusKey: keepUIState.selectedStatusKey || 'ALL',
- pageNo: keepUIState.pageNo || pagination.pageNo || 1,
- pageSize: keepUIState.pageSize || pagination.pageSize || 15,
- totalSize: keepUIState.totalSize || pagination.totalSize || 0,
- };
- }
- componentWillReceiveProps(nextProps) {
- if (nextProps.footer) {
- const { pagination } = nextProps.footer;
- const { pageNo, pageSize, totalSize } = pagination;
- this.setState({ pageNo, pageSize, totalSize });
- }
- }
- getListHeader = () => {
- const {
- showStatusSelect,
- header: { basicSearch, onAdvanceFilterClick, onCreateClick, onDownload, campusAmount },
- footer: { pagination },
- } = this.props;
- const { keys } = basicSearch;
- return (
- <div className={styles.header}>
- <div className={styles.headerSearch}>
- <div className={styles.basicSearch}>
- <div className={styles.left}>
- {showStatusSelect && (
- <Select
- onChange={this.handleStatusChange}
- value={this.state.selectedStatusKey}
- style={{ width: '20%', marginRight: 10 }}
- >
- <Select.Option key="all" value="ALL">全部状态</Select.Option>
- <Select.Option key="normal" value="NORMAL">正常</Select.Option>
- <Select.Option key="delete" value="DEL">已删除</Select.Option>
- </Select>
- )}
- <Input.Search
- value={this.state.searchInputValue}
- style={{ width: '75%' }}
- addonBefore={
- <Select
- placeholder="请选择"
- value={this.state.searchSelectKey}
- onChange={this.handleSearchSelectChange}
- >
- {keys.map(item => (
- <Select.Option
- key={item.field}
- value={item.field}
- >
- {item.name}
- </Select.Option>))}
- </Select>
- }
- placeholder="请输入"
- enterButton
- onChange={this.handleInputChange}
- onSearch={this.handleSearchBtnClick}
- />
- </div>
- <div className={styles.right}>
- {onAdvanceFilterClick && (
- <a
- className={styles.searchLevel}
- onClick={onAdvanceFilterClick}
- >高级筛选
- </a>
- )}
- {campusAmount !== undefined && (
- <span
- style={{ marginLeft: 5, marginRight: 10, }}
- >当前共有<span style={{fontWeight:600, color:'#5E8732'}}>{campusAmount}</span>个校区
- <span style={{fontWeight:600, color:'#5E8732'}}>{pagination.totalSize}</span>个终端用户
- </span>
- )}
- <Button icon="sync" onClick={this.handleRefreshBtnClick}>刷新</Button>
- {/* noCreate 参数控制是否显示新建按钮 */}
- {onCreateClick !== undefined && (
- <Button
- icon="plus"
- type="primary"
- style={{ marginLeft: 5 }}
- onClick={onCreateClick}
- >新建
- </Button>
- )}
- {onDownload !== undefined && (
- <Button
- icon="download"
- type="primary"
- style={{ marginLeft: 5 }}
- onClick={onDownload}
- >下载
- </Button>
- )}
- </div>
- </div>
- </div>
- <Alert
- message={(
- <Fragment>
- 已选择 <a style={{ fontWeight: 600 }}>{this.state.selectedKeys.length}</a> 项
- 总计 <a style={{ fontWeight: 600 }}>{pagination.totalSize}</a> 项
- </Fragment>
- )}
- type="info"
- showIcon
- />
- </div>
- );
- };
- getListFooter = () => {
- const { footer: { batchActions, pagination } } = this.props;
- const paginationProps = {
- total: this.state.totalSize,
- current: this.state.pageNo,
- pageSize: this.state.pageSize,
- showSizeChanger: true,
- showQuickJumper: true,
- onChange: this.handleListPageChange,
- onShowSizeChange: this.handleListPageSizeChange,
- };
- return (
- <div className={styles.footer}>
- {batchActions && (
- <div className={styles.batch}>
- <Select
- style={{ width: 100 }}
- placeholder="请选择..."
- value={this.state.batchActionKey}
- onChange={this.handleBatchActionSelectChange}
- >
- {batchActions.map(item => (
- <Select.Option key={item.key} value={item.key}>
- {item.name}
- </Select.Option>))}
- </Select>
- <Button
- type="primary"
- style={{ marginLeft: 15 }}
- onClick={this.handleBatchBtnClick}
- disabled={!this.state.selectedKeys.length}
- >确定
- </Button>
- </div>
- )}
- {pagination && (
- <div className={styles.pagination}>
- <Pagination
- {...paginationProps}
- showTotal={total => `共 ${total} 条`}
- />
- </div>)}
- </div>
- );
- };
- // 过滤
- handleFilterOperation = (kv) => {
- const {
- header: {
- onFilterClick,
- },
- } = this.props;
- const {
- searchSelectKey,
- searchInputValue,
- selectedStatusKey,
- pageNo,
- pageSize,
- } = this.state;
- const queryParams = {
- pageNo,
- pageSize,
- [searchSelectKey]: searchInputValue,
- status: selectedStatusKey,
- ...kv,
- };
- if (queryParams.status === 'ALL') {
- delete queryParams.status;
- }
- onFilterClick(queryParams, this.state);
- };
- // 单选/取消单选
- handleItemSelectChange = (itemId, checked) => {
- const { selectedKeys } = this.state;
- const newSelectedKeys = [...selectedKeys];
- if (checked) {
- this.setState({ selectedKeys: [...newSelectedKeys, itemId] });
- } else {
- this.setState({ selectedKeys: newSelectedKeys.filter(a => a !== itemId) });
- }
- };
- // 刷新页面,重置筛选参数
- cleanFilterParams = () => {
- const { header: { basicSearch } } = this.props;
- this.setState({
- batchActionKey: undefined,
- searchSelectKey: getSearchField(basicSearch),
- selectedKeys: [],
- searchInputValue: '',
- allChecked: false,
- selectedStatusKey: 'ALL',
- });
- };
- // 过滤状态
- handleStatusChange = (value) => {
- this.setState({
- selectedStatusKey: value,
- }, () =>
- this.handleFilterOperation({ status: value })
- );
- };
- // 选择搜索字段
- handleSearchSelectChange = (value) => {
- this.setState({ searchSelectKey: value });
- };
- // 响应input输入
- handleInputChange = (e) => {
- this.setState({ searchInputValue: e.target.value });
- };
- // 筛选搜索操作
- handleSearchBtnClick = (value) => {
- const { searchSelectKey } = this.state;
- this.setState({
- searchInputValue: value,
- }, () =>
- this.handleFilterOperation({ [searchSelectKey]: value, pageNo: 1 })
- );
- };
- // 刷新操作
- handleRefreshBtnClick = () => {
- this.handleFilterOperation({});
- };
- // list pageNo变化
- handleListPageChange = (page, pageSize) => {
- this.setState({
- pageSize,
- pageNo: page,
- }, () => {
- this.handleFilterOperation({
- pageSize,
- pageNo: page,
- });
- });
- };
- // list pageSize变化
- handleListPageSizeChange = (current, size) => {
- this.setState({
- pageSize: size,
- pageNo: current,
- }, () => {
- this.handleFilterOperation({
- pageSize: size,
- pageNo: current,
- });
- });
- };
- // 选择批量处理类型
- handleBatchActionSelectChange = (value) => {
- this.setState({ batchActionKey: value });
- };
- // 批量处理操作
- handleBatchBtnClick = () => {
- const { footer: { onBatchClick } } = this.props;
- const { batchActionKey, selectedKeys } = this.state;
- onBatchClick(batchActionKey, selectedKeys);
- };
- handleRowSelectChange = (selectedKeys) => {
- this.setState({ selectedKeys });
- // 把selectedKeys传递给父组件
- if (this.props.rowSelectChange) {
- this.props.rowSelectChange(selectedKeys);
- }
- };
- render() {
- const {
- loading,
- columns,
- dataSource,
- header,
- footer,
- rowSelectable,
- ...restProps
- } = this.props;
- const listFooter = footer ? this.getListFooter : false;
- const listHeader = header ? this.getListHeader : false;
- const { selectedKeys } = this.state;
- const rowSelection = rowSelectable ? {
- selectedKeys,
- onChange: this.handleRowSelectChange,
- getCheckboxProps: record => ({
- disabled: record.disabled,
- }),
- } : null;
- return (
- <Table
- bordered={false}
- title={listHeader}
- footer={listFooter}
- loading={loading}
- rowKey={record => record.key}
- rowSelection={rowSelection}
- columns={columns}
- dataSource={dataSource}
- pagination={false}
- className={styles.table}
- {...restProps}
- />
- );
- }
- }
|