Sunday, 9 June 2019

unable to select all checkboxes in tree using angular2-tree on init

Objective : i have a button named "feed data" so when ever i click it the data will be loaded i mean the tree with checkboxes here my requirement is when ever i click it along with data all the check boxes have to be checked on init i tried using

 this.treeComp.treeModel.doForAll((node: TreeNode) => node.setIsSelected(true));

but it is not working below is my code

 click(tree: TreeModel) {

    this.arrayData = [];

    let result: any = {};
    let rs = [];

    console.log(tree.selectedLeafNodeIds);





    Object.keys(tree.selectedLeafNodeIds).forEach(x => {


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


      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;
    tree.selectedLeafNodeIds = {};

  }

  selectAllNodes() {
    this.treeComp.treeModel.doForAll((node: TreeNode) => node.setIsSelected(true));
    //  firstNode.setIsSelected(true);

  }

  onTreeLoad(){
    console.log('tree');
  }



  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;


  }

  onActivate(event) {
    this.selectedDataList.push(event.node.data);
    console.log(this.selectedDataList)
  }

  onDeactivate(event) {
    const index = this.selectedDataList.indexOf(event.node.data);
    this.selectedDataList.splice(index, 1);
    console.log(this.selectedDataList)
  }

below is my stackblitz https://stackblitz.com/edit/angular-hrbppy



from unable to select all checkboxes in tree using angular2-tree on init

No comments:

Post a Comment