feat(报表定义): 字段支持排序(拖拽+上移下移按钮),顺序即导出表格列顺序

This commit is contained in:
liufumingren 2026-09-01 00:41:05 +08:00
parent de80a1d1f2
commit 4b2d7b195c
3 changed files with 216 additions and 18 deletions

26
package-lock.json generated
View File

@ -24,6 +24,7 @@
"path-to-regexp": "^8.3.0",
"pinia": "^3.0.4",
"qrcode": "^1.5.4",
"sortablejs": "^1.15.7",
"spark-md5": "^3.0.2",
"three": "^0.183.2",
"uuid": "^13.0.0",
@ -41,6 +42,7 @@
"@types/node": "^24.11.0",
"@types/path-browserify": "^1.0.3",
"@types/qrcode": "^1.5.6",
"@types/sortablejs": "^1.15.9",
"@types/spark-md5": "^3.0.5",
"@types/three": "^0.183.1",
"@vitejs/plugin-vue": "^6.0.4",
@ -95,7 +97,6 @@
"integrity": "sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@babel/code-frame": "^7.29.0",
"@babel/generator": "^7.29.0",
@ -2066,7 +2067,6 @@
"resolved": "https://registry.npmjs.org/@types/lodash-es/-/lodash-es-4.17.12.tgz",
"integrity": "sha512-0NgftHUcV4v34VhXm8QBSftKVXtbkBG3ViCjs6+eJ5a6y6Mi/jiFGPc1sC7QK+9BFhWrURE3EOggmWaSxL9OzQ==",
"license": "MIT",
"peer": true,
"dependencies": {
"@types/lodash": "*"
}
@ -2208,7 +2208,6 @@
"integrity": "sha512-/Zb/xaIDfxeJnvishjGdcR4jmr7S+bda8PKNhRGdljDM+elXhlvN0FyPSsMnLmJUrVG9aPO6dof80wjMawsASg==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@typescript-eslint/scope-manager": "8.58.2",
"@typescript-eslint/types": "8.58.2",
@ -2792,7 +2791,6 @@
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.16.0.tgz",
"integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==",
"license": "MIT",
"peer": true,
"bin": {
"acorn": "bin/acorn"
},
@ -3190,7 +3188,6 @@
}
],
"license": "MIT",
"peer": true,
"dependencies": {
"baseline-browser-mapping": "^2.9.0",
"caniuse-lite": "^1.0.30001759",
@ -4493,7 +4490,6 @@
"integrity": "sha512-+L0vBFYGIpSNIt/KWTpFonPrqYvgKw1eUI5Vn7mEogrQcWtWYtNQ7dNqC+px/J0idT3BAkiWrhfS7k+Tum8TUA==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@eslint-community/eslint-utils": "^4.8.0",
"@eslint-community/regexpp": "^4.12.2",
@ -6347,15 +6343,13 @@
"version": "4.17.23",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.23.tgz",
"integrity": "sha512-LgVTMpQtIopCi79SJeDiP0TfWi5CNEc/L/aRdTh3yIvmZXTnheWpKjSZhnvMl8iXbC1tFg9gdHHDMLoV7CnG+w==",
"license": "MIT",
"peer": true
"license": "MIT"
},
"node_modules/lodash-es": {
"version": "4.17.23",
"resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.23.tgz",
"integrity": "sha512-kVI48u3PZr38HdYz98UmfPnXl2DXrpdctLrFLCd3kOx1xUkOmpFPx7gCWWM5MPkL/fD8zb+Ph0QzjGFs4+hHWg==",
"license": "MIT",
"peer": true
"license": "MIT"
},
"node_modules/lodash-unified": {
"version": "1.0.3",
@ -7182,7 +7176,6 @@
"resolved": "https://registry.npmjs.org/pinia/-/pinia-3.0.4.tgz",
"integrity": "sha512-l7pqLUFTI/+ESXn6k3nu30ZIzW5E2WZF/LaHJEpoq6ElcLD+wduZoB2kBN19du6K/4FDpPMazY2wJr+IndBtQw==",
"license": "MIT",
"peer": true,
"dependencies": {
"@vue/devtools-api": "^7.7.7"
},
@ -7258,7 +7251,6 @@
}
],
"license": "MIT",
"peer": true,
"dependencies": {
"nanoid": "^3.3.11",
"picocolors": "^1.1.1",
@ -8229,6 +8221,12 @@
"node": ">=0.10.0"
}
},
"node_modules/sortablejs": {
"version": "1.15.7",
"resolved": "https://registry.npmjs.org/sortablejs/-/sortablejs-1.15.7.tgz",
"integrity": "sha512-Kk8wLQPlS+yi1ZEf48a4+fzHa4yxjC30M/Sr2AnQu+f/MPwvvX9XjZ6OWejiz8crBsLwSq8GHqaxaET7u6ux0A==",
"license": "MIT"
},
"node_modules/source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
@ -9031,7 +9029,6 @@
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"devOptional": true,
"license": "Apache-2.0",
"peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
@ -9309,7 +9306,6 @@
"integrity": "sha512-w+N7Hifpc3gRjZ63vYBXA56dvvRlNWRczTdmCBBa+CotUzAPf5b7YMdMR/8CQoeYE5LX3W4wj6RYTgonm1b9DA==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"esbuild": "^0.27.0",
"fdir": "^6.5.0",
@ -9556,7 +9552,6 @@
"resolved": "https://registry.npmjs.org/vue/-/vue-3.5.29.tgz",
"integrity": "sha512-BZqN4Ze6mDQVNAni0IHeMJ5mwr8VAJ3MQC9FmprRhcBYENw+wOAAjRj8jfmN6FLl0j96OXbR+CjWhmAmM+QGnA==",
"license": "MIT",
"peer": true,
"dependencies": {
"@vue/compiler-dom": "3.5.29",
"@vue/compiler-sfc": "3.5.29",
@ -9596,7 +9591,6 @@
"integrity": "sha512-Vxi9pJdbN3ZnVGLODVtZ7y4Y2kzAAE2Cm0CZ3ZDRvydVYxZ6VrnBhLikBsRS+dpwj4Jv4UCv21PTEwF5rQ9WXg==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"debug": "^4.4.0",
"eslint-scope": "^8.2.0 || ^9.0.0",

View File

@ -44,6 +44,7 @@
"path-to-regexp": "^8.3.0",
"pinia": "^3.0.4",
"qrcode": "^1.5.4",
"sortablejs": "^1.15.7",
"spark-md5": "^3.0.2",
"three": "^0.183.2",
"uuid": "^13.0.0",
@ -61,6 +62,7 @@
"@types/node": "^24.11.0",
"@types/path-browserify": "^1.0.3",
"@types/qrcode": "^1.5.6",
"@types/sortablejs": "^1.15.9",
"@types/spark-md5": "^3.0.5",
"@types/three": "^0.183.1",
"@vitejs/plugin-vue": "^6.0.4",

View File

@ -1,7 +1,7 @@
<!--
DefinitionFormDialog — 报表定义(wms_report_definition)新增/编辑 表单弹窗
对接接口:report.definition.save(按 id upsert,未传字段不覆盖)
极简配置:报表类型 type(report.definition.typeList 注册)+ 勾选字段 fields + 导出文件名 file_name(支持 {date} 占位)
极简配置:报表类型 type(report.definition.typeList 注册)+ 勾选字段 fields(勾选后支持上移/下移排序,顺序即导出表格列顺序)+ 导出文件名 file_name(支持 {date} 占位)
-->
<template>
<el-dialog
@ -67,6 +67,34 @@
<span v-if="f.children?.length" class="field-array">(含子字段 {{ f.children.length }} 项)</span>
</el-checkbox>
</el-checkbox-group>
<div v-if="form.fields.length" class="field-order">
<div class="field-order__title">
字段顺序(自上而下即导出表格列顺序)
<span class="field-order__tip">已选 {{ form.fields.length }} 项,可拖动手柄或使用按钮调整</span>
</div>
<div ref="orderListRef" class="field-order__list">
<div
v-for="(key, idx) in form.fields"
:key="key"
class="field-order__item"
>
<el-icon class="field-order__drag" :size="14"><Rank /></el-icon>
<span class="field-order__idx">{{ idx + 1 }}</span>
<span class="field-order__label" :title="key">{{ fieldLabel(key) }}</span>
<span class="field-order__key">{{ key }}</span>
<span class="field-order__actions">
<el-button link size="small" :disabled="idx === 0" @click="moveField(idx, -1)">
上移
</el-button>
<el-button link size="small" :disabled="idx === form.fields.length - 1" @click="moveField(idx, 1)">
下移
</el-button>
</span>
</div>
</div>
</div>
<div v-if="!currentTypeFields.length" class="form-tip">
{{ isEdit ? '未找到该类型的字段清单' : '请先选择报表类型' }}
</div>
@ -92,8 +120,10 @@
</template>
<script setup lang="ts">
import { reactive, ref, watch, computed } from 'vue'
import { reactive, ref, watch, computed, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import { Rank } from '@element-plus/icons-vue'
import Sortable from 'sortablejs'
import { saveReportDefinition, type ReportDefinitionTypeField } from '@/api/report/report'
import {
loadReportTypeList,
@ -139,6 +169,42 @@ const form = reactive({
remark: ''
})
/** 排序列表容器(拖拽排序 DOM) */
const orderListRef = ref<HTMLElement | null>(null)
let sortable: Sortable | null = null
let sortableEl: HTMLElement | null = null
/**
* 初始化/重建拖拽排序实例。
* - 容器不存在(未勾选字段/对话框未打开)则跳过,等待后续调用;
* - 容器引用未变化(仅 v-for 子项增删)则复用已有实例;
* - 容器因 v-if(fields 清空)重建时销毁旧实例并重新绑定。
* 拖动结束后将 DOM 顺序回写 form.fields,保存时按该顺序提交(即导出表格列顺序)。
*/
function initSortable() {
const el = orderListRef.value
if (!el) return
if (sortable && sortableEl === el) return
sortable?.destroy()
sortable = new Sortable(el, {
animation: 150,
handle: '.field-order__drag',
ghostClass: 'field-order__ghost',
chosenClass: 'field-order__chosen',
dragClass: 'field-order__dragging',
onEnd: (evt) => {
const { oldIndex, newIndex } = evt
if (oldIndex === undefined || newIndex === undefined || oldIndex === newIndex) return
const arr = [...form.fields]
const [cur] = arr.splice(oldIndex, 1)
if (cur === undefined) return
arr.splice(newIndex, 0, cur)
form.fields = arr
}
})
sortableEl = el
}
const isEdit = computed(() => Number(props.id) > 0)
const rules = {
@ -170,6 +236,26 @@ function handleTypeChange() {
}
}
/** 字段 key → 中文名(未命中注册清单时回退 key 本身) */
function fieldLabel(key: string): string {
return currentTypeFields.value.find((f) => f.key === key)?.label || key
}
/**
* 移动已选字段顺序(dir: -1 上移 / 1 下移)。
* fields 数组按传入顺序保存(report.definition.save 按顺序去重),
* 该顺序即导出表格的列顺序,故此处调整会直接影响表格输出。
*/
function moveField(index: number, dir: -1 | 1) {
const target = index + dir
if (target < 0 || target >= form.fields.length) return
const arr = [...form.fields]
const [cur] = arr.splice(index, 1)
if (cur === undefined) return
arr.splice(target, 0, cur)
form.fields = arr
}
/**
* 编辑时对比报表定义既有字段与该类型注册字段清单(getReportTypeFields,来自 report.definition.typeList):
* - 类型命中注册清单时,仅保留清单中仍存在的字段 —— 类型字段清单变更后,自动剔除 list 中残留的失效字段
@ -183,6 +269,31 @@ function resolveEditableFields(fields: any[] | undefined, typeKey: string): stri
return fields.map(String).filter((k) => registeredKeys.has(k))
}
// dialog destroy-on-close 每次打开会重建 DOM,打开后重建拖拽实例
watch(
() => props.visible,
() => {
if (props.visible) {
nextTick(() => {
sortable?.destroy()
sortable = null
sortableEl = null
initSortable()
})
}
}
)
// 勾选/取消/回填字段后确保拖拽就绪(深度监听,长度不变也触发;容器 v-if 重建时重建实例)
watch(
() => [...form.fields],
async () => {
if (!props.visible) return
await nextTick()
initSortable()
}
)
watch(() => props.visible, async (v) => {
if (v) {
// 打开前确保类型注册清单已加载
@ -265,6 +376,97 @@ async function handleSubmit() {
color: #409eff;
}
.field-order {
width: 100%;
margin-top: 8px;
&__title {
font-size: 12px;
color: #606266;
font-weight: 600;
margin-bottom: 6px;
.field-order__tip {
font-weight: 400;
color: #909399;
margin-left: 6px;
}
}
&__list {
display: flex;
flex-direction: column;
gap: 4px;
max-height: 220px;
overflow-y: auto;
padding-right: 4px;
}
&__item {
display: flex;
align-items: center;
gap: 8px;
padding: 4px 8px;
border: 1px solid #ebeef5;
border-radius: 4px;
background: #fafafa;
font-size: 13px;
}
&__drag {
flex: 0 0 auto;
color: #909399;
cursor: move;
}
&__ghost {
opacity: 0.4;
border-style: dashed;
border-color: #409eff;
background: #ecf5ff;
}
&__chosen {
border-color: #409eff;
background: #ecf5ff;
}
&__dragging {
opacity: 0.9;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}
&__idx {
flex: 0 0 20px;
height: 20px;
line-height: 20px;
text-align: center;
border-radius: 50%;
background: #409eff;
color: #fff;
font-size: 12px;
}
&__label {
flex: 0 0 auto;
color: #303133;
}
&__key {
flex: 1 1 auto;
color: #909399;
font-size: 12px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
&__actions {
flex: 0 0 auto;
white-space: nowrap;
}
}
.type-desc {
max-height: 52px;
overflow-y: auto;