123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import { Table, TableExpose } from '@/components/Table'
- import { ElTable, ElMessageBox, ElMessage } from 'element-plus'
- import { ref, reactive, watch, computed, unref, nextTick } from 'vue'
- import { AxiosPromise } from 'axios'
- import { get } from 'lodash-es'
- import type { TableProps } from '@/components/Table/src/types'
- import { useI18n } from '@/hooks/web/useI18n'
- const { t } = useI18n()
- interface UseTableConfig<T, L> {
- getListApi: (option: L) => AxiosPromise<T>
- delListApi?: (option: AxiosConfig) => AxiosPromise<unknown>
- // 返回数据格式配置
- response: {
- list: string
- total?: string
- }
- props?: TableProps
- }
- interface TableObject<K, L> {
- pageSize: number
- currentPage: number
- total: number
- tableList: K[]
- parmasObj: L
- loading: boolean
- currentRow: Nullable<K>
- }
- export const useTable = <T, K, L extends AxiosConfig = AxiosConfig>(
- config?: UseTableConfig<T, L>
- ) => {
- const tableObject = reactive<TableObject<K, L>>({
- // 页数
- pageSize: 10,
- // 当前页
- currentPage: 1,
- // 总条数
- total: 10,
- // 表格数据
- tableList: [],
- // AxiosConfig 配置
- parmasObj: {} as L,
- // 加载中
- loading: true,
- // 当前行的数据
- currentRow: null
- })
- const parmasObj = computed(() => {
- return {
- params: {
- ...tableObject.parmasObj.params,
- pageSize: tableObject.pageSize,
- pageIndex: tableObject.currentPage
- }
- }
- })
- watch(
- () => tableObject.currentPage,
- () => {
- methods.getList()
- }
- )
- watch(
- () => tableObject.pageSize,
- () => {
- tableObject.currentPage = 1
- 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 = 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 (paramsObj: AxiosConfig, ids: string[] | number[]) => {
- const res = await (config?.delListApi && config?.delListApi(paramsObj))
- 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: {
- setProps: (props: Recordable) => void
- getList: () => Promise<void>
- setColumn: (columnProps: TableSetPropsType[]) => void
- setSearchParmas: (data: Recordable) => void
- getSelections: () => Promise<K[]>
- delList: (ids: string[] | number[], multiple: boolean, message?: boolean) => Promise<void>
- } = {
- getList: async () => {
- tableObject.loading = true
- const res = await config
- ?.getListApi(unref(parmasObj) as unknown as L)
- .catch(() => {})
- .finally(() => {
- tableObject.loading = false
- })
- if (res) {
- tableObject.tableList = get(res.data || {}, config?.response.list as string)
- tableObject.total = get(res.data || {}, config?.response?.total as string) || 0
- }
- },
- setProps: async (props: TableProps = {}) => {
- const table = await getTable()
- table?.setProps(props)
- },
- setColumn: async (columnProps: TableSetPropsType[]) => {
- const table = await getTable()
- table?.setColumn(columnProps)
- },
- getSelections: async () => {
- const table = await getTable()
- return (table?.selections || []) as K[]
- },
- // 与Search组件结合
- setSearchParmas: (data: Recordable) => {
- tableObject.currentPage = 1
- tableObject.parmasObj = Object.assign(tableObject.parmasObj, {
- params: {
- pageSize: tableObject.pageSize,
- pageIndex: tableObject.currentPage,
- ...data
- }
- })
- methods.getList()
- },
- // 删除数据
- 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
- }
- }
- const paramsObj: AxiosConfig = {
- data: {
- ids
- }
- }
- if (message) {
- ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
- confirmButtonText: t('common.delOk'),
- cancelButtonText: t('common.delCancel'),
- type: 'warning'
- })
- .then(async () => {
- await delData(paramsObj, ids)
- })
- .catch(() => {})
- } else {
- await delData(paramsObj, ids)
- }
- }
- }
- config?.props && methods.setProps(config.props)
- return {
- register,
- elTableRef,
- tableObject,
- methods
- }
- }
|