|
|
@@ -62,9 +62,9 @@
|
|
|
<el-radio v-if="item.type == 'radio'" v-model="radio" :label="scope.row[rowKey]">
|
|
|
<i />
|
|
|
</el-radio>
|
|
|
- <!-- sort -->
|
|
|
- <el-tag v-if="item.type == 'sort' && item.condition && item.condition(scope, index)" class="move">
|
|
|
- <el-icon> <DCaret /></el-icon>
|
|
|
+ <!-- sort:未配置 condition 时默认显示拖拽手柄 -->
|
|
|
+ <el-tag v-if="item.type == 'sort' && showSortHandle(item, scope, index)" class="move sort-drag-handle">
|
|
|
+ <el-icon><Rank /></el-icon>
|
|
|
</el-tag>
|
|
|
</template>
|
|
|
</el-table-column>
|
|
|
@@ -104,13 +104,13 @@
|
|
|
</template>
|
|
|
|
|
|
<script setup lang="ts" name="ProTable">
|
|
|
-import { ref, watch, provide, onMounted, unref, computed, reactive } from "vue";
|
|
|
+import { ref, watch, provide, onMounted, unref, computed, reactive, nextTick } 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 { Refresh, Operation, Search } from "@element-plus/icons-vue";
|
|
|
+import { Refresh, Operation, Search, Rank } from "@element-plus/icons-vue";
|
|
|
import { generateUUID, handleProp } from "@/utils";
|
|
|
import SearchForm from "@/components/SearchForm/index.vue";
|
|
|
import Pagination from "./components/Pagination.vue";
|
|
|
@@ -157,6 +157,12 @@ const uuid = ref("id-" + generateUUID());
|
|
|
// column 列类型
|
|
|
const columnTypes: TypeProps[] = ["selection", "radio", "index", "expand", "sort"];
|
|
|
|
|
|
+function showSortHandle(item: ColumnProps, scope: any, index: number) {
|
|
|
+ const fn = item.condition as ((a?: any, b?: number) => boolean) | undefined;
|
|
|
+ if (fn == null) return true;
|
|
|
+ return fn(scope, index);
|
|
|
+}
|
|
|
+
|
|
|
// 是否显示搜索模块
|
|
|
const isShowSearch = ref(true);
|
|
|
|
|
|
@@ -178,12 +184,7 @@ const { tableData, pageable, searchParam, searchInitParam, getTableList, search,
|
|
|
// 清空选中数据列表
|
|
|
const clearSelection = () => tableRef.value!.clearSelection();
|
|
|
|
|
|
-// 初始化表格数据 && 拖拽排序
|
|
|
-onMounted(() => {
|
|
|
- dragSort();
|
|
|
- props.requestAuto && getTableList();
|
|
|
- props.data && (pageable.value.total = props.data.length);
|
|
|
-});
|
|
|
+let sortableInstance: Sortable | null = null;
|
|
|
|
|
|
// 处理表格数据
|
|
|
const processTableData = computed(() => {
|
|
|
@@ -297,20 +298,37 @@ const handleRefresh = () => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-// 表格拖拽排序
|
|
|
-const dragSort = () => {
|
|
|
- const tbody = document.querySelector(`#${uuid.value} tbody`) as HTMLElement;
|
|
|
- Sortable.create(tbody, {
|
|
|
+// 表格拖拽排序(存在 sort 列时启用;数据更新后需重建实例)
|
|
|
+const dragSortInit = () => {
|
|
|
+ const hasSortCol = tableColumns.some((c: ColumnProps) => c.type === "sort");
|
|
|
+ if (!hasSortCol) return;
|
|
|
+ sortableInstance?.destroy();
|
|
|
+ sortableInstance = null;
|
|
|
+ const tbody = document.querySelector(`#${uuid.value} tbody`) as HTMLElement | null;
|
|
|
+ if (!tbody) return;
|
|
|
+ sortableInstance = Sortable.create(tbody, {
|
|
|
handle: ".move",
|
|
|
animation: 300,
|
|
|
onEnd({ newIndex, oldIndex }) {
|
|
|
- const [removedItem] = processTableData.value.splice(oldIndex!, 1);
|
|
|
- processTableData.value.splice(newIndex!, 0, removedItem);
|
|
|
+ if (oldIndex == null || newIndex == null || oldIndex === newIndex) return;
|
|
|
+ const [removedItem] = processTableData.value.splice(oldIndex, 1);
|
|
|
+ processTableData.value.splice(newIndex, 0, removedItem);
|
|
|
emit("dragSort", { newIndex, oldIndex });
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+onMounted(() => {
|
|
|
+ props.data && (pageable.value.total = props.data.length);
|
|
|
+ if (props.requestAuto) {
|
|
|
+ getTableList().then(() => nextTick(dragSortInit));
|
|
|
+ } else {
|
|
|
+ nextTick(dragSortInit);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+watch(tableData, () => nextTick(dragSortInit));
|
|
|
+
|
|
|
// 暴露给父组件的参数和方法 (外部需要什么,都可以从这里暴露出去)
|
|
|
defineExpose({
|
|
|
element: tableRef,
|