123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <template>
-
- <div class="card table-main">
-
- <div class="table-header">
- <div class="header-button-lf">
- <slot name="tableHeader" :selected-list="selectedList" :selected-list-ids="selectedListIds" :is-selected="isSelected" />
- </div>
- <div v-if="toolButton" class="header-button-ri">
- <slot name="toolButton">
- <img
- class="setting"
- src="@/assets/images/setting.png"
- v-if="showToolButton('setting') && columns.length"
- @click="openColSetting"
- />
- </slot>
- </div>
- </div>
-
- <el-table
- stripe
- ref="tableRef"
- v-bind="$attrs"
- :id="uuid"
- :data="processTableData"
- :border="border"
- :row-key="rowKey"
- @selection-change="selectionChange"
- >
-
- <slot />
- <template v-for="item in tableColumns" :key="item">
-
- <el-table-column
- v-if="item.type && columnTypes.includes(item.type)"
- v-bind="item"
- :align="item.align ?? 'left'"
- :reserve-selection="item.type == 'selection'"
- >
- <template #default="scope">
-
- <template v-if="item.type == 'expand'">
- <component :is="item.render" v-bind="scope" v-if="item.render" />
- <slot v-else :name="item.type" v-bind="scope" />
- </template>
-
- <el-radio v-if="item.type == 'radio'" v-model="radio" :label="scope.row[rowKey]">
- <i></i>
- </el-radio>
-
- <el-tag v-if="item.type == 'sort'" class="move">
- <el-icon> <DCaret /></el-icon>
- </el-tag>
- </template>
- </el-table-column>
-
- <TableColumn v-else :column="item">
- <template v-for="slot in Object.keys($slots)" #[slot]="scope">
- <slot :name="slot" v-bind="scope" />
- </template>
- </TableColumn>
- </template>
-
- <template #append>
- <slot name="append" />
- </template>
-
- <template #empty>
- <div class="table-empty">
- <slot name="empty">
- <img src="@/assets/images/notData.png" alt="notData" />
- <div>暂无数据</div>
- </slot>
- </div>
- </template>
- </el-table>
-
- <slot name="pagination">
- <Pagination
- v-if="pagination"
- :pageable="pageable"
- :handle-size-change="handleSizeChange"
- :handle-current-change="handleCurrentChange"
- />
- </slot>
- </div>
-
- <ColSetting v-if="toolButton" ref="colRef" v-model:col-setting="colSetting" />
- </template>
- <script setup lang="ts" name="ProTable">
- import { ref, watch, provide, onMounted, unref, computed, reactive } from "vue";
- 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 { generateUUID, handleProp } from "@/utils";
- import Pagination from "./components/Pagination.vue";
- import ColSetting from "./components/ColSetting.vue";
- import TableColumn from "./components/TableColumn.vue";
- import Sortable from "sortablejs";
- export interface ProTableProps {
- columns: ColumnProps[];
- data?: any[];
- requestApi?: (params: any) => Promise<any>;
- requestAuto?: boolean;
- requestError?: (params: any) => void;
- dataCallback?: (data: any) => any;
- title?: string;
- pagination?: boolean;
- initParam?: any;
- border?: boolean;
- toolButton?: "setting"[] | boolean;
- rowKey?: string;
- searchCol?: number | Record<BreakPoint, number>;
- }
- const props = withDefaults(defineProps<ProTableProps>(), {
- columns: () => [],
- requestAuto: true,
- pagination: true,
- initParam: {},
- border: true,
- toolButton: true,
- rowKey: "id",
- searchCol: () => ({ xs: 1, sm: 2, md: 2, lg: 3, xl: 4 })
- });
- const tableRef = ref<InstanceType<typeof ElTable>>();
- const uuid = ref("id-" + generateUUID());
- const columnTypes: TypeProps[] = ["selection", "radio", "index", "expand", "sort"];
- const showToolButton = (key: "refresh" | "setting" | "search") => {
- return Array.isArray(props.toolButton) ? props.toolButton.includes(key) : props.toolButton;
- };
- const radio = ref("");
- const { selectionChange, selectedList, selectedListIds, isSelected } = useSelection(props.rowKey);
- const { tableData, pageable, searchParam, searchInitParam, getTableList, search, reset, handleSizeChange, handleCurrentChange } =
- useTable(props.requestApi, props.initParam, props.pagination, props.dataCallback, props.requestError);
- const clearSelection = () => tableRef.value!.clearSelection();
- onMounted(() => {
- dragSort();
- props.requestAuto && getTableList();
- props.data && (pageable.value.total = props.data.length);
- });
- const processTableData = computed(() => {
- if (!props.data) return tableData.value;
- if (!props.pagination) return props.data;
- return props.data.slice(
- (pageable.value.pageNum - 1) * pageable.value.pageSize,
- pageable.value.pageSize * pageable.value.pageNum
- );
- });
- watch(() => props.initParam, getTableList, { deep: true });
- const tableColumns = reactive<ColumnProps[]>(props.columns);
- const flatColumns = computed(() => flatColumnsFunc(tableColumns));
- const enumMap = ref(new Map<string, { [key: string]: any }[]>());
- const setEnumMap = async ({ prop, enum: enumValue }: ColumnProps) => {
- if (!enumValue) return;
-
- if (enumMap.value.has(prop!) && (typeof enumValue === "function" || enumMap.value.get(prop!) === enumValue)) return;
-
- if (typeof enumValue !== "function") return enumMap.value.set(prop!, unref(enumValue!));
-
- enumMap.value.set(prop!, []);
-
- const { data } = await enumValue();
- enumMap.value.set(prop!, data);
- };
- provide("enumMap", enumMap);
- const flatColumnsFunc = (columns: ColumnProps[], flatArr: ColumnProps[] = []) => {
- columns.forEach(async col => {
- if (col._children?.length) flatArr.push(...flatColumnsFunc(col._children));
- flatArr.push(col);
-
- col.isShow = col.isShow ?? true;
- col.isSetting = col.isSetting ?? true;
- col.isFilterEnum = col.isFilterEnum ?? true;
-
- await setEnumMap(col);
- });
- return flatArr.filter(item => !item._children?.length);
- };
- const searchColumns = computed(() => {
- return flatColumns.value
- ?.filter(item => item.search?.el || item.search?.render)
- .sort((a, b) => a.search!.order! - b.search!.order!);
- });
- searchColumns.value?.forEach((column, index) => {
- column.search!.order = column.search?.order ?? index + 2;
- const key = column.search?.key ?? handleProp(column.prop!);
- const defaultValue = column.search?.defaultValue;
- if (defaultValue !== undefined && defaultValue !== null) {
- searchParam.value[key] = defaultValue;
- searchInitParam.value[key] = defaultValue;
- }
- });
- const colRef = ref();
- const colSetting = tableColumns!.filter(item => {
- const { type, prop, isSetting } = item;
- return !columnTypes.includes(type!) && prop !== "operation" && isSetting;
- });
- const openColSetting = () => colRef.value.openColSetting();
- const emit = defineEmits<{
- search: [];
- reset: [];
- dragSort: [{ newIndex?: number; oldIndex?: number }];
- }>();
- const dragSort = () => {
- const tbody = document.querySelector(`#${uuid.value} tbody`) as HTMLElement;
- Sortable.create(tbody, {
- handle: ".move",
- animation: 300,
- onEnd({ newIndex, oldIndex }) {
- const [removedItem] = processTableData.value.splice(oldIndex!, 1);
- processTableData.value.splice(newIndex!, 0, removedItem);
- emit("dragSort", { newIndex, oldIndex });
- }
- });
- };
- defineExpose({
- element: tableRef,
- tableData: processTableData,
- radio,
- pageable,
- searchParam,
- searchInitParam,
- isSelected,
- selectedList,
- selectedListIds,
-
- getTableList,
- search,
- reset,
- handleSizeChange,
- handleCurrentChange,
- clearSelection,
- enumMap
- });
- </script>
- <style>
- .setting {
- height: 20px;
- width: 20px;
- margin-top: 10px;
- margin-right: 5px;
- cursor: pointer;
- }
- </style>
|