Ant Design Table Column Filters 组合Search框折中重置方式
修改的地方: (需要在search 方法里也加入过滤的参数,如果不加引起什么不适反应,可以自己尝试) state初始化 filters
文章图片
在过滤的方法里,设置state filters 的值
文章图片
每次重置后,再次查询时,参数传filters
文章图片
完整代码
import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
import {
Row,
Col,
Card,
Form,
Input,
Select,
Icon,
Button,
Dropdown,
Menu,
InputNumber,
DatePicker,
Modal,
message,
Badge,
Divider,
} from 'antd';
import StandardTable from 'components/StandardTable';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import styles from './TableList.less';
const FormItem = Form.Item;
const { Option } = Select;
const getValue = https://www.it610.com/article/obj =>
Object.keys(obj)
.map(key => obj[key])
.join(',');
const statusMap = ['default', 'processing', 'success', 'error'];
const status = ['关闭', '运行中', '已上线', '异常'];
const CreateForm = Form.create()(props => {
const { modalVisible, form, handleAdd, handleModalVisible } = props;
const okHandle = () => {
form.validateFields((err, fieldsValue) => {
if (err) return;
form.resetFields();
handleAdd(fieldsValue);
});
};
return (
handleModalVisible()}
>
{form.getFieldDecorator('desc', {
rules: [{ required: true, message: 'Please input some description...' }],
})()}
);
});
@connect(({ rule, loading }) => ({
rule,
loading: loading.models.rule,
}))
@Form.create()
export default class TableList extends PureComponent {
state = {
modalVisible: false,
expandForm: false,
selectedRows: [],
formValues: {},
filters :{}
};
componentDidMount() {
const { dispatch } = this.props;
dispatch({
type: 'rule/fetch',
});
}handleStandardTableChange = (pagination, filtersArg, sorter) => {
const { dispatch } = this.props;
const { formValues } = this.state;
const filters = Object.keys(filtersArg).reduce((obj, key) => {
const newObj = { ...obj };
newObj[key] = getValue(filtersArg[key]);
return newObj;
}, {});
console.log(filters);
this.setState({
filters : filters
});
const params = {
currentPage: pagination.current,
pageSize: pagination.pageSize,
...formValues,
...filters,
};
if (sorter.field) {
params.sorter = `${sorter.field}_${sorter.order}`;
}dispatch({
type: 'rule/fetch',
payload: params,
});
};
handleFormReset = () => {
const { form, dispatch } = this.props;
form.resetFields();
this.setState({
formValues: {},
});
dispatch({
type: 'rule/fetch',
payload: {
... this.state.filters
},
});
};
toggleForm = () => {
const { expandForm } = this.state;
this.setState({
expandForm: !expandForm,
});
};
handleMenuClick = e => {
const { dispatch } = this.props;
const { selectedRows } = this.state;
if (!selectedRows) return;
switch (e.key) {
case 'remove':
dispatch({
type: 'rule/remove',
payload: {
no: selectedRows.map(row => row.no).join(','),
},
callback: () => {
this.setState({
selectedRows: [],
});
},
});
break;
default:
break;
}
};
handleSelectRows = rows => {
this.setState({
selectedRows: rows,
});
};
handleSearch = e => {
e.preventDefault();
const { dispatch, form } = this.props;
form.validateFields((err, fieldsValue) => {
if (err) return;
const values = {
...fieldsValue,
updatedAt: fieldsValue.updatedAt && fieldsValue.updatedAt.valueOf(),
};
this.setState({
formValues: values,
});
dispatch({
type: 'rule/fetch',
payload: values,
});
});
};
handleModalVisible = flag => {
this.setState({
modalVisible: !!flag,
});
};
handleAdd = fields => {
const { dispatch } = this.props;
dispatch({
type: 'rule/add',
payload: {
description: fields.desc,
},
});
message.success('添加成功');
this.setState({
modalVisible: false,
});
};
renderSimpleForm() {
const { form } = this.props;
const { getFieldDecorator } = form;
return (
);
}renderAdvancedForm() {
const { form } = this.props;
const { getFieldDecorator } = form;
return (
);
}renderForm() {
const { expandForm } = this.state;
return expandForm ? this.renderAdvancedForm() : this.renderSimpleForm();
}render() {
const {
rule: { data },
loading,
} = this.props;
const { selectedRows, modalVisible } = this.state;
const columns = [
{
title: '规则编号',
dataIndex: 'no',
},
{
title: '描述',
dataIndex: 'description',
},
{
title: '服务调用次数',
dataIndex: 'callNo',
sorter: true,
align: 'right',
render: val => `${val} 万`,
// mark to display a total number
needTotal: true,
},
{
title: '状态',
dataIndex: 'status',
filters: [
{
text: status[0],
value: 0,
},
{
text: status[1],
value: 1,
},
{
text: status[2],
value: 2,
},
{
text: status[3],
value: 3,
},
],
onFilter: (value, record) => record.status.toString() === value,
render(val) {
return ;
},
},
{
title: '更新时间',
dataIndex: 'updatedAt',
sorter: true,
render: val => {moment(val).format('YYYY-MM-DD HH:mm:ss')},
},
{
title: '操作',
render: () => (
"">配置
"vertical" />
"">订阅警报
),
},
];
const menu = (
);
const parentMethods = {
handleAdd: this.handleAdd,
handleModalVisible: this.handleModalVisible,
};
return ("查询表格">
{this.renderForm()}
{selectedRows.length > 0 && (
)}this.handleSelectRows}
onChange={this.handleStandardTableChange}
/>
);
}
}
【Ant Design Table Column Filters组合Search框折中重置方式】希望明天折中方法在 run build 以后依旧好使!
推荐阅读
- 前端|面试官(谈谈Vue和React的区别())
- react|高德地图的使用及自定义图标
- React|【React Native开发】React Native控件之RefreshControl组件详解(21)
- React|React Native开源项目-嘎嘎商城客户端(持续更新中)
- 工作与生活|2016总结,真正新的里程碑和新起点
- React|【React Native开发】React Native应用设备运行(Running)以及调试(Debugging)(3)
- React|【React Native开发】React Native控件之ListView组件讲解以及最齐全实例(19)
- React|React Native控件之PullToRefreshViewAndroid下拉刷新组件讲解(20)
- React|【React Native开发】React Native控件之Text组件讲解(9)
- react学习之旅|react+antd-mobile之项目构建+基础配置