无法将多个值/数据推送到角度2树

时间:2019-01-22 10:01:22

标签: javascript angular typescript

我有一个动态json数据和3个按钮“ feed data”,它包含一组动态数据和按钮2“ feed另一个数据”,它包含另一组动态数据,并且按钮3是“发送数据”,这是我的问题每当我按提要数据并选择选项并按发送数据时,它便开始工作,之后,如果我按提要另一个数据并按发送数据,则出现错误“ isSelected is undefined”

错误模拟:提要数据->选择选项->发送数据并按提要提供其他数据->选择选项->发送数据,那么我们会出错

  options = {
    useCheckbox: true
  };
  arrayData;
 nodes;
  data = {
  "info": {
    "laptop": {
    },
    "config": {
      "properties": {
        "ram": {
        },
        "processor": {
        },
        "hdd": {

        }
      }
    },
    "link": {

    },
    "name": {

    },
    "company": {
      "properties": {
        "model": {

        },
        "maker": {
          "type": "integer"
        },
        "country": {
          "type": "text"
        },
        "enterprise": {

        }

      }
    }
  }
};

 dataa = {
  "info": {
    "mobile": {
    },
    "config": {
      "properties": {
        "ram": {
        },
        "processor": {
        },
        "storage": {

        }
      }
    },
    "link": {

    },
    "name": {

    },
    "company": {
      "properties": {
        "model": {

        },
        "maker": {
          "type": "integer"
        },
        "country": {
          "type": "text"
        },
        "enterprise": {

        }

      }
    }
  }
};
click(tree:TreeModel)
{  

  this.arrayData= [];
  console.log(tree.activeNodes);
    let result: any = {}; 
     let rs = [];
    // tree.selectedLeafNodeIds = {};
    Object.keys(tree.selectedLeafNodeIds).forEach(x=>{

      let node:TreeNode=tree.getNodeById(x);


      if (node.isSelected)
      {  

        if (node.parent.data.name) //if the node has parent
          {
            rs.push(node.parent.data.name+'.'+node.data.name);
            if (!result[node.parent.data.name]) //If the parent is not in the object
              result[node.parent.data.name] = {} //create
            result[node.parent.data.name][node.data.name] = true;

          }
          else {
            if (!result[node.data.name]) //If the node is not in the object
              result[node.data.name] = {} //create
              rs.push(node.data.name);
          }


      }
  }) 
  this.arrayData = rs;

}


feedData(){
   const results = Object.keys(this.data.info).map(k => ({
  name: k,
  children: this.data.info[k].properties
    ? Object.keys(this.data.info[k].properties).map(kk => ({name: kk}))
    : []
}));

 this.nodes = results; 

}

feedAnother(){
  const results = Object.keys(this.dataa.info).map(k => ({
  name: k,
  children: this.dataa.info[k].properties
    ? Object.keys(this.dataa.info[k].properties).map(kk => ({name: kk}))
    : []
}));

 this.nodes = results;
}

当我按提要数据并按发送数据时,我便能够查看信息并说谎,我想更新数据我按提要另一个数据然后按发送数据,然后它需要获取更新的结果

下面是我的堆栈闪电网址:https://stackblitz.com/edit/angular-zapxko

1 个答案:

答案 0 :(得分:0)

您只需更改nodes对象即可更新结果。

您需要使用Async Data方法来更新节点。这是Angular Tree的官方文档中的democode