修改商品搜索及完善批次页面

This commit is contained in:
zhengw
2026-02-05 11:28:26 +08:00
parent 62039e3684
commit 4b8842d676
14 changed files with 256 additions and 13 deletions

View File

@@ -66,6 +66,52 @@ Page({
paginationChange(e: any) {
this.getList(e.detail.curr_page);
},
/**
* 根据父级ID排序树形结构的一维数组
*/
sortTreeArrayByParentId(
arr: any[],
rootParentId = 0,
idKey = 'cate_id',
parentIdKey = 'parent_cate_id',
) {
// 1. 构建映射表,快速查找子节点
const nodeMap = new Map();
arr.forEach((node) => {
nodeMap.set(node[idKey], { ...node, children: [] });
});
// 2. 构建树形结构
const tree: any[] = [];
arr.forEach((node) => {
const currentNode = nodeMap.get(node[idKey]);
const parentNode = nodeMap.get(node[parentIdKey]);
if (parentNode) {
// 有父节点添加到父节点的children中
parentNode.children.push(currentNode);
} else if (node[parentIdKey] === rootParentId) {
// 根节点,直接加入树形结构
tree.push(currentNode);
}
});
// 3. 递归扁平化树形结构,按层级输出
function flattenTree(nodes: any[]) {
const result: any[] = [];
nodes.forEach((node) => {
// 先加入当前节点
result.push({ ...node, children: undefined }); // 移除children属性还原原始结构
// 递归处理子节点
if (node.children && node.children.length > 0) {
result.push(...flattenTree(node.children));
}
});
return result;
}
return flattenTree(tree);
},
getData() {
post('ErpDepot/list').then((res: any) => {
const depots = toArray(res.data?.list).map((el) => {
@@ -74,10 +120,14 @@ Page({
this.setData({ depots });
});
post('GoodsCategory/list').then((res: any) => {
const goodsClass = toArray(res.data?.list).map((el) => {
// const goodsClass = toArray(res.data?.list).map((el) => {
// return { label: el.cate_name, value: el.cate_id };
// });
const sortedArray = this.sortTreeArrayByParentId(toArray(res.data?.list)).map((el) => {
return { label: el.cate_name, value: el.cate_id };
});
this.setData({ goodsClass });
this.setData({ goodsClass: sortedArray });
});
},
getList(curr: number = 1) {