| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <template>
- <avue-crud v-bind="bindVal" v-on="onEvent" v-model="form"> </avue-crud>
- </template>
- <script>
- export default window.$crudCommon(
- {
- props: {
- yearAndMonth: [String, Number],
- },
- watch: {
- yearAndMonth() {
- this.loadData();
- },
- },
- methods: {
- buildTree(flatList, options = {}) {
- const {
- idKey = 'id', // 节点ID的键名
- parentKey = 'parentId', // 父节点ID的键名
- childrenKey = 'children' // 子节点列表的键名
- } = options;
-
- const nodeMap = {}; // 用于快速查找节点的映射表
- const tree = []; // 最终生成的树
-
- // 第一次遍历:创建所有节点的映射
- flatList.forEach(node => {
- nodeMap[node[idKey]] = {
- ...node,
- [childrenKey]: []
- };
- });
-
- // 第二次遍历:构建父子关系
- flatList.forEach(node => {
- const parentId = node[parentKey];
- if (parentId !== null && parentId !== undefined && nodeMap[parentId]) {
- // 如果有父节点,则将当前节点添加到父节点的children中
- nodeMap[parentId][childrenKey].push(nodeMap[node[idKey]]);
- } else {
- // 如果没有父节点或父节点不存在,则作为根节点
- tree.push(nodeMap[node[idKey]]);
- }
- });
-
- return tree;
- },
- loadData() {
- this.loading = true;
- this.api[this.option.list || "getList"](
- { yearAndMonth: this.yearAndMonth }
- ).then(({ data }) => {
- console.log(this.buildTree(data.data))
- this.data = this.buildTree(data.data);
- this.loading = false;
- });
- },
- // getSearchParams() {
- // return { yearAndMonth: this.yearAndMonth };
- // },
- },
- },
- {
- // 模块路径
- name: "basicResource/compDeptList",
- }
- );
- </script>
|