如何使用angular2使用angular2-tree插件获取检查的树结构值

时间:2018-12-11 17:23:28

标签: javascript angular typescript

在这里,我正在使用json和angular2-树组件生成动态树结构,直到一切都好为止,当我们选择必须选择特定名称的事件时,我无法生成选择事件广告对象,如果孩子在那儿,我尝试了此URL,并且在文档中也没有找到任何方法来获取所选择的值,因此,请提出建议。

https://angular2-tree.readme.io/docs

下面是我的代码

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

        }
      }
    },
    "link": {

    },
    "name": {

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

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

        }

      }
    }
  }
};

check(){
  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;
}

.html代码

<button type="button" (click)="check()">click</button>

<hr>

<input id="filter" #filter (keyup)="tree.treeModel.filterNodes(filter.value)" placeholder="filter nodes" />
<button (click)="tree.treeModel.clearFilter()">Clear Filter</button>
<tree-root #tree [focused]="true" [options]="options" [nodes]="nodes"></tree-root>

stackblitz链接

https://stackblitz.com/edit/angular-kh28sg

2 个答案:

答案 0 :(得分:2)

我不知道是否有更好的方法可以使用tree.treeModel.selectedLeafNodeIds访问选定的节点。您必须先检查是否已选中 参见docs/API

例如,如果您有一个按钮

 <!--I like pass as argument the property "treeModel" of #tree ("reference variable")-->
<button (click)="click(tree.treeModel)">sendData</button>
<tree-root #tree [focused]="true" [options]="options" [nodes]="nodes"></tree-root>

您的功能点击可以像

click(tree:TreeModel)
{
  console.log(tree.activeNodes);
    Object.keys(tree.selectedLeafNodeIds).forEach(x=>{
      let node:TreeNode=tree.getNodeById(x);
      if (node.isSelected)
      {
         console.log("Selected:",node.data.name,
                     "Parent:",node.parent.data.name);
      }
  }) 
}

注意:我分叉了your stackblitz

已更新,创建带有响应的对象

click(tree: TreeModel) {
    console.log(tree.activeNodes);
    let result: any = {} //<--declare a variable
    Object.keys(tree.selectedLeafNodeIds).forEach(x => {
      let node: TreeNode = tree.getNodeById(x);
      if (node.isSelected) {
        console.log("Selected:", node.data.name,
          "Parent:", node.parent.data.name);
        if (node.parent.data.name) //if the node has parent
        {
          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
        }
      }
    })
    console.log(result);
  }

答案 1 :(得分:0)

我无法获得可接受的答案,我的tree.getNodeById(x)将始终返回null

所以我使用了动作映射:

将IActionMapping添加到您的Tree组件

actionMapping: IActionMapping = {
    // ...
    checkboxClick: (tree, node) => {
        node.data.checked = !node.data.checked;
        this.setCheckedNodes(node.id, node.data.checked); 
      }

 }

将值存储在服务中的方法:

private setCheckedNodes(id: string, checked: boolean) {
    if (!this.treeService.selectedIds) {
      this.treeService.selectedIds = new Array<[string, boolean]>();
    }

    const checkedNode = this.treeService.selectedIds.find(cn => cn[0] === id);
    if (checkedNode) {
      if (checkedNode[1] !== checked) {
        checkedNode[1] = checked;
        this.treeService.selectedIds[id] = checkedNode;
      }
    } else {
      this.treeService.selectedIds.push([id, checked]);
    }

  }

然后在某些select事件上获取或发出存储在服务中的值:

export class treeService {
    // ...
    public selectedIds: Array<[string, boolean]>;
  }

可能想在以后清除它们