张延森 5 years ago
parent
commit
cf5f41496f

+ 44
- 6
src/layouts/SearchList/SearchForm.jsx View File

1
 import React, { PureComponent } from 'react'
1
 import React, { PureComponent } from 'react'
2
 import PropTypes from 'prop-types'
2
 import PropTypes from 'prop-types'
3
 import createForm from '@zjxpcyc/xrc-form2'
3
 import createForm from '@zjxpcyc/xrc-form2'
4
+import { Button } from 'antd'
4
 
5
 
5
 const Form = createForm()
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
 export default class SearchForm extends PureComponent {
17
 export default class SearchForm extends PureComponent {
8
   static propTypes = {
18
   static propTypes = {
9
     storage: PropTypes.object,
19
     storage: PropTypes.object,
10
     onSubmit: PropTypes.func,
20
     onSubmit: PropTypes.func,
21
+    onCancel: PropTypes.func,
11
     configs: PropTypes.object,
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
   componentDidMount() {
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
   render() {
63
   render() {
26
     const { fields, ...leftProps } = this.props.configs || {}
64
     const { fields, ...leftProps } = this.props.configs || {}
27
 
65
 
28
     return (
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 View File

1
 import React from 'react'
1
 import React from 'react'
2
 import PropTypes from 'prop-types'
2
 import PropTypes from 'prop-types'
3
 import classNames from 'classnames'
3
 import classNames from 'classnames'
4
-import { notification } from 'antd'
4
+import { Spin, notification } from 'antd'
5
 
5
 
6
 import SearchForm from './SearchForm'
6
 import SearchForm from './SearchForm'
7
 import SearchBody from './SearchBody'
7
 import SearchBody from './SearchBody'
19
     body: PropTypes.object,
19
     body: PropTypes.object,
20
     pagination: PropTypes.object,
20
     pagination: PropTypes.object,
21
     service: PropTypes.func,
21
     service: PropTypes.func,
22
+    reServe: PropTypes.func,
22
   }
23
   }
23
 
24
 
24
   state = {
25
   state = {
25
-    defaultStorage: getStorageBuilder(window.Math.random().toString(36).substr(2)),
26
+    defaultStorage: this.props.storage || getStorageBuilder('PAGE-SearchList'),
26
     dataSource: [],
27
     dataSource: [],
27
-    conditions: {},
28
+    conditions: this.getStorage().get('conditions') || {},
29
+    loadding: false,
28
     pageConfig: {
30
     pageConfig: {
29
-      current: 1,
30
-      pageSize: 10,
31
+      ...(this.getStorage().get('pagination') || { current: 1, pageSize: 10 }),
31
       total: 0,
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
     this.setState(
49
     this.setState(
37
       (state) => ({
50
       (state) => ({
38
         ...state,
51
         ...state,
39
         pageConfig: {
52
         pageConfig: {
40
           ...state.pageConfig,
53
           ...state.pageConfig,
41
-          current: page,
54
+          current,
42
           pageSize,
55
           pageSize,
43
         }
56
         }
44
       }),
57
       }),
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
     this.setState(
70
     this.setState(
51
       (state) => ({
71
       (state) => ({
52
         ...state,
72
         ...state,
53
         conditions,
73
         conditions,
74
+        pageConfig: newPageConfig,
54
       }),
75
       }),
55
       this.queryData,
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
   queryData = () => {
88
   queryData = () => {
60
     if (this.props.service) {
89
     if (this.props.service) {
61
       const pageConfigProps = this.props.pagination || {}
90
       const pageConfigProps = this.props.pagination || {}
65
 
94
 
66
       const { conditions = {}, pageConfig } = this.state
95
       const { conditions = {}, pageConfig } = this.state
67
 
96
 
97
+      this.setState({ loadding: true })
98
+
68
       this.props.service({
99
       this.props.service({
69
         ...conditions,
100
         ...conditions,
70
         ...{
101
         ...{
76
 
107
 
77
         this.setState((state) => ({
108
         this.setState((state) => ({
78
           ...state,
109
           ...state,
110
+          loadding: false,
79
           dataSource,
111
           dataSource,
80
           pageConfig: {
112
           pageConfig: {
81
             ...state.pageConfig,
113
             ...state.pageConfig,
83
           },
115
           },
84
         }))
116
         }))
85
       }).catch((e) => {
117
       }).catch((e) => {
118
+        this.setState({ loadding: false })
86
         console.error(e)
119
         console.error(e)
87
       })
120
       })
88
     }
121
     }
101
       ...this.state.pageConfig,
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
     return (
139
     return (
107
       <div className={Style['search-list']}>
140
       <div className={Style['search-list']}>
108
         <div className={Style['head']}>
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
         </div>
143
         </div>
111
         <div className={Style['body']}>
144
         <div className={Style['body']}>
112
           <BodyHeader storage={storage} title={this.props.title} actions={this.props.actions} />
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
           <BodyPagination storage={storage} onChange={this.handlePageChange} configs={pageConfig} />
149
           <BodyPagination storage={storage} onChange={this.handlePageChange} configs={pageConfig} />
115
         </div>
150
         </div>
116
       </div>
151
       </div>
119
 }
154
 }
120
 
155
 
121
 function withSearchList (configs = {}) {
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
   const storage = getStorageBuilder(cacheKey)
158
   const storage = getStorageBuilder(cacheKey)
124
 
159
 
125
   return React.forwardRef((props, ref) => {
160
   return React.forwardRef((props, ref) => {

+ 6
- 0
src/layouts/SearchList/style.less View File

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

+ 5
- 3
src/layouts/SearchList/utils.js View File

16
   return window.JSON.parse(str);
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
   return {
24
   return {
23
     set: (k, v) => {
25
     set: (k, v) => {
24
       const data = getStorage(key) || {}
26
       const data = getStorage(key) || {}
25
 
27
 
26
-      setStorage({ ...data, [`${k}`]: v })
28
+      setStorage(key, { ...data, [`${k}`]: v })
27
     },
29
     },
28
 
30
 
29
     get: (k) => {
31
     get: (k) => {

+ 14
- 4
src/pages/news/type/NewsType.jsx View File

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

117
   }
117
   }
118
 });
118
 });
119
 
119
 
120
-
121
 const fetch = api => options => request(api.url, {...api, ...options || {}})
120
 const fetch = api => options => request(api.url, {...api, ...options || {}})
122
 
121
 
123
 const fetchList = api => options => new Promise((resolve, reject) => {
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
     const { records, ...page  } = res || {}
124
     const { records, ...page  } = res || {}
126
     resolve([records, page])
125
     resolve([records, page])
127
   }).catch(reject)
126
   }).catch(reject)