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

chore: 修改类别不展示0级目录

上级 b7450e4b
...@@ -3,12 +3,7 @@ import { getCategoryList } from '@/api/base' ...@@ -3,12 +3,7 @@ import { getCategoryList } from '@/api/base'
export function useGetCategoryList() { export function useGetCategoryList() {
const list = ref([]) const list = ref([])
getCategoryList({ type: 'tree' }).then((res: any) => { getCategoryList({ type: 'tree' }).then((res: any) => {
list.value = res.data.filter((item: any) => { list.value = detailChild(res.data?.[0]?.children)
if (parseInt(item.status)) {
item.children = detailChild(item.children)
}
return parseInt(item.status)
})
}) })
const detailChild = (child: any) => { const detailChild = (child: any) => {
...@@ -21,4 +16,4 @@ export function useGetCategoryList() { ...@@ -21,4 +16,4 @@ export function useGetCategoryList() {
}) })
} }
return { list } return { list }
} }
\ No newline at end of file
...@@ -53,6 +53,9 @@ const props = defineProps({ ...@@ -53,6 +53,9 @@ const props = defineProps({
required: true required: true
} }
}) })
const isTopLevelCategory = computed(() =>
props.isEdit ? Number(props.editData?.depth) === 1 : Number(props.editData?.depth) === 0
)
const emit = defineEmits<{ const emit = defineEmits<{
(e: 'update:dialogVisible', dialogVisible: false): void (e: 'update:dialogVisible', dialogVisible: false): void
(e: 'updatePage'): void (e: 'updatePage'): void
...@@ -123,7 +126,7 @@ onMounted(() => { ...@@ -123,7 +126,7 @@ onMounted(() => {
ref="formRef" ref="formRef"
label-suffix=":" label-suffix=":"
> >
<el-form-item label="上级类别" prop="parent_id"> <el-form-item v-if="!isTopLevelCategory" label="上级类别" prop="parent_id">
<div style="width: 100%"> <div style="width: 100%">
<el-input v-model="categoryName" disabled> <el-input v-model="categoryName" disabled>
<template #append> <template #append>
...@@ -132,7 +135,7 @@ onMounted(() => { ...@@ -132,7 +135,7 @@ onMounted(() => {
</el-input> </el-input>
</div> </div>
</el-form-item> </el-form-item>
<el-form-item label="类别层级" prop="hierarchy"> <el-form-item v-if="!isTopLevelCategory" label="类别层级" prop="hierarchy">
<el-input v-model="categoryForm.depth" disabled></el-input> <el-input v-model="categoryForm.depth" disabled></el-input>
</el-form-item> </el-form-item>
<el-form-item label="类别名称" prop="category_name"> <el-form-item label="类别名称" prop="category_name">
......
...@@ -24,6 +24,7 @@ interface ICategory { ...@@ -24,6 +24,7 @@ interface ICategory {
children?: ICategory[] children?: ICategory[]
} }
const tableData = ref<ICategory[]>([]) const tableData = ref<ICategory[]>([])
const rootCategory = ref<ICategory>()
const editData = ref({}) const editData = ref({})
const isEdit = ref(false) const isEdit = ref(false)
const handleEdit = (row: ICategory) => { const handleEdit = (row: ICategory) => {
...@@ -31,8 +32,8 @@ const handleEdit = (row: ICategory) => { ...@@ -31,8 +32,8 @@ const handleEdit = (row: ICategory) => {
isEdit.value = true isEdit.value = true
dialogVisible.value = true dialogVisible.value = true
editData.value = row editData.value = row
if (row.depth > '0') { if (row.depth > '0' && rootCategory.value) {
prevCategoryName.value = getParent(row.id, tableData.value[0]).category_name prevCategoryName.value = getParent(row.id, rootCategory.value)?.category_name ?? ''
} else { } else {
prevCategoryName.value = '' prevCategoryName.value = ''
} }
...@@ -64,12 +65,12 @@ const handleAddCategory = () => { ...@@ -64,12 +65,12 @@ const handleAddCategory = () => {
isEdit.value = false isEdit.value = false
dialogVisible.value = true dialogVisible.value = true
title.value = '新增类别' title.value = '新增类别'
editData.value = tableData.value[0] editData.value = rootCategory.value ?? {}
prevCategoryName.value = tableData.value[0].category_name prevCategoryName.value = rootCategory.value?.category_name ?? ''
} }
// 获取上级类别 // 获取上级类别
const getParent = (node: string, tree: any) => { const getParent = (node: string, tree: any) => {
let result: any = [] const result: any = []
function find(node: string, tree: any) { function find(node: string, tree: any) {
tree.children.forEach((item: any) => { tree.children.forEach((item: any) => {
if (item.id === node) { if (item.id === node) {
...@@ -98,7 +99,8 @@ const listOptions = computed(() => { ...@@ -98,7 +99,8 @@ const listOptions = computed(() => {
httpRequest: getCategoryList, httpRequest: getCategoryList,
params: { type: 'tree', category_name: '' }, params: { type: 'tree', category_name: '' },
callback(data: ICategory[], params: any) { callback(data: ICategory[], params: any) {
const list = rebuildData(params.category_name, data) rootCategory.value = data[0]
const list = rebuildData(params.category_name, rootCategory.value?.children ?? [])
tableData.value = list tableData.value = list
tableDataList.value = flatten(list) tableDataList.value = flatten(list)
return { list } return { list }
...@@ -180,7 +182,7 @@ const rebuildData = (value: any, arr: any) => { ...@@ -180,7 +182,7 @@ const rebuildData = (value: any, arr: any) => {
if (!arr) { if (!arr) {
return [] return []
} }
let newArr: any = [] const newArr: any = []
arr.forEach((element: any) => { arr.forEach((element: any) => {
// indexOf用来判读当前节点name字段是否包含所搜索的字符串value // indexOf用来判读当前节点name字段是否包含所搜索的字符串value
// 返回值:包含则返回索引值,反之返回-1 // 返回值:包含则返回索引值,反之返回-1
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论