TableList.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import React, { PureComponent } from 'react';
  2. import { connect } from 'dva';
  3. import { Row, Col, Card, Form, Input, Select, Icon, Button, Dropdown, Menu, InputNumber, DatePicker, Modal, message } from 'antd';
  4. import StandardTable from '../../components/StandardTable';
  5. import PageHeaderLayout from '../../layouts/PageHeaderLayout';
  6. import styles from './TableList.less';
  7. const FormItem = Form.Item;
  8. const { Option } = Select;
  9. const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
  10. @connect(state => ({
  11. rule: state.rule,
  12. }))
  13. @Form.create()
  14. export default class TableList extends PureComponent {
  15. state = {
  16. addInputValue: '',
  17. modalVisible: false,
  18. expandForm: false,
  19. selectedRows: [],
  20. formValues: {},
  21. };
  22. componentDidMount() {
  23. const { dispatch } = this.props;
  24. dispatch({
  25. type: 'rule/fetch',
  26. });
  27. }
  28. handleStandardTableChange = (pagination, filtersArg, sorter) => {
  29. const { dispatch } = this.props;
  30. const { formValues } = this.state;
  31. const filters = Object.keys(filtersArg).reduce((obj, key) => {
  32. const newObj = { ...obj };
  33. newObj[key] = getValue(filtersArg[key]);
  34. return newObj;
  35. }, {});
  36. const params = {
  37. currentPage: pagination.current,
  38. pageSize: pagination.pageSize,
  39. ...formValues,
  40. ...filters,
  41. };
  42. if (sorter.field) {
  43. params.sorter = `${sorter.field}_${sorter.order}`;
  44. }
  45. dispatch({
  46. type: 'rule/fetch',
  47. payload: params,
  48. });
  49. }
  50. handleFormReset = () => {
  51. const { form, dispatch } = this.props;
  52. form.resetFields();
  53. dispatch({
  54. type: 'rule/fetch',
  55. payload: {},
  56. });
  57. }
  58. toggleForm = () => {
  59. this.setState({
  60. expandForm: !this.state.expandForm,
  61. });
  62. }
  63. handleMenuClick = (e) => {
  64. const { dispatch } = this.props;
  65. const { selectedRows } = this.state;
  66. if (!selectedRows) return;
  67. switch (e.key) {
  68. case 'remove':
  69. dispatch({
  70. type: 'rule/remove',
  71. payload: {
  72. no: selectedRows.map(row => row.no).join(','),
  73. },
  74. callback: () => {
  75. this.setState({
  76. selectedRows: [],
  77. });
  78. },
  79. });
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. handleSelectRows = (rows) => {
  86. this.setState({
  87. selectedRows: rows,
  88. });
  89. }
  90. handleSearch = (e) => {
  91. e.preventDefault();
  92. const { dispatch, form } = this.props;
  93. form.validateFields((err, fieldsValue) => {
  94. if (err) return;
  95. const values = {
  96. ...fieldsValue,
  97. updatedAt: fieldsValue.updatedAt && fieldsValue.updatedAt.valueOf(),
  98. };
  99. this.setState({
  100. formValues: values,
  101. });
  102. dispatch({
  103. type: 'rule/fetch',
  104. payload: values,
  105. });
  106. });
  107. }
  108. handleModalVisible = (flag) => {
  109. this.setState({
  110. modalVisible: !!flag,
  111. });
  112. }
  113. handleAddInput = (e) => {
  114. this.setState({
  115. addInputValue: e.target.value,
  116. });
  117. }
  118. handleAdd = () => {
  119. this.props.dispatch({
  120. type: 'rule/add',
  121. payload: {
  122. description: this.state.addInputValue,
  123. },
  124. });
  125. message.success('添加成功');
  126. this.setState({
  127. modalVisible: false,
  128. });
  129. }
  130. renderSimpleForm() {
  131. const { getFieldDecorator } = this.props.form;
  132. return (
  133. <Form onSubmit={this.handleSearch} layout="inline">
  134. <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
  135. <Col md={8} sm={24}>
  136. <FormItem label="规则编号">
  137. {getFieldDecorator('no')(
  138. <Input placeholder="请输入" />
  139. )}
  140. </FormItem>
  141. </Col>
  142. <Col md={8} sm={24}>
  143. <FormItem label="使用状态">
  144. {getFieldDecorator('status')(
  145. <Select placeholder="请选择" style={{ width: '100%' }}>
  146. <Option value="0">关闭</Option>
  147. <Option value="1">运行中</Option>
  148. </Select>
  149. )}
  150. </FormItem>
  151. </Col>
  152. <Col md={8} sm={24}>
  153. <span className={styles.submitButtons}>
  154. <Button type="primary" htmlType="submit">查询</Button>
  155. <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>重置</Button>
  156. <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
  157. 展开 <Icon type="down" />
  158. </a>
  159. </span>
  160. </Col>
  161. </Row>
  162. </Form>
  163. );
  164. }
  165. renderAdvancedForm() {
  166. const { getFieldDecorator } = this.props.form;
  167. return (
  168. <Form onSubmit={this.handleSearch} layout="inline">
  169. <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
  170. <Col md={8} sm={24}>
  171. <FormItem label="规则编号">
  172. {getFieldDecorator('no')(
  173. <Input placeholder="请输入" />
  174. )}
  175. </FormItem>
  176. </Col>
  177. <Col md={8} sm={24}>
  178. <FormItem label="使用状态">
  179. {getFieldDecorator('status')(
  180. <Select placeholder="请选择" style={{ width: '100%' }}>
  181. <Option value="0">关闭</Option>
  182. <Option value="1">运行中</Option>
  183. </Select>
  184. )}
  185. </FormItem>
  186. </Col>
  187. <Col md={8} sm={24}>
  188. <FormItem label="调用次数">
  189. {getFieldDecorator('number')(
  190. <InputNumber style={{ width: '100%' }} />
  191. )}
  192. </FormItem>
  193. </Col>
  194. </Row>
  195. <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
  196. <Col md={8} sm={24}>
  197. <FormItem label="更新日期">
  198. {getFieldDecorator('date')(
  199. <DatePicker style={{ width: '100%' }} placeholder="请输入更新日期" />
  200. )}
  201. </FormItem>
  202. </Col>
  203. <Col md={8} sm={24}>
  204. <FormItem label="使用状态">
  205. {getFieldDecorator('status3')(
  206. <Select placeholder="请选择" style={{ width: '100%' }}>
  207. <Option value="0">关闭</Option>
  208. <Option value="1">运行中</Option>
  209. </Select>
  210. )}
  211. </FormItem>
  212. </Col>
  213. <Col md={8} sm={24}>
  214. <FormItem label="使用状态">
  215. {getFieldDecorator('status4')(
  216. <Select placeholder="请选择" style={{ width: '100%' }}>
  217. <Option value="0">关闭</Option>
  218. <Option value="1">运行中</Option>
  219. </Select>
  220. )}
  221. </FormItem>
  222. </Col>
  223. </Row>
  224. <div style={{ overflow: 'hidden' }}>
  225. <span style={{ float: 'right', marginBottom: 24 }}>
  226. <Button type="primary" htmlType="submit">查询</Button>
  227. <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>重置</Button>
  228. <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
  229. 收起 <Icon type="up" />
  230. </a>
  231. </span>
  232. </div>
  233. </Form>
  234. );
  235. }
  236. renderForm() {
  237. return this.state.expandForm ? this.renderAdvancedForm() : this.renderSimpleForm();
  238. }
  239. render() {
  240. const { rule: { loading: ruleLoading, data } } = this.props;
  241. const { selectedRows, modalVisible, addInputValue } = this.state;
  242. const menu = (
  243. <Menu onClick={this.handleMenuClick} selectedKeys={[]}>
  244. <Menu.Item key="remove">删除</Menu.Item>
  245. <Menu.Item key="approval">批量审批</Menu.Item>
  246. </Menu>
  247. );
  248. return (
  249. <PageHeaderLayout title="查询表格">
  250. <Card bordered={false}>
  251. <div className={styles.tableList}>
  252. <div className={styles.tableListForm}>
  253. {this.renderForm()}
  254. </div>
  255. <div className={styles.tableListOperator}>
  256. <Button icon="plus" type="primary" onClick={() => this.handleModalVisible(true)}>新建</Button>
  257. {
  258. selectedRows.length > 0 && (
  259. <span>
  260. <Button>批量操作</Button>
  261. <Dropdown overlay={menu}>
  262. <Button>
  263. 更多操作 <Icon type="down" />
  264. </Button>
  265. </Dropdown>
  266. </span>
  267. )
  268. }
  269. </div>
  270. <StandardTable
  271. selectedRows={selectedRows}
  272. loading={ruleLoading}
  273. data={data}
  274. onSelectRow={this.handleSelectRows}
  275. onChange={this.handleStandardTableChange}
  276. />
  277. </div>
  278. </Card>
  279. <Modal
  280. title="新建规则"
  281. visible={modalVisible}
  282. onOk={this.handleAdd}
  283. onCancel={() => this.handleModalVisible()}
  284. >
  285. <FormItem
  286. labelCol={{ span: 5 }}
  287. wrapperCol={{ span: 15 }}
  288. label="描述"
  289. >
  290. <Input placeholder="请输入" onChange={this.handleAddInput} value={addInputValue} />
  291. </FormItem>
  292. </Modal>
  293. </PageHeaderLayout>
  294. );
  295. }
  296. }