张延森 5 år sedan
förälder
incheckning
cf5f41496f

+ 44
- 6
src/layouts/SearchList/SearchForm.jsx Visa fil

@@ -1,32 +1,70 @@
1 1
 import React, { PureComponent } from 'react'
2 2
 import PropTypes from 'prop-types'
3 3
 import createForm from '@zjxpcyc/xrc-form2'
4
+import { Button } from 'antd'
4 5
 
5 6
 const Form = createForm()
6 7
 
8
+function clearProperties (o) {
9
+  return Object.keys(o).reduce((acc, k) => {
10
+    return {
11
+      ...acc,
12
+      [`${k}`]: undefined,
13
+    }
14
+  }, {})
15
+}
16
+
7 17
 export default class SearchForm extends PureComponent {
8 18
   static propTypes = {
9 19
     storage: PropTypes.object,
10 20
     onSubmit: PropTypes.func,
21
+    onCancel: PropTypes.func,
11 22
     configs: PropTypes.object,
12 23
   }
13 24
 
14
-  form = null
15
-  storage = this.props.storage
25
+  wrapperForm = null
26
+  storage = this.props.storage || {}
16 27
 
17 28
   componentDidMount() {
18
-    const conditions = this.storage.get('form.values')
29
+    const conditions = this.storage.get('conditions')
19 30
 
20
-    if (this.form && conditions) {
21
-      this.form.setFieldsValue(conditions)
31
+    if (this.wrapperForm && conditions) {
32
+      this.wrapperForm.props.form.setFieldsValue(conditions)
22 33
     }
23 34
   }
24 35
 
36
+  handleCancel = () => {
37
+    const conditions = this.storage.get('conditions') || {}
38
+    this.storage.set('conditions', {})
39
+
40
+    if (this.wrapperForm) {
41
+      this.wrapperForm.props.form.setFieldsValue(clearProperties(conditions))
42
+    }
43
+
44
+    if (this.props.onCancel) {
45
+      this.props.onCancel()
46
+    }
47
+  }
48
+
49
+  wrapperFields = (fields) => {
50
+    return [
51
+      ...fields,
52
+      {
53
+        action: true,
54
+        render: (props) => (<Button htmlType="submit" type="primary">搜索</Button>)
55
+      },
56
+      {
57
+        action: true,
58
+        render: (props) => (<Button htmlType="button" onClick={this.handleCancel} style={{ marginLeft: '16px' }}>重置</Button>)
59
+      },
60
+    ]
61
+  }
62
+
25 63
   render() {
26 64
     const { fields, ...leftProps } = this.props.configs || {}
27 65
 
28 66
     return (
29
-      <Form wrappedComponentRef={f => this.form = f} layout="inline" fields={fields} onSubmit={this.props.onSubmit} {...leftProps} />
67
+      <Form wrappedComponentRef={f => this.wrapperForm = f} submitBtn={false} cancelBtn={false} layout="inline" fields={this.wrapperFields(fields)} onSubmit={this.props.onSubmit} {...leftProps} />
30 68
     )
31 69
   }
32 70
 }

+ 47
- 12
src/layouts/SearchList/index.jsx Visa fil

@@ -1,7 +1,7 @@
1 1
 import React from 'react'
2 2
 import PropTypes from 'prop-types'
3 3
 import classNames from 'classnames'
4
-import { notification } from 'antd'
4
+import { Spin, notification } from 'antd'
5 5
 
6 6
 import SearchForm from './SearchForm'
7 7
 import SearchBody from './SearchBody'
@@ -19,26 +19,39 @@ class SearchList extends React.PureComponent {
19 19
     body: PropTypes.object,
20 20
     pagination: PropTypes.object,
21 21
     service: PropTypes.func,
22
+    reServe: PropTypes.func,
22 23
   }
23 24
 
24 25
   state = {
25
-    defaultStorage: getStorageBuilder(window.Math.random().toString(36).substr(2)),
26
+    defaultStorage: this.props.storage || getStorageBuilder('PAGE-SearchList'),
26 27
     dataSource: [],
27
-    conditions: {},
28
+    conditions: this.getStorage().get('conditions') || {},
29
+    loadding: false,
28 30
     pageConfig: {
29
-      current: 1,
30
-      pageSize: 10,
31
+      ...(this.getStorage().get('pagination') || { current: 1, pageSize: 10 }),
31 32
       total: 0,
32 33
     },
33 34
   }
34 35
 
35
-  handlePageChange = (page, pageSize) => {
36
+  componentDidMount () {
37
+    if (this.props.reServe) {
38
+      this.props.reServe(this.queryData)
39
+    }
40
+  }
41
+  
42
+  getStorage() {
43
+    return this.props.storage || this.state.defaultStorage
44
+  }
45
+
46
+  handlePageChange = (current, pageSize) => {
47
+    this.getStorage().set('pagination', { current, pageSize: 10 })
48
+
36 49
     this.setState(
37 50
       (state) => ({
38 51
         ...state,
39 52
         pageConfig: {
40 53
           ...state.pageConfig,
41
-          current: page,
54
+          current,
42 55
           pageSize,
43 56
         }
44 57
       }),
@@ -46,16 +59,32 @@ class SearchList extends React.PureComponent {
46 59
     )
47 60
   }
48 61
 
49
-  handleSearchSubmit = (conditions) => {
62
+  repondToSearch = (conditions) => {
63
+    // 查询提交会重置页码
64
+    const { pageConfig } = this.state
65
+    const newPageConfig = { ...pageConfig, current: 1, total: 0 }
66
+
67
+    this.getStorage().set('conditions', conditions)
68
+    this.getStorage().set('pagination', newPageConfig)
69
+
50 70
     this.setState(
51 71
       (state) => ({
52 72
         ...state,
53 73
         conditions,
74
+        pageConfig: newPageConfig,
54 75
       }),
55 76
       this.queryData,
56 77
     )
57 78
   }
58 79
 
80
+  handleSearchSubmit = (conditions) => {
81
+    this.repondToSearch(conditions)
82
+  }
83
+
84
+  handleSearchCancel = () => {
85
+    this.repondToSearch({})
86
+  }
87
+
59 88
   queryData = () => {
60 89
     if (this.props.service) {
61 90
       const pageConfigProps = this.props.pagination || {}
@@ -65,6 +94,8 @@ class SearchList extends React.PureComponent {
65 94
 
66 95
       const { conditions = {}, pageConfig } = this.state
67 96
 
97
+      this.setState({ loadding: true })
98
+
68 99
       this.props.service({
69 100
         ...conditions,
70 101
         ...{
@@ -76,6 +107,7 @@ class SearchList extends React.PureComponent {
76 107
 
77 108
         this.setState((state) => ({
78 109
           ...state,
110
+          loadding: false,
79 111
           dataSource,
80 112
           pageConfig: {
81 113
             ...state.pageConfig,
@@ -83,6 +115,7 @@ class SearchList extends React.PureComponent {
83 115
           },
84 116
         }))
85 117
       }).catch((e) => {
118
+        this.setState({ loadding: false })
86 119
         console.error(e)
87 120
       })
88 121
     }
@@ -101,16 +134,18 @@ class SearchList extends React.PureComponent {
101 134
       ...this.state.pageConfig,
102 135
     }
103 136
 
104
-    const storage = this.props.storage || this.state.defaultStorage
137
+    const storage = this.getStorage()
105 138
 
106 139
     return (
107 140
       <div className={Style['search-list']}>
108 141
         <div className={Style['head']}>
109
-          <SearchForm storage={storage} onSubmit={this.handleSearchSubmit} configs={this.props.search} />
142
+          <SearchForm storage={storage} onSubmit={this.handleSearchSubmit} onCancel={this.handleSearchCancel} configs={this.props.search} />
110 143
         </div>
111 144
         <div className={Style['body']}>
112 145
           <BodyHeader storage={storage} title={this.props.title} actions={this.props.actions} />
113
-          <SearchBody storage={storage} dataSource={this.state.dataSource} configs={this.props.body} />
146
+          <Spin spinning={this.state.loadding}>
147
+            <SearchBody storage={storage} dataSource={this.state.dataSource} configs={this.props.body} />
148
+          </Spin>
114 149
           <BodyPagination storage={storage} onChange={this.handlePageChange} configs={pageConfig} />
115 150
         </div>
116 151
       </div>
@@ -119,7 +154,7 @@ class SearchList extends React.PureComponent {
119 154
 }
120 155
 
121 156
 function withSearchList (configs = {}) {
122
-  const cacheKey = `${configs.name || 'SearchList'}-${window.Math.random().toString(36).substr(2)}`
157
+  const cacheKey = `PAGE-${configs.name || 'SearchList'}`
123 158
   const storage = getStorageBuilder(cacheKey)
124 159
 
125 160
   return React.forwardRef((props, ref) => {

+ 6
- 0
src/layouts/SearchList/style.less Visa fil

@@ -8,6 +8,12 @@
8 8
   }
9 9
 
10 10
   .body {
11
+    &::after {
12
+      content: '';
13
+      clear: both;
14
+      display: block;
15
+    }
16
+
11 17
     .body-header {
12 18
       display: flex;
13 19
       align-items: center;

+ 5
- 3
src/layouts/SearchList/utils.js Visa fil

@@ -16,14 +16,16 @@ export function getStorage(k) {
16 16
   return window.JSON.parse(str);
17 17
 }
18 18
 
19
-export function getStorageBuilder(key, initData = {}) {
20
-  setStorage(key, initData)  
19
+export function getStorageBuilder(key, initData) {
20
+  if (initData) {
21
+    setStorage(key, initData)  
22
+  }
21 23
 
22 24
   return {
23 25
     set: (k, v) => {
24 26
       const data = getStorage(key) || {}
25 27
 
26
-      setStorage({ ...data, [`${k}`]: v })
28
+      setStorage(key, { ...data, [`${k}`]: v })
27 29
     },
28 30
 
29 31
     get: (k) => {

+ 14
- 4
src/pages/news/type/NewsType.jsx Visa fil

@@ -10,10 +10,12 @@ import { fetchList, apis } from '../../../utils/request'
10 10
 import { withSearchList } from '@/layouts/SearchList'
11 11
 
12 12
 const { Option } = Select;
13
-const SearchList = withSearchList()
13
+const SearchList = withSearchList({ name: 'NEWSTYPE' })
14 14
 const getNewsTypeList = fetchList(apis.newsType.list)
15 15
 
16 16
 export default function NewsType(props) {
17
+  let queryData = null
18
+
17 19
   const conditions = [
18 20
     {
19 21
       name: 'buildingId',
@@ -57,8 +59,13 @@ export default function NewsType(props) {
57 59
       ),
58 60
     },
59 61
   ];
60
-  
61 62
 
63
+  const handleReServe = (callback) => {
64
+    queryData = callback
65
+    // 立即调用一次
66
+    queryData()
67
+  }
68
+  
62 69
   const renderActions = () => {
63 70
     return (
64 71
       <AuthButton name="admin.taNewsType.post" noRight={null}>
@@ -67,14 +74,17 @@ export default function NewsType(props) {
67 74
     );
68 75
   }
69 76
 
77
+  const rowKey = r => r.newsTypeId
78
+
70 79
   return (
71 80
     <SearchList
72 81
       title="资讯类型"
73 82
       actions={renderActions()}
74 83
       service={getNewsTypeList}
75 84
       search={{ fields: conditions }}
76
-      body={{ columns }}
77
-      pagination={{}}
85
+      reServe={handleReServe}
86
+      body={{ columns, rowKey }}
87
+      pagination={{ keyOfPageNumber: 'pageNum' }}
78 88
     />
79 89
   )
80 90
 }

+ 1
- 2
src/utils/request.js Visa fil

@@ -117,11 +117,10 @@ request.interceptors.response.use(async (response, options) => {
117 117
   }
118 118
 });
119 119
 
120
-
121 120
 const fetch = api => options => request(api.url, {...api, ...options || {}})
122 121
 
123 122
 const fetchList = api => options => new Promise((resolve, reject) => {
124
-  request(api.url, {...api, ...options || {}}).then((res) => {
123
+  request(api.url, {...api, params: options }).then((res) => {
125 124
     const { records, ...page  } = res || {}
126 125
     resolve([records, page])
127 126
   }).catch(reject)