Skip to content

Commit 71de0c3

Browse files
committed
【代码优化】工作流:1)完善相关注释;2)解决模型每次操作后,刷新列表后,重渲染会导致滚动条的位置不对的问题
1 parent c3cf479 commit 71de0c3

File tree

2 files changed

+37
-48
lines changed

2 files changed

+37
-48
lines changed

src/views/bpm/model/CategoryDraggableModel.vue

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<template>
22
<div class="flex items-center h-50px">
3+
<!-- 头部:分类名 -->
34
<div class="flex items-center">
45
<el-tooltip content="拖动排序" v-if="isCategorySorting">
56
<Icon
@@ -11,6 +12,7 @@
1112
<h3 class="ml-20px mr-8px text-18px">{{ categoryInfo.name }}</h3>
1213
<div class="color-gray-600 text-16px"> ({{ categoryInfo.modelList?.length || 0 }}) </div>
1314
</div>
15+
<!-- 头部:操作 -->
1416
<div class="flex-1 flex" v-if="!isCategorySorting">
1517
<div
1618
v-if="categoryInfo.modelList.length > 0"
@@ -62,6 +64,7 @@
6264
</div>
6365
</div>
6466
</div>
67+
<!-- 模型列表 -->
6568
<el-collapse-transition>
6669
<div v-show="isExpand">
6770
<el-table
@@ -70,7 +73,7 @@
7073
:header-cell-style="{ backgroundColor: isDark ? '' : '#edeff0', paddingLeft: '10px' }"
7174
:cell-style="{ paddingLeft: '10px' }"
7275
:row-style="{ height: '68px' }"
73-
:data="tableData"
76+
:data="modelList"
7477
row-key="id"
7578
>
7679
<el-table-column label="流程名" prop="name" min-width="150">
@@ -110,15 +113,15 @@
110113
<el-table-column label="表单信息" prop="formType" min-width="200">
111114
<template #default="scope">
112115
<el-button
113-
v-if="scope.row.formType === 10"
116+
v-if="scope.row.formType === BpmModelFormType.NORMAL"
114117
type="primary"
115118
link
116119
@click="handleFormDetail(scope.row)"
117120
>
118121
<span>{{ scope.row.formName }}</span>
119122
</el-button>
120123
<el-button
121-
v-else-if="scope.row.formType === 20"
124+
v-else-if="scope.row.formType === BpmModelFormType.CUSTOM"
122125
type="primary"
123126
link
124127
@click="handleFormDetail(scope.row)"
@@ -246,7 +249,7 @@ import { formatDate } from '@/utils/formatTime'
246249
import * as ModelApi from '@/api/bpm/model'
247250
import * as FormApi from '@/api/bpm/form'
248251
import { setConfAndFields2 } from '@/utils/formCreate'
249-
import { BpmModelType } from '@/utils/constants'
252+
import { BpmModelFormType, BpmModelType } from '@/utils/constants'
250253
import { checkPermi } from '@/utils/permission'
251254
import { useUserStoreWithOut } from '@/store/modules/user'
252255
import { useAppStore } from '@/store/modules/app'
@@ -255,20 +258,19 @@ import { cloneDeep } from 'lodash-es'
255258
defineOptions({ name: 'BpmModel' })
256259
257260
const props = defineProps({
258-
// 分类后的数据
259-
categoryInfo: propTypes.object.def([]),
260-
isCategorySorting: propTypes.bool.def(false)
261+
categoryInfo: propTypes.object.def([]), // 分类后的数据
262+
isCategorySorting: propTypes.bool.def(false) // 是否分类在排序
261263
})
262264
const emit = defineEmits(['success'])
263-
const appStore = useAppStore()
264265
const message = useMessage() // 消息弹窗
265-
const isDark = computed(() => appStore.getIsDark)
266266
const { t } = useI18n() // 国际化
267267
const { push } = useRouter() // 路由
268268
const userStore = useUserStoreWithOut() // 用户信息缓存
269+
const isDark = computed(() => useAppStore().getIsDark) // 是否黑暗模式
270+
269271
const isModelSorting = ref(false) // 是否正处于排序状态
270-
const tableData: any = ref([]) // 模型列表
271272
const originalData: any = ref([]) // 原始数据
273+
const modelList: any = ref([]) // 模型列表
272274
const isExpand = ref(false) // 是否处于展开状态
273275
274276
/** '更多'操作按钮 */
@@ -414,7 +416,7 @@ const handleModelSort = () => {
414416
/** 处理模型的排序提交 */
415417
const handleModelSortSubmit = async () => {
416418
// 保存排序
417-
const ids = tableData.value.map((item: any) => item.id)
419+
const ids = modelList.value.map((item: any) => item.id)
418420
await ModelApi.updateModelSortBatch(ids)
419421
// 刷新列表
420422
isModelSorting.value = false
@@ -425,12 +427,12 @@ const handleModelSortSubmit = async () => {
425427
/** 处理模型的排序取消 */
426428
const handleModelSortCancel = () => {
427429
// 恢复初始数据
428-
tableData.value = cloneDeep(originalData.value)
430+
modelList.value = cloneDeep(originalData.value)
429431
isModelSorting.value = false
430432
}
431433
434+
/** 创建拖拽实例 */
432435
const tableRef = ref()
433-
// 创建拖拽实例
434436
const initSort = () => {
435437
const table = document.querySelector(`.${props.categoryInfo.name} .el-table__body-wrapper tbody`)
436438
Sortable.create(table, {
@@ -441,19 +443,19 @@ const initSort = () => {
441443
// 结束拖动事件
442444
onEnd: ({ newDraggableIndex, oldDraggableIndex }) => {
443445
if (oldDraggableIndex !== newDraggableIndex) {
444-
tableData.value.splice(
446+
modelList.value.splice(
445447
newDraggableIndex,
446448
0,
447-
tableData.value.splice(oldDraggableIndex, 1)[0]
449+
modelList.value.splice(oldDraggableIndex, 1)[0]
448450
)
449451
}
450452
}
451453
})
452454
}
453455
454-
// 更新表格数据
455-
const updateTableData = () => {
456-
tableData.value = cloneDeep(props.categoryInfo.modelList)
456+
/** 更新 modelList 模型列表 */
457+
const updateModeList = () => {
458+
modelList.value = cloneDeep(props.categoryInfo.modelList)
457459
if (props.categoryInfo.modelList.length > 0) {
458460
isExpand.value = true
459461
}
@@ -497,7 +499,7 @@ const openModelForm = (type: string, id?: number) => {
497499
modelFormRef.value.open(type, id)
498500
}
499501
500-
watch(() => props.categoryInfo.modelList, updateTableData, { immediate: true })
502+
watch(() => props.categoryInfo.modelList, updateModeList, { immediate: true })
501503
watch(
502504
() => props.isCategorySorting,
503505
(val) => {

src/views/bpm/model/index.vue

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
</el-button>
3939
<template #dropdown>
4040
<el-dropdown-menu>
41-
<el-dropdown-item command="handleAddCategory">
41+
<el-dropdown-item command="handleCategoryAdd">
4242
<Icon icon="ep:circle-plus" :size="13" class="mr-5px" />
4343
新建分类
4444
</el-dropdown-item>
45-
<el-dropdown-item command="handleSort">
45+
<el-dropdown-item command="handleCategorySort">
4646
<Icon icon="fa:sort-amount-desc" :size="13" class="mr-5px" />
4747
分类排序
4848
</el-dropdown-item>
@@ -101,7 +101,7 @@ import { CategoryApi } from '@/api/bpm/category'
101101
import * as ModelApi from '@/api/bpm/model'
102102
import ModelForm from './ModelForm.vue'
103103
import CategoryForm from '../category/CategoryForm.vue'
104-
import { groupBy, cloneDeep } from 'lodash-es'
104+
import { cloneDeep } from 'lodash-es'
105105
import CategoryDraggableModel from './CategoryDraggableModel.vue'
106106
107107
defineOptions({ name: 'BpmModel' })
@@ -116,26 +116,6 @@ const queryFormRef = ref() // 搜索的表单
116116
const categoryGroup: any = ref([]) // 按照 category 分组的数据
117117
const originalData: any = ref([]) // 原始数据
118118
119-
/** 查询所有分类 */
120-
const getAllCategory = async () => {
121-
const list = await CategoryApi.getCategorySimpleList()
122-
categoryGroup.value = list.map((item: any) => ({ ...item, modelList: [] }))
123-
}
124-
125-
/** 查询所有流程模型接口 */
126-
const getAllModel = async () => {
127-
// 查询所有流程模型
128-
const list = await ModelApi.getModelList(queryParams.name)
129-
// 按照 categoryName 分组
130-
const groupedData = groupBy(list, 'categoryName')
131-
Object.keys(groupedData).forEach((key) => {
132-
const category = categoryGroup.value.find((item: any) => item.name === key)
133-
if (category) {
134-
category.modelList = groupedData[key]
135-
}
136-
})
137-
}
138-
139119
/** 搜索按钮操作 */
140120
const handleQuery = () => {
141121
getList()
@@ -157,10 +137,10 @@ const formDetailPreview = ref({
157137
/** 右上角设置按钮 */
158138
const handleCommand = (command: string) => {
159139
switch (command) {
160-
case 'handleAddCategory':
161-
handleAddCategory()
140+
case 'handleCategoryAdd':
141+
handleCategoryAdd()
162142
break
163-
case 'handleSort':
143+
case 'handleCategorySort':
164144
handleCategorySort()
165145
break
166146
default:
@@ -170,7 +150,7 @@ const handleCommand = (command: string) => {
170150
171151
/** 新建分类 */
172152
const categoryFormRef = ref()
173-
const handleAddCategory = () => {
153+
const handleCategoryAdd = () => {
174154
categoryFormRef.value.open('create')
175155
}
176156
@@ -203,8 +183,15 @@ const handleCategorySortSubmit = async () => {
203183
const getList = async () => {
204184
loading.value = true
205185
try {
206-
await getAllCategory()
207-
await getAllModel()
186+
// 查询模型 + 分裂的列表
187+
const modelList = await ModelApi.getModelList(queryParams.name)
188+
const categoryList = await CategoryApi.getCategorySimpleList()
189+
// 按照 category 聚合
190+
// 注意:必须一次性赋值给 categoryGroup,否则每次操作后,列表会重新渲染,滚动条的位置会偏离!!!
191+
categoryGroup.value = categoryList.map((category: any) => ({
192+
...category,
193+
modelList: modelList.filter((model: any) => model.categoryName == category.name)
194+
}))
208195
} finally {
209196
loading.value = false
210197
}

0 commit comments

Comments
 (0)