当前位置:网站首页>Convert the array selected by El tree into an array object

Convert the array selected by El tree into an array object

2022-07-06 06:20:00 Don't sleep I


problem :

el-tree Render according to the structure of the array object , When we check the desired data, only an array will be generated


Desired effect :

Get the selected data ( Array ), According to the father A Inferior son a Structure , Perform data structure conversion corresponding to parent-child level


My data structure :

 Insert picture description here


Post code : Direct copy , Change the variable name to use

<el-tree
     :data="collectionListData"
      show-checkbox
      node-key="id"
      @check="checkFn"
      ref="tree"
      :props="defaultProps">
 </el-tree>
 //  In the following code devices Is my child array data , Remember to change 
checkFn(data, node) {
    
      let nodearr = [];
      if (!node.halfCheckedKeys.length) {
    
        node.checkedNodes.forEach((item) => {
    
          if (item.devices) {
    
            nodearr.push(item);
          }
        });
      } else {
    
        let nodeCheckedNodes = node.checkedNodes;
        let nodeHalfCheckedNodes = node.halfCheckedNodes;

        nodeHalfCheckedNodes.forEach((item) => {
    
          let assign = Object.assign({
    }, item);
          assign.devices = [];
          let childrenLength = item.devices;

          for (let i = 0; i < nodeCheckedNodes.length; i++) {
    
            if (nodeCheckedNodes[i].devices) {
    
              nodearr.push(nodeCheckedNodes[i]);
            } else {
    
              for (let j = 0; j < childrenLength.length; j++) {
    
                if (nodeCheckedNodes[i].id != childrenLength[j].id) {
    
                  continue;
                } else {
    
                  assign.devices.push(childrenLength[j]);
                }
              }
            }
          }
          nodearr.push(assign);
        });
      }
      this.nodeList = nodearr;
    },
原网站

版权声明
本文为[Don't sleep I]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060604444370.html