Files
pqs-9100_client/frontend/src/components/ProTable/index.vue

333 lines
11 KiB
Vue
Raw Normal View History

2024-10-22 14:47:25 +08:00
<template>
2024-08-26 20:05:04 +08:00
<!-- 查询表单 -->
<SearchForm
2024-10-22 14:47:25 +08:00
v-show='isShowSearch'
:search='_search'
:reset='_reset'
:columns='searchColumns'
:search-param='searchParam'
:search-col='searchCol'
2024-08-26 20:05:04 +08:00
/>
<!-- 表格主体 -->
2024-10-22 14:47:25 +08:00
<div class='card table-main'>
2024-08-26 20:05:04 +08:00
<!-- 表格头部 操作按钮 -->
2024-10-22 14:47:25 +08:00
<div class='table-header'>
<div class='header-button-lf'>
<slot name='tableHeader' :selected-list='selectedList' :selected-list-ids='selectedListIds'
:is-selected='isSelected' />
2024-08-26 20:05:04 +08:00
</div>
2024-10-22 14:47:25 +08:00
<div v-if='toolButton' class='header-button-ri'>
<slot name='toolButton'>
<el-button v-if="showToolButton('refresh')" :icon='Refresh' circle @click='getTableList' />
<el-button v-if="showToolButton('setting') && columns.length" :icon='Operation' circle
@click='openColSetting' />
2024-08-26 20:05:04 +08:00
<el-button
v-if="showToolButton('search') && searchColumns?.length"
2024-10-22 14:47:25 +08:00
:icon='Search'
2024-08-26 20:05:04 +08:00
circle
2024-10-22 14:47:25 +08:00
@click='isShowSearch = !isShowSearch'
2024-08-26 20:05:04 +08:00
/>
</slot>
</div>
</div>
<!-- 表格主体 -->
<el-table
2024-10-22 14:47:25 +08:00
ref='tableRef'
v-bind='$attrs'
:id='uuid'
:data='processTableData'
:border='border'
:row-key='rowKey'
@selection-change='selectionChange'
2024-08-26 20:05:04 +08:00
>
<!-- 默认插槽 -->
<slot />
2024-10-22 14:47:25 +08:00
<template v-for='item in tableColumns' :key='item'>
2024-08-26 20:05:04 +08:00
<!-- selection || radio || index || expand || sort -->
<el-table-column
2024-10-22 14:47:25 +08:00
v-if='item.type && columnTypes.includes(item.type)'
v-bind='item'
2024-08-26 20:05:04 +08:00
:align="item.align ?? 'center'"
:reserve-selection="item.type == 'selection'"
>
2024-10-22 14:47:25 +08:00
<template #default='scope'>
2024-08-26 20:05:04 +08:00
<!-- expand -->
<template v-if="item.type == 'expand'">
2024-10-22 14:47:25 +08:00
<component :is='item.render' v-bind='scope' v-if='item.render' />
<slot v-else :name='item.type' v-bind='scope' />
2024-08-26 20:05:04 +08:00
</template>
<!-- radio -->
2024-10-22 14:47:25 +08:00
<el-radio v-if="item.type == 'radio'" v-model='radio' :label='scope.row[rowKey]'>
2024-08-26 20:05:04 +08:00
<i></i>
</el-radio>
<!-- sort -->
2024-10-22 14:47:25 +08:00
<el-tag v-if="item.type == 'sort'" class='move'>
<el-icon>
<DCaret />
</el-icon>
2024-08-26 20:05:04 +08:00
</el-tag>
</template>
</el-table-column>
<!-- other -->
2024-10-22 14:47:25 +08:00
<TableColumn v-else :column='item'>
<template v-for='slot in Object.keys($slots)' #[slot]='scope'>
<slot :name='slot' v-bind='scope' />
2024-08-26 20:05:04 +08:00
</template>
</TableColumn>
</template>
<!-- 插入表格最后一行之后的插槽 -->
<template #append>
2024-10-22 14:47:25 +08:00
<slot name='append' />
2024-08-26 20:05:04 +08:00
</template>
<!-- 无数据 -->
<template #empty>
2024-10-22 14:47:25 +08:00
<div class='table-empty'>
<slot name='empty'>
<img src='@/assets/images/notData.png' alt='notData' />
2024-08-26 20:05:04 +08:00
<div>暂无数据</div>
</slot>
</div>
</template>
</el-table>
<!-- 分页组件 -->
2024-10-22 14:47:25 +08:00
<slot name='pagination'>
2024-08-26 20:05:04 +08:00
<Pagination
2024-10-22 14:47:25 +08:00
v-if='pagination'
:pageable='pageable'
:handle-size-change='handleSizeChange'
:handle-current-change='handleCurrentChange'
2024-08-26 20:05:04 +08:00
/>
</slot>
</div>
<!-- 列设置 -->
2024-10-22 14:47:25 +08:00
<ColSetting v-if='toolButton' ref='colRef' v-model:col-setting='colSetting' />
2024-08-26 20:05:04 +08:00
</template>
2024-10-22 14:47:25 +08:00
<script setup lang='ts' name='ProTable'>
import { ElTable } from 'element-plus'
import { useTable } from '@/hooks/useTable'
import { useSelection } from '@/hooks/useSelection'
import { BreakPoint } from '@/components/Grid/interface'
import { ColumnProps, TypeProps } from '@/components/ProTable/interface'
import { Refresh, Operation, Search } from '@element-plus/icons-vue'
import { generateUUID, handleProp } from '@/utils'
import SearchForm from '@/components/SearchForm/index.vue'
import Pagination from './components/Pagination.vue'
import ColSetting from './components/ColSetting.vue'
import TableColumn from './components/TableColumn.vue'
import Sortable from 'sortablejs'
2024-08-26 20:05:04 +08:00
export interface ProTableProps {
columns: ColumnProps[]; // 列配置项 ==> 必传
data?: any[]; // 静态 table data 数据,若存在则不会使用 requestApi 返回的 data ==> 非必传
requestApi?: (params: any) => Promise<any>; // 请求表格数据的 api ==> 非必传
requestAuto?: boolean; // 是否自动执行请求 api ==> 非必传默认为true
requestError?: (params: any) => void; // 表格 api 请求错误监听 ==> 非必传
dataCallback?: (data: any) => any; // 返回数据的回调函数,可以对数据进行处理 ==> 非必传
title?: string; // 表格标题 ==> 非必传
pagination?: boolean; // 是否需要分页组件 ==> 非必传默认为true
initParam?: any; // 初始化请求参数 ==> 非必传(默认为{}
border?: boolean; // 是否带有纵向边框 ==> 非必传默认为true
2024-10-22 14:47:25 +08:00
toolButton?: ('refresh' | 'setting' | 'search')[] | boolean; // 是否显示表格功能按钮 ==> 非必传默认为true
2024-08-26 20:05:04 +08:00
rowKey?: string; // 行数据的 Key用来优化 Table 的渲染,当表格数据多选时,所指定的 id ==> 非必传(默认为 id
searchCol?: number | Record<BreakPoint, number>; // 表格搜索项 每列占比配置 ==> 非必传 { xs: 1, sm: 2, md: 2, lg: 3, xl: 4 }
}
// 接受父组件参数,配置默认值
const props = withDefaults(defineProps<ProTableProps>(), {
columns: () => [],
requestAuto: true,
pagination: true,
initParam: {},
border: true,
toolButton: true,
2024-10-22 14:47:25 +08:00
rowKey: 'id',
searchCol: () => ({ xs: 1, sm: 2, md: 2, lg: 3, xl: 4 }),
})
2024-08-26 20:05:04 +08:00
// table 实例
2024-10-22 14:47:25 +08:00
const tableRef = ref<InstanceType<typeof ElTable>>()
2024-08-26 20:05:04 +08:00
// 生成组件唯一id
2024-10-22 14:47:25 +08:00
const uuid = ref('id-' + generateUUID())
2024-08-26 20:05:04 +08:00
// column 列类型
2024-10-22 14:47:25 +08:00
const columnTypes: TypeProps[] = ['selection', 'radio', 'index', 'expand', 'sort']
2024-08-26 20:05:04 +08:00
// 是否显示搜索模块
2024-10-22 14:47:25 +08:00
const isShowSearch = ref(true)
2024-08-26 20:05:04 +08:00
// 控制 ToolButton 显示
2024-10-22 14:47:25 +08:00
const showToolButton = (key: 'refresh' | 'setting' | 'search') => {
return Array.isArray(props.toolButton) ? props.toolButton.includes(key) : props.toolButton
}
2024-08-26 20:05:04 +08:00
// 单选值
2024-10-22 14:47:25 +08:00
const radio = ref('')
2024-08-26 20:05:04 +08:00
// 表格多选 Hooks
2024-10-22 14:47:25 +08:00
const { selectionChange, selectedList, selectedListIds, isSelected } = useSelection(props.rowKey)
2024-08-26 20:05:04 +08:00
// 表格操作 Hooks
2024-10-22 14:47:25 +08:00
const {
tableData,
pageable,
searchParam,
searchInitParam,
getTableList,
search,
reset,
handleSizeChange,
handleCurrentChange,
} =
useTable(props.requestApi, props.initParam, props.pagination, props.dataCallback, props.requestError)
2024-08-26 20:05:04 +08:00
// 清空选中数据列表
2024-10-22 14:47:25 +08:00
const clearSelection = () => tableRef.value!.clearSelection()
2024-08-26 20:05:04 +08:00
// 初始化表格数据 && 拖拽排序
onMounted(() => {
2024-10-22 14:47:25 +08:00
dragSort()
props.requestAuto && getTableList()
props.data && (pageable.value.total = props.data.length)
})
2024-08-26 20:05:04 +08:00
// 处理表格数据
const processTableData = computed(() => {
2024-10-22 14:47:25 +08:00
if (!props.data) return tableData.value
if (!props.pagination) return props.data
2024-08-26 20:05:04 +08:00
return props.data.slice(
(pageable.value.pageNum - 1) * pageable.value.pageSize,
2024-10-22 14:47:25 +08:00
pageable.value.pageSize * pageable.value.pageNum,
)
})
2024-08-26 20:05:04 +08:00
// 监听页面 initParam 改化,重新获取表格数据
2024-10-22 14:47:25 +08:00
watch(() => props.initParam, getTableList, { deep: true })
2024-08-26 20:05:04 +08:00
// 接收 columns 并设置为响应式
2024-10-22 14:47:25 +08:00
const tableColumns = reactive<ColumnProps[]>(props.columns)
2024-08-26 20:05:04 +08:00
// 扁平化 columns
2024-10-22 14:47:25 +08:00
const flatColumns = computed(() => flatColumnsFunc(tableColumns))
2024-08-26 20:05:04 +08:00
// 定义 enumMap 存储 enum 值(避免异步请求无法格式化单元格内容 || 无法填充搜索下拉选择)
2024-10-22 14:47:25 +08:00
const enumMap = ref(new Map<string, { [key: string]: any }[]>())
2024-08-26 20:05:04 +08:00
const setEnumMap = async ({ prop, enum: enumValue }: ColumnProps) => {
2024-10-22 14:47:25 +08:00
if (!enumValue) return
2024-08-26 20:05:04 +08:00
// 如果当前 enumMap 存在相同的值 return
2024-10-22 14:47:25 +08:00
if (enumMap.value.has(prop!) && (typeof enumValue === 'function' || enumMap.value.get(prop!) === enumValue)) return
2024-08-26 20:05:04 +08:00
// 当前 enum 为静态数据,则直接存储到 enumMap
2024-10-22 14:47:25 +08:00
if (typeof enumValue !== 'function') return enumMap.value.set(prop!, unref(enumValue!))
2024-08-26 20:05:04 +08:00
// 为了防止接口执行慢,而存储慢,导致重复请求,所以预先存储为[],接口返回后再二次存储
2024-10-22 14:47:25 +08:00
enumMap.value.set(prop!, [])
2024-08-26 20:05:04 +08:00
// 当前 enum 为后台数据需要请求数据,则调用该请求接口,并存储到 enumMap
2024-10-22 14:47:25 +08:00
const { data } = await enumValue()
enumMap.value.set(prop!, data)
}
2024-08-26 20:05:04 +08:00
// 注入 enumMap
2024-10-22 14:47:25 +08:00
provide('enumMap', enumMap)
2024-08-26 20:05:04 +08:00
// 扁平化 columns 的方法
2024-10-11 14:46:17 +08:00
const flatColumnsFunc = (columns: ColumnProps[], flatArr: ColumnProps[] = []) => {
columns.forEach(async col => {
2024-10-22 14:47:25 +08:00
if (col._children?.length) flatArr.push(...flatColumnsFunc(col._children))
flatArr.push(col)
2024-08-26 20:05:04 +08:00
// column 添加默认 isShow && isSetting && isFilterEnum 属性值
2024-10-22 14:47:25 +08:00
col.isShow = col.isShow ?? true
col.isSetting = col.isSetting ?? true
col.isFilterEnum = col.isFilterEnum ?? true
2024-08-26 20:05:04 +08:00
// 设置 enumMap
2024-10-22 14:47:25 +08:00
await setEnumMap(col)
})
return flatArr.filter(item => !item._children?.length)
}
2024-08-26 20:05:04 +08:00
// 过滤需要搜索的配置项 && 排序
const searchColumns = computed(() => {
return flatColumns.value
2024-10-11 14:46:17 +08:00
?.filter(item => item.search?.el || item.search?.render)
2024-10-22 14:47:25 +08:00
.sort((a, b) => a.search!.order! - b.search!.order!)
})
2024-08-26 20:05:04 +08:00
// 设置 搜索表单默认排序 && 搜索表单项的默认值
searchColumns.value?.forEach((column, index) => {
2024-10-22 14:47:25 +08:00
column.search!.order = column.search?.order ?? index + 2
const key = column.search?.key ?? handleProp(column.prop!)
const defaultValue = column.search?.defaultValue
2024-08-26 20:05:04 +08:00
if (defaultValue !== undefined && defaultValue !== null) {
2024-10-22 14:47:25 +08:00
searchParam.value[key] = defaultValue
searchInitParam.value[key] = defaultValue
2024-08-26 20:05:04 +08:00
}
2024-10-22 14:47:25 +08:00
})
2024-08-26 20:05:04 +08:00
// 列设置 ==> 需要过滤掉不需要设置的列
2024-10-22 14:47:25 +08:00
const colRef = ref()
2024-10-11 14:46:17 +08:00
const colSetting = tableColumns!.filter(item => {
2024-10-22 14:47:25 +08:00
const { type, prop, isSetting } = item
return !columnTypes.includes(type!) && prop !== 'operation' && isSetting
})
const openColSetting = () => colRef.value.openColSetting()
2024-08-26 20:05:04 +08:00
// 定义 emit 事件
const emit = defineEmits<{
search: [];
reset: [];
dragSort: [{ newIndex?: number; oldIndex?: number }];
2024-10-22 14:47:25 +08:00
}>()
2024-08-26 20:05:04 +08:00
const _search = () => {
2024-10-22 14:47:25 +08:00
search()
emit('search')
}
2024-08-26 20:05:04 +08:00
const _reset = () => {
2024-10-22 14:47:25 +08:00
reset()
emit('reset')
}
2024-08-26 20:05:04 +08:00
// 表格拖拽排序
const dragSort = () => {
2024-10-22 14:47:25 +08:00
const tbody = document.querySelector(`#${uuid.value} tbody`) as HTMLElement
2024-08-26 20:05:04 +08:00
Sortable.create(tbody, {
2024-10-22 14:47:25 +08:00
handle: '.move',
2024-08-26 20:05:04 +08:00
animation: 300,
onEnd({ newIndex, oldIndex }) {
2024-10-22 14:47:25 +08:00
const [removedItem] = processTableData.value.splice(oldIndex!, 1)
processTableData.value.splice(newIndex!, 0, removedItem)
emit('dragSort', { newIndex, oldIndex })
},
})
}
2024-08-26 20:05:04 +08:00
// 暴露给父组件的参数和方法 (外部需要什么,都可以从这里暴露出去)
defineExpose({
element: tableRef,
tableData: processTableData,
radio,
pageable,
searchParam,
searchInitParam,
isSelected,
selectedList,
selectedListIds,
// 下面为 function
getTableList,
search,
reset,
handleSizeChange,
handleCurrentChange,
clearSelection,
2024-10-22 14:47:25 +08:00
enumMap,
})
2024-08-26 20:05:04 +08:00
</script>