You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
4.7 KiB
TypeScript

import { Table, TableExpose, TableProps, TableSetProps, TableColumn } from '@/components/Table'
import { ElTable } from 'element-plus'
import { ref, watch, unref, nextTick, onMounted } from 'vue'
interface UseTableConfig {
/**
*
*/
immediate?: boolean
fetchDataApi: () => Promise<{
list: any[]
total: number
}>
}
export const useTable = (config: UseTableConfig) => {
const { immediate = true } = config
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dataList = ref<any[]>([])
watch(
() => currentPage.value,
() => {
methods.getList()
}
)
watch(
() => pageSize.value,
() => {
// 当前页不为1时修改页数后会导致多次调用getList方法
if (unref(currentPage) === 1) {
methods.getList()
} else {
currentPage.value = 1
methods.getList()
}
}
)
onMounted(() => {
if (immediate) {
methods.getList()
}
})
// Table实例
const tableRef = ref<typeof Table & TableExpose>()
// ElTable实例
const elTableRef = ref<ComponentRef<typeof ElTable>>()
const register = (ref: typeof Table & TableExpose, elRef: ComponentRef<typeof ElTable>) => {
tableRef.value = ref
elTableRef.value = unref(elRef)
}
const getTable = async () => {
await nextTick()
const table = unref(tableRef)
if (!table) {
console.error('The table is not registered. Please use the register method to register')
}
return table
}
// const delData = async (ids: string[] | number[]) => {
// const res = await (config?.delListApi && config?.delListApi(ids))
// if (res) {
// ElMessage.success(t('common.delSuccess'))
// // 计算出临界点
// const currentPage =
// tableObject.total % tableObject.pageSize === ids.length || tableObject.pageSize === 1
// ? tableObject.currentPage > 1
// ? tableObject.currentPage - 1
// : tableObject.currentPage
// : tableObject.currentPage
// tableObject.currentPage = currentPage
// methods.getList()
// }
// }
const methods = {
/**
*
*/
getList: async () => {
loading.value = true
try {
const res = await config?.fetchDataApi()
console.log('fetchDataApi res', res)
if (res) {
dataList.value = res.list
total.value = res.total
}
} catch (err) {
console.log('fetchDataApi error')
} finally {
loading.value = false
}
},
/**
* @description tableprops
* @param props tableprops
*/
setProps: async (props: TableProps = {}) => {
const table = await getTable()
table?.setProps(props)
},
/**
* @description column
* @param columnProps
*/
setColumn: async (columnProps: TableSetProps[]) => {
const table = await getTable()
table?.setColumn(columnProps)
},
/**
* @description column
* @param tableColumn
* @param index
*/
addColumn: async (tableColumn: TableColumn, index?: number) => {
const table = await getTable()
table?.addColumn(tableColumn, index)
},
/**
* @description column
* @param field
*/
delColumn: async (field: string) => {
const table = await getTable()
table?.delColumn(field)
},
/**
* @description ElTable
* @returns ElTable instance
*/
getElTableExpose: async () => {
await getTable()
return unref(elTableRef)
}
// // 删除数据
// delList: async (ids: string[] | number[], multiple: boolean, message = true) => {
// const tableRef = await getTable()
// if (multiple) {
// if (!tableRef?.selections.length) {
// ElMessage.warning(t('common.delNoData'))
// return
// }
// } else {
// if (!tableObject.currentRow) {
// ElMessage.warning(t('common.delNoData'))
// return
// }
// }
// if (message) {
// ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
// confirmButtonText: t('common.delOk'),
// cancelButtonText: t('common.delCancel'),
// type: 'warning'
// }).then(async () => {
// await delData(ids)
// })
// } else {
// await delData(ids)
// }
// }
}
return {
tableRegister: register,
tableMethods: methods,
tableState: {
currentPage,
pageSize,
total,
dataList,
loading
}
}
}