index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React, { PureComponent } from 'react';
  2. import queryString from 'query-string';
  3. import { connect } from 'dva';
  4. import { routerRedux } from 'dva/router';
  5. import { Card } from 'antd';
  6. import TableList from './table';
  7. import Search from './search';
  8. import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
  9. import { Codes } from '../../../utils/config';
  10. @connect(state => ({ training: state.training }))
  11. export default class Training extends PureComponent {
  12. render() {
  13. const { location, dispatch, training } = this.props;
  14. location.query = queryString.parse(location.search);
  15. const { query, pathname } = location;
  16. const { field, keyword, ...filters } = query;
  17. const { list, listLoading, pagination, currentItem, itemLoading, modalVisible, modalType } = training;
  18. // 把携带的参数中空值项删除
  19. Object.keys(filters).map(key => { filters[key] ? null : delete filters[key] });
  20. // 如果搜索内容不为空则添加进filters中
  21. if (field && keyword) {
  22. filters.field = field;
  23. filters.keyword = keyword;
  24. }
  25. const searchProps = {
  26. field,
  27. keyword,
  28. onSearch: (payload) => {
  29. if (!payload.keyword.length) {
  30. delete payload.field;
  31. delete payload.keyword;
  32. }
  33. dispatch(routerRedux.push({
  34. pathname,
  35. search: queryString.stringify({
  36. ...payload
  37. })
  38. }));
  39. },
  40. onAdd: () => {
  41. dispatch(
  42. routerRedux.push({
  43. pathname: '/product/training/add',
  44. state: filters,
  45. })
  46. );
  47. }
  48. };
  49. const listProps = {
  50. pagination,
  51. location,
  52. dataSource: list,
  53. loading: listLoading,
  54. onChange: (pagination, filterArgs) => {
  55. const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
  56. const tableFilters = Object.keys(filterArgs).reduce((obj, key) => {
  57. const newObj = { ...obj };
  58. newObj[key] = getValue(filterArgs[key]);
  59. return newObj;
  60. }, {});
  61. const data = { ...filters, ...tableFilters };
  62. Object.keys(data).map(key => data[key] ? null : delete data[key]);
  63. dispatch(routerRedux.push({
  64. pathname,
  65. search: queryString.stringify({
  66. ...data,
  67. pageNo: pagination.current,
  68. pageSize: pagination.pageSize,
  69. }),
  70. }));
  71. },
  72. onEditItem: ({ pid }) => {
  73. dispatch(
  74. routerRedux.push({
  75. pathname: `/product/training/edit/${pid}`,
  76. state: filters,
  77. })
  78. );
  79. },
  80. onDeleteItem: (id) => {
  81. dispatch({
  82. type: 'training/delete',
  83. payload: id,
  84. callback: () => {
  85. dispatch(
  86. routerRedux.push({
  87. pathname,
  88. search: queryString.stringify(filters),
  89. })
  90. );
  91. }
  92. });
  93. },
  94. };
  95. return (
  96. <PageHeaderLayout>
  97. <Card>
  98. <Search { ...searchProps } />
  99. <TableList { ...listProps } />
  100. </Card>
  101. </PageHeaderLayout>
  102. );
  103. }
  104. }