增加统一的列表处理类
This commit is contained in:
36
src/components/utils/list.js
Normal file
36
src/components/utils/list.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import http from "./http";
|
||||
|
||||
class List {
|
||||
constructor(action) {
|
||||
this.action = action
|
||||
this.current = 1
|
||||
this.total = 0
|
||||
this.list = []
|
||||
}
|
||||
|
||||
getData(params) {
|
||||
const {action} = this
|
||||
return http.post(action, null, {params})
|
||||
}
|
||||
|
||||
init(params) {
|
||||
this.getData({...params, current: 1}).then(res => {
|
||||
if (res?.data) {
|
||||
this.list = res.data.records
|
||||
this.total = res.data.total
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
loadMore(params) {
|
||||
if (this.list.length < this.total) {
|
||||
this.getData({...params, current: ++this.current}).then(res => {
|
||||
if (res?.data) {
|
||||
this.list = [...this.list, ...res.data.records]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default List
|
||||
Reference in New Issue
Block a user