朱鑫波 2 éve
szülő
commit
e4570a39d6
1 módosított fájl, 64 hozzáadás és 29 törlés
  1. 64 29
      src/views/goodsManagement/selectTree.vue

+ 64 - 29
src/views/goodsManagement/selectTree.vue

@@ -33,7 +33,7 @@
         @node-click="handleNodeClick"
         @check-change="handleCheckChange"
         :load="loadNode"
-        lazy
+        :lazy="lazy"
       ></el-tree>
     </el-option>
   </el-select>
@@ -83,6 +83,10 @@ export default {
         };
       },
     },
+    lazy: {
+      type: Boolean,
+      default: true,
+    },
   },
   data() {
     return {
@@ -116,29 +120,27 @@ export default {
       }
       if (node.level > 1) return resolve([]);
 
-      console.log(node);
-      getCity({ province: node.data.id }).then((res) => {
-        const data = res.map((item) => {
+      console.log(node, node.data.hasOwnProperty(this.props.children));
+      if (node.data.hasOwnProperty(this.props.children)) {
+        let data = node.data[this.props.children].map((item) => {
           return {
-            id: item,
-            name: item,
+            ...item,
+            projectName: item.accountName,
+            projectId: item.accountId,
           };
         });
         resolve(data);
-      });
-      //   setTimeout(() => {
-      //     const data = [
-      //       {
-      //         name: "leaf",
-      //         leaf: true,
-      //       },
-      //       {
-      //         name: "zone",
-      //       },
-      //     ];
-
-      //     resolve(data);
-      //   }, 500);
+      } else {
+        getCity({ province: node.data.id }).then((res) => {
+          const data = res.map((item) => {
+            return {
+              id: item,
+              name: item,
+            };
+          });
+          resolve(data);
+        });
+      }
     },
     // 是否为空
     isEmpty(val) {
@@ -148,7 +150,7 @@ export default {
       return true;
     },
     handleNodeClick(data, node, leaf) {
-      console.log(data, node, leaf);
+      // console.log(data, node, leaf);
       if (this.multiple) {
         return;
       }
@@ -161,22 +163,55 @@ export default {
 
       this.$refs.select.visible = false;
     },
-    handleCheckChange() {
-      const nodes = this.$refs.tree.getCheckedNodes();
-      const value = nodes.map((item) => item[this.nodeKey]).join(",");
-      this.$emit("input", value);
+    handleCheckChange(data, node, leaf) {
+      console.log(data, node, leaf);
+      // const nodes = this.$refs.tree.getCheckedNodes();
+      // const value = nodes.map((item) => item[this.nodeKey]).join(",");
+      // this.$emit("input", value);
+
+      this.$nextTick(() => {
+        const nodes = this.$refs.tree.getCheckedNodes();
+        // this.getSimpleCheckedNodes(this.$refs.tree.store);
+        console.log(nodes);
+        const value = nodes.map((item) => item[this.nodeKey]).join(",");
+        console.log(value);
+        this.$emit("input", value);
+      });
+    },
+    getSimpleCheckedNodes(store) {
+      console.log(store);
+      // 定义数组
+      const checkedNodes = [];
+      // 判断是否为全选,若为全选状态返回被全选的节点,不为全选状态正常返回被选中的节点
+      const traverse = function (node) {
+        const childNodes = node.root ? node.root.childNodes : node.childNodes;
+        childNodes.forEach((child) => {
+          if (child.checked) {
+            checkedNodes.push(child.data);
+          }
+          if (child.indeterminate) {
+            traverse(child);
+          }
+        });
+      };
+      traverse(store);
+      return checkedNodes;
     },
+
     init(val) {
+      console.log(val);
       // 多选
       if (this.multiple) {
+        const label = this.props.label || "name";
         const arr = val.toString().split(",");
         this.$nextTick(() => {
           this.$refs.tree.setCheckedKeys(arr);
-          const nodes = this.$refs.tree.getCheckedNodes();
+          const nodes = this.getSimpleCheckedNodes(this.$refs.tree.store);
+          const nodesTwo = this.$refs.tree.getCheckedNodes();
+          // this.$refs.tree.getCheckedNodes();
+          console.log(nodes, nodesTwo);
           this.optionData.id = val;
-          this.optionData.name = nodes
-            .map((item) => item[this.props.label])
-            .join(",");
+          this.optionData.name = nodes.map((item) => item[label]).join();
         });
       }
       // 单选