品类新增

This commit is contained in:
孙景学
2025-07-30 14:39:37 +08:00
parent ce39764f08
commit ad4efd8e46

View File

@ -12,12 +12,39 @@ const approveTypeOptions = [
{ label: '是', value: 'online' }, { label: '是', value: 'online' },
{ label: '否', value: 'offline' }, { label: '否', value: 'offline' },
]; ];
function collectSelectedNodes(nodes: TreeNodeType[], checkedKeys: string[]): { id: string; categoryName: string; categoryType:string }[] {
const result: { id: string; categoryName: string; categoryType:string; lockType:number }[] = []; function collectCheckedWithParents(
nodes: TreeNodeType[],
checked: string[]
): Set<string> {
const result = new Set<string>();
function dfs(node: TreeNodeType, parents: string[]) {
if (checked.includes(node.key)) {
result.add(node.key);
parents.forEach(pid => result.add(pid));
}
node.children?.forEach(child => dfs(child, [...parents, node.key]));
}
nodes.forEach(root => dfs(root, []));
return result;
}
// 从树中获取所有选中节点及其父级节点
function collectSelectedNodesWithParents(
nodes: TreeNodeType[],
checkedKeys: string[]
): { id: string; categoryName: string; lockType: number; categoryType: string }[] {
const allKeysSet = collectCheckedWithParents(nodes, checkedKeys);
const result: { id: string; categoryName: string; lockType: number; categoryType: string }[] = [];
function dfs(list: TreeNodeType[]) { function dfs(list: TreeNodeType[]) {
list.forEach(node => { list.forEach(node => {
if (checkedKeys.includes(node.key)) { if (allKeysSet.has(node.key)) {
result.push({ id: node.key, categoryName: node.categoryName, lockType: 0, categoryType: node.children?.length === 0? '1': '0' }); result.push({
id: node.key,
categoryName: node.categoryName,
lockType: 0,
categoryType: node.children?.length === 0 ? '1' : '0',
});
} }
if (node.children && node.children.length) dfs(node.children); if (node.children && node.children.length) dfs(node.children);
}); });
@ -25,6 +52,7 @@ function collectSelectedNodes(nodes: TreeNodeType[], checkedKeys: string[]): { i
dfs(nodes); dfs(nodes);
return result; return result;
} }
// TS 类型定义 // TS 类型定义
interface TreeNodeType { interface TreeNodeType {
id: string; id: string;
@ -166,10 +194,9 @@ const CategoryAddModal: React.FC<Props> = ({ visible, onCancel, onSuccess }) =>
form.setFieldsValue({ categoryKeys: checkedKeysValue }); form.setFieldsValue({ categoryKeys: checkedKeysValue });
// 新增:存储 [{id, title}] // 新增:存储 [{id, title}]
const selectedList = collectSelectedNodes(treeData, checkedKeysValue); const selectedList = collectSelectedNodesWithParents(treeData, checkedKeysValue);
form.setFieldsValue({ coscoAccessCategoryList: selectedList }); form.setFieldsValue({ coscoAccessCategoryList: selectedList });
if (hasFuelCategory(checkedKeysValue, treeData)) { if (hasFuelCategory(checkedKeysValue, treeData)) {
setAreaRequired(true); setAreaRequired(true);
form.validateFields(['area']); form.validateFields(['area']);
@ -177,21 +204,8 @@ const CategoryAddModal: React.FC<Props> = ({ visible, onCancel, onSuccess }) =>
setAreaRequired(false); setAreaRequired(false);
} }
}; };
// 从树中获取所有选中节点及其父级节点
const collectCheckedWithParents = (nodes: TreeNodeType[], checked: string[]): string[] => {
const result = new Set<string>();
const dfs = (node: TreeNodeType, parents: string[]) => {
if (checked.includes(node.key)) {
result.add(node.key);
parents.forEach(pid => result.add(pid)); // 加入所有父级
}
node.children?.forEach(child => dfs(child, [...parents, node.key]));
};
nodes.forEach(root => dfs(root, []));
return Array.from(result);
};
// 提交校验 // 提交校验
const handleOk = async () => { const handleOk = async () => {