提交 04d8a0cd authored 作者: 王鹏飞's avatar 王鹏飞

chore: 优化类别管理拖拽

上级 2e3c599e
...@@ -70,6 +70,7 @@ const fetchList = (isReset = false) => { ...@@ -70,6 +70,7 @@ const fetchList = (isReset = false) => {
// } // }
// } // }
loading.value = true loading.value = true
return (
httpRequest(requestParams) httpRequest(requestParams)
.then((res: any) => { .then((res: any) => {
const { list = [], total = 0 } = callback ? callback(res.data) : res.data || {} const { list = [], total = 0 } = callback ? callback(res.data) : res.data || {}
...@@ -83,12 +84,13 @@ const fetchList = (isReset = false) => { ...@@ -83,12 +84,13 @@ const fetchList = (isReset = false) => {
.finally(() => { .finally(() => {
loading.value = false loading.value = false
}) })
)
} }
// 搜索 // 搜索
const search = () => { const search = () => {
page.currentPage = 1 page.currentPage = 1
fetchList() return fetchList()
} }
// 重置 // 重置
...@@ -98,12 +100,12 @@ const reset = () => { ...@@ -98,12 +100,12 @@ const reset = () => {
// 初始化页码 // 初始化页码
page.currentPage = 1 page.currentPage = 1
// 刷新列表 // 刷新列表
fetchList(true) return fetchList(true)
} }
// 刷新 // 刷新
const refetch = (isForce = false) => { const refetch = (isForce = false) => {
isForce ? reset() : fetchList() return isForce ? reset() : fetchList()
} }
// 页数改变 // 页数改变
...@@ -117,7 +119,7 @@ onMounted(() => { ...@@ -117,7 +119,7 @@ onMounted(() => {
fetchList() fetchList()
}) })
defineExpose({ refetch }) defineExpose({ refetch, tableRef })
</script> </script>
<template> <template>
......
...@@ -93,7 +93,8 @@ const handleUpdate = () => { ...@@ -93,7 +93,8 @@ const handleUpdate = () => {
// 扁平列表数据 // 扁平列表数据
let tableDataList = $ref<ICategory[]>([]) let tableDataList = $ref<ICategory[]>([])
// 列表配置 // 列表配置
const listOptions = { const listOptions = computed(() => {
return {
remote: { remote: {
httpRequest: getCategoryList, httpRequest: getCategoryList,
params: { type: 'tree', category_name: '' }, params: { type: 'tree', category_name: '' },
...@@ -110,8 +111,10 @@ const listOptions = { ...@@ -110,8 +111,10 @@ const listOptions = {
{ label: '层级', prop: 'depth', align: 'center' }, { label: '层级', prop: 'depth', align: 'center' },
{ label: '状态', prop: 'status_name', align: 'center' }, { label: '状态', prop: 'status_name', align: 'center' },
{ label: '操作', slots: 'table-operate', width: 230, align: 'center' } { label: '操作', slots: 'table-operate', width: 230, align: 'center' }
] ],
} data: tableData
}
})
// 数据扁平化 // 数据扁平化
function flatten(data: any[], pid = '0') { function flatten(data: any[], pid = '0') {
return data.reduce((acc: any[], item: any) => { return data.reduce((acc: any[], item: any) => {
...@@ -142,23 +145,37 @@ function useSortable() { ...@@ -142,23 +145,37 @@ function useSortable() {
}, },
onEnd(evt: SortableEvent) { onEnd(evt: SortableEvent) {
const { oldIndex, newIndex } = evt const { oldIndex, newIndex } = evt
if (oldIndex === undefined || newIndex === undefined) return if (oldIndex === undefined || newIndex === undefined || oldIndex === newIndex) return
const draggedRow = tableDataList[oldIndex] const draggedRow = tableDataList[oldIndex]
const relatedRow = tableDataList[newIndex] const relatedRow = tableDataList[newIndex]
moveCategory({ id: draggedRow.id, brother_id: relatedRow.id, type: newIndex > oldIndex ? 'after' : 'before' }).then(() => {
moveCategory({ id: draggedRow.id, brother_id: relatedRow.id, type: 'before' }).then(() => { // 修复拖拽后数据顺序不对的问题
if (oldIndex < newIndex) {
tableData = []
}
handleUpdate() handleUpdate()
}) })
} }
}) })
} }
// 展开行
let expandRowKeys = $ref<string[]>([])
function onExpandChange(row: ICategory, expanded: boolean) {
if (expanded) {
expandRowKeys.push(row.id)
} else {
expandRowKeys = expandRowKeys.filter(id => id !== row.id)
}
}
onMounted(() => { onMounted(() => {
useSortable() useSortable()
}) })
</script> </script>
<template> <template>
<AppCard title="类别管理"> <AppCard title="类别管理">
<AppList v-bind="listOptions" :has-pagination="false" row-key="id" ref="appList"> <AppList v-bind="listOptions" :has-pagination="false" row-key="id" ref="appList" :expand-row-keys="expandRowKeys" @expand-change="onExpandChange">
<el-button type="primary" @click="handleAddCategory">新增类别</el-button> <el-button type="primary" @click="handleAddCategory">新增类别</el-button>
<template #table-drag> <template #table-drag>
<el-icon><Operation /></el-icon> <el-icon><Operation /></el-icon>
...@@ -170,13 +187,5 @@ onMounted(() => { ...@@ -170,13 +187,5 @@ onMounted(() => {
</template> </template>
</AppList> </AppList>
</AppCard> </AppCard>
<AddDialog <AddDialog v-model:dialogVisible="dialogVisible" @updatePage="handleUpdate" :editData="editData" :isEdit="isEdit" :title="title" :prevCategoryName="prevCategoryName" v-if="dialogVisible" />
v-model:dialogVisible="dialogVisible"
@updatePage="handleUpdate"
:editData="editData"
:isEdit="isEdit"
:title="title"
:prevCategoryName="prevCategoryName"
v-if="dialogVisible"
/>
</template> </template>
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论