2023-01-03 10:10:51 +08:00
|
|
|
import { ref, unref } from 'vue'
|
|
|
|
import { XTableProps } from '@/components/XTable/src/type'
|
|
|
|
|
|
|
|
export interface tableMethod {
|
|
|
|
reload: () => void
|
|
|
|
setProps: (props: XTableProps) => void
|
2023-01-03 10:40:53 +08:00
|
|
|
deleteData: (ids: string | number) => void
|
|
|
|
exportList: (fileName?: string) => void
|
2023-01-03 10:10:51 +08:00
|
|
|
}
|
|
|
|
|
2023-01-03 14:23:48 +08:00
|
|
|
export const useXTable = (props: XTableProps): [Function, tableMethod] => {
|
2023-01-03 10:10:51 +08:00
|
|
|
const tableRef = ref<Nullable<tableMethod>>(null)
|
|
|
|
|
2023-01-03 14:23:48 +08:00
|
|
|
const register = (instance) => {
|
2023-01-03 10:10:51 +08:00
|
|
|
tableRef.value = instance
|
|
|
|
props && instance.setProps(props)
|
|
|
|
}
|
2023-01-03 14:23:48 +08:00
|
|
|
const getInstance = (): tableMethod => {
|
2023-01-03 10:10:51 +08:00
|
|
|
const table = unref(tableRef)
|
|
|
|
if (!table) {
|
|
|
|
console.error('表格实例不存在')
|
|
|
|
}
|
|
|
|
return table as tableMethod
|
|
|
|
}
|
|
|
|
const methods: tableMethod = {
|
|
|
|
reload: () => getInstance().reload(),
|
2023-01-03 10:40:53 +08:00
|
|
|
setProps: (props) => getInstance().setProps(props),
|
|
|
|
deleteData: (ids: string | number) => getInstance().deleteData(ids),
|
|
|
|
exportList: (fileName?: string) => getInstance().exportList(fileName)
|
2023-01-03 10:10:51 +08:00
|
|
|
}
|
|
|
|
return [register, methods]
|
|
|
|
}
|