From 08231671c573d0239bea20c3f1c6c21f55b37f2e Mon Sep 17 00:00:00 2001 From: wangzenghua <1048523306@qq.com> Date: Fri, 28 Mar 2025 08:17:43 +0100 Subject: [PATCH] =?UTF-8?q?fix:cascader=E5=9B=9E=E6=98=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/apis/system/dict.js | 18 ++++---- .../src/apis/system/dictType.js | 18 ++++---- .../src/utils/index.js | 44 ++++++++++++++++++- .../component/landCassification/index.vue | 14 +++--- .../src/views/system/dict/index.vue | 2 +- .../src/views/system/dict/type.vue | 2 +- 6 files changed, 71 insertions(+), 27 deletions(-) diff --git a/sub-government-affairs-service/src/apis/system/dict.js b/sub-government-affairs-service/src/apis/system/dict.js index ab163c6..cb26c56 100644 --- a/sub-government-affairs-service/src/apis/system/dict.js +++ b/sub-government-affairs-service/src/apis/system/dict.js @@ -10,15 +10,6 @@ export function GetEntityList(params = {}) { }); } -/** - * @Title: 根据字典类型查询字典数据信息 - */ -export function GetDict(dictType) { - return request(`/system/dict/data/list/${dictType}`, { - method: 'GET', - }); -} - /** * @Title: 新增 */ @@ -47,3 +38,12 @@ export function DeleteEntity(params = {}) { method: 'DELETE', }); } + +/** + * @Title: 根据字典类型查询字典数据信息 + */ +export function GetDict(dictType) { + return request(`/system/dict/data/list/${dictType}`, { + method: 'GET', + }); +} diff --git a/sub-government-affairs-service/src/apis/system/dictType.js b/sub-government-affairs-service/src/apis/system/dictType.js index b43b9db..b95c0eb 100644 --- a/sub-government-affairs-service/src/apis/system/dictType.js +++ b/sub-government-affairs-service/src/apis/system/dictType.js @@ -10,15 +10,6 @@ export function GetEntityList(params = {}) { }); } -/** - * @Title: 信息 - */ -export function GetEntity(dictId) { - return request(`/system/dict/type/list/${dictId}`, { - method: 'GET', - }); -} - /** * @Title: 新增 */ @@ -56,3 +47,12 @@ export function GetEntityOption() { method: 'GET', }); } + +/** + * @Title: 信息 + */ +export function GetDictType(dictId) { + return request(`/system/dict/type/list/${dictId}`, { + method: 'GET', + }); +} diff --git a/sub-government-affairs-service/src/utils/index.js b/sub-government-affairs-service/src/utils/index.js index 34eb2bf..b9b0504 100644 --- a/sub-government-affairs-service/src/utils/index.js +++ b/sub-government-affairs-service/src/utils/index.js @@ -3,7 +3,7 @@ * @Author: zenghua.wang * @Date: 2022-02-23 21:12:37 * @LastEditors: zenghua.wang - * @LastEditTime: 2025-03-27 15:43:36 + * @LastEditTime: 2025-03-28 14:18:58 */ import lodash from 'lodash'; import dayjs from 'dayjs'; @@ -123,6 +123,16 @@ export const setDicLabel = (dicData, value) => { } return label; }; +/** + * @Title 设置字段显隐 + * @param {*} column + * @param {*} fields + */ +export const setPropDisplay = (column, fields) => { + column.forEach((item) => { + item.display = fields.includes(item.prop) ? false : true; + }); +}; /** * @Title 将tree数据结构打平 * @param {*} tree @@ -283,6 +293,38 @@ export const getTree = (data, id = 'id', parentId = 'parentId', children = 'chil return tree; }; +/** + * @Title 根据节点 id 查找其父级、祖父级等所有上级节点 id + * @param {*} treeData + * @param {*} targetId + * @returns + */ +export const getParentIds = (treeData, targetId, id = 'id', children = 'children') => { + const parentMap = {}; + function buildParentMap(node, parentId = null) { + if (node[id]) { + parentMap[node[id]] = parentId; + } + if (node[children]) { + for (const child of node[children]) { + buildParentMap(child, node[id]); + } + } + } + + for (const rootNode of treeData) { + buildParentMap(rootNode); + } + + const parentIds = []; + let currentId = targetId; + while (parentMap[currentId] !== null) { + parentIds.push(parentMap[currentId]); + currentId = parentMap[currentId]; + } + return parentIds.reverse(); +}; + /** * @Title 获取路由中的参数 * @param name diff --git a/sub-government-affairs-service/src/views/dict/component/landCassification/index.vue b/sub-government-affairs-service/src/views/dict/component/landCassification/index.vue index 37fd670..d392829 100644 --- a/sub-government-affairs-service/src/views/dict/component/landCassification/index.vue +++ b/sub-government-affairs-service/src/views/dict/component/landCassification/index.vue @@ -58,7 +58,7 @@ import { useApp } from '@/hooks'; import { CRUD_OPTIONS } from '@/config'; import { getLandTypeTree, landTypeSave, getLandType, exportLandType, delLandType, editLandType } from '@/apis/baseInfo'; import { useUserStore } from '@/store/modules/user'; -import { isEmpty, flattenTree, setDicLabel, downloadFile } from '@/utils'; +import { isEmpty, getParentIds, flattenTree, setDicLabel, downloadFile } from '@/utils'; const { VITE_APP_BASE_API } = import.meta.env; const app = useApp(); @@ -125,7 +125,7 @@ const state = reactive({ dicHeaders: { authorization: UserStore.token, }, - dicFormatter: (res) => [{ id: null, landType: '土地分类', children: res.data }], + dicFormatter: (res) => [{ id: '0', landType: '土地分类', children: res.data }], span: 24, rules: [ { @@ -188,7 +188,7 @@ watch( if (!isEmpty(val)) { const list = flattenTree(val); treeDicData.value = list.map((item) => { - return { label: item.landType, value: item.id }; + return { label: item.landType, value: item.id, id: item.id, parentId: item.pid }; }); } } @@ -198,7 +198,7 @@ const getLandTree = async () => { try { const res = await getLandTypeTree(); if (res.code == 200) { - treeData.value = [{ id: null, landType: '土地分类', children: res.data }]; + treeData.value = [{ id: '0', landType: '土地分类', children: res.data }]; } } catch (err) { app.$message.error(err.msg); @@ -210,7 +210,7 @@ getLandTree(); const loadData = () => { state.loading = true; getLandType({ - pid: treeSelected.value?.id ?? null, + pid: treeSelected.value?.id ?? '0', ...state.query, }) .then((res) => { @@ -307,7 +307,7 @@ async function rowDel(row, done, loading) { const setPid = (row) => { if (!isEmpty(row.pids)) { const len = row.pids.length; - row.pid = row?.pids[len - 1] ?? null; + row.pid = row?.pids[len - 1] ?? '0'; } }; @@ -333,6 +333,8 @@ const rowSave = async (row, done, loading) => { // 编辑 const rowEdit = (row) => { + row.pids = getParentIds(treeData.value, row.id); + console.log('pids=', row.pids); crudRef.value && crudRef.value.rowEdit(row); }; diff --git a/sub-government-affairs-service/src/views/system/dict/index.vue b/sub-government-affairs-service/src/views/system/dict/index.vue index fe3b860..bfd2833 100644 --- a/sub-government-affairs-service/src/views/system/dict/index.vue +++ b/sub-government-affairs-service/src/views/system/dict/index.vue @@ -112,7 +112,7 @@ const state = reactive({ value: '0', rules: { required: true, - message: '请输入', + message: '请选择', trigger: 'blur', }, }, diff --git a/sub-government-affairs-service/src/views/system/dict/type.vue b/sub-government-affairs-service/src/views/system/dict/type.vue index 94e0b9c..fc196ac 100644 --- a/sub-government-affairs-service/src/views/system/dict/type.vue +++ b/sub-government-affairs-service/src/views/system/dict/type.vue @@ -83,7 +83,7 @@ const state = reactive({ value: '0', rules: { required: true, - message: '请输入', + message: '请选择', trigger: 'blur', }, },