2023-12-22 16:19:33 +08:00
|
|
|
import { reactive } from 'vue'
|
2023-12-26 16:17:30 +08:00
|
|
|
import createAxios from '@/utils/request'
|
|
|
|
|
import { requestPayload } from '@/utils/request'
|
|
|
|
|
import { Method } from 'axios'
|
2023-12-22 16:19:33 +08:00
|
|
|
|
|
|
|
|
interface TableStoreParams {
|
|
|
|
|
url: string
|
|
|
|
|
pk?: string
|
|
|
|
|
column: TableColumn[]
|
|
|
|
|
params?: anyObj
|
2023-12-26 16:17:30 +08:00
|
|
|
method?: Method
|
2023-12-29 13:23:11 +08:00
|
|
|
isWebPaging?: boolean // 是否前端分页
|
|
|
|
|
resetCallback?: () => void
|
|
|
|
|
loadCallback?: () => void
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
2023-12-26 16:17:30 +08:00
|
|
|
|
2023-12-22 16:19:33 +08:00
|
|
|
export default class TableStore {
|
|
|
|
|
public url
|
|
|
|
|
public pk
|
2023-12-26 16:17:30 +08:00
|
|
|
public method: Method
|
2023-12-27 10:39:40 +08:00
|
|
|
public initData: any = null
|
2023-12-28 09:59:28 +08:00
|
|
|
public isWebPaging = false
|
2023-12-22 16:19:33 +08:00
|
|
|
public table: CnTable = reactive({
|
|
|
|
|
ref: null,
|
|
|
|
|
selection: [],
|
|
|
|
|
data: [],
|
2023-12-28 09:59:28 +08:00
|
|
|
webPagingData: [],
|
2023-12-22 16:19:33 +08:00
|
|
|
total: 0,
|
|
|
|
|
params: {
|
|
|
|
|
pageNum: 1,
|
2023-12-28 09:59:28 +08:00
|
|
|
pageSize: 20
|
2023-12-22 16:19:33 +08:00
|
|
|
},
|
|
|
|
|
loading: true,
|
2023-12-29 13:23:11 +08:00
|
|
|
column: [],
|
|
|
|
|
loadCallback: null,
|
|
|
|
|
resetCallback: null
|
2023-12-22 16:19:33 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
constructor(public options: TableStoreParams) {
|
|
|
|
|
this.url = options.url
|
|
|
|
|
this.pk = options.pk || 'id'
|
2023-12-28 09:59:28 +08:00
|
|
|
this.isWebPaging = options.isWebPaging || false
|
2023-12-26 16:17:30 +08:00
|
|
|
this.method = options.method || 'GET'
|
2023-12-22 16:19:33 +08:00
|
|
|
this.table.column = options.column
|
2023-12-29 13:23:11 +08:00
|
|
|
this.table.resetCallback = options.resetCallback || null
|
2023-12-29 14:46:20 +08:00
|
|
|
this.table.loadCallback = options.loadCallback || null
|
2023-12-22 16:19:33 +08:00
|
|
|
Object.assign(this.table.params, options.params)
|
|
|
|
|
}
|
2023-12-26 16:17:30 +08:00
|
|
|
|
2023-12-22 16:19:33 +08:00
|
|
|
index() {
|
2023-12-27 10:39:40 +08:00
|
|
|
// 重置用的数据数据
|
|
|
|
|
if (!this.initData) {
|
|
|
|
|
this.initData = JSON.parse(JSON.stringify(this.table.params))
|
|
|
|
|
}
|
2023-12-26 16:17:30 +08:00
|
|
|
createAxios(
|
|
|
|
|
Object.assign(
|
|
|
|
|
{
|
|
|
|
|
url: this.url,
|
|
|
|
|
method: this.method
|
|
|
|
|
},
|
|
|
|
|
requestPayload(this.method, this.table.params)
|
|
|
|
|
)
|
|
|
|
|
).then((res: any) => {
|
|
|
|
|
this.table.data = res.data.records || res.data
|
|
|
|
|
this.table.total = res.data.total || res.data.length
|
2023-12-28 09:59:28 +08:00
|
|
|
if (this.isWebPaging) {
|
|
|
|
|
this.table.webPagingData = window.XEUtils.chunk(this.table.data, this.table.params.pageSize)
|
|
|
|
|
this.table.data = this.table.webPagingData[this.table.params.pageNum - 1]
|
|
|
|
|
}
|
2023-12-29 13:23:11 +08:00
|
|
|
this.table.loadCallback && this.table.loadCallback()
|
2023-12-26 16:17:30 +08:00
|
|
|
this.table.loading = false
|
|
|
|
|
})
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
2023-12-26 16:17:30 +08:00
|
|
|
|
2023-12-22 16:19:33 +08:00
|
|
|
/**
|
|
|
|
|
* 表格内的事件统一响应
|
|
|
|
|
* @param event 事件:selection-change=选中项改变,page-size-change=每页数量改变,current-page-change=翻页
|
|
|
|
|
* @param data 携带数据
|
|
|
|
|
*/
|
|
|
|
|
onTableAction = (event: string, data: anyObj) => {
|
|
|
|
|
const actionFun = new Map([
|
2023-12-27 10:39:40 +08:00
|
|
|
[
|
|
|
|
|
'search',
|
|
|
|
|
() => {
|
|
|
|
|
this.table.params.pageNum = 1
|
|
|
|
|
this.index()
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'reset',
|
|
|
|
|
() => {
|
|
|
|
|
delete this.initData.pageSize
|
|
|
|
|
Object.assign(this.table.params, this.initData)
|
|
|
|
|
this.index()
|
2023-12-29 13:23:11 +08:00
|
|
|
this.table.resetCallback && this.table.resetCallback()
|
2023-12-27 10:39:40 +08:00
|
|
|
}
|
|
|
|
|
],
|
2023-12-22 16:19:33 +08:00
|
|
|
[
|
|
|
|
|
'selection-change',
|
|
|
|
|
() => {
|
|
|
|
|
this.table.selection = data as TableRow[]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'page-size-change',
|
|
|
|
|
() => {
|
2023-12-28 09:59:28 +08:00
|
|
|
this.table.params.pageSize = data.size
|
|
|
|
|
this.table.params.pageNum = 1
|
|
|
|
|
|
|
|
|
|
if (this.isWebPaging) {
|
|
|
|
|
this.table.webPagingData = window.XEUtils.chunk(
|
|
|
|
|
window.XEUtils.flatten(this.table.webPagingData),
|
|
|
|
|
this.table.params.pageSize
|
|
|
|
|
)
|
|
|
|
|
this.table.data = this.table.webPagingData[this.table.params.pageNum - 1]
|
|
|
|
|
} else {
|
|
|
|
|
this.index()
|
|
|
|
|
}
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'current-page-change',
|
|
|
|
|
() => {
|
2023-12-28 09:59:28 +08:00
|
|
|
this.table.params.pageNum = data.page
|
|
|
|
|
if (this.isWebPaging) {
|
|
|
|
|
this.table.data = this.table.webPagingData[data.page - 1]
|
|
|
|
|
} else {
|
|
|
|
|
this.index()
|
|
|
|
|
}
|
2023-12-22 16:19:33 +08:00
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'field-change',
|
|
|
|
|
() => {
|
|
|
|
|
console.warn('field-change')
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'default',
|
|
|
|
|
() => {
|
|
|
|
|
console.warn('No action defined')
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
])
|
|
|
|
|
const action = actionFun.get(event) || actionFun.get('default')
|
|
|
|
|
action!.call(this)
|
|
|
|
|
}
|
|
|
|
|
}
|