【问题标题】:angular material tree to display organization structure角度材质树展示组织结构
【发布时间】:2020-02-06 05:26:21
【问题描述】:

我想使用以职位、薪水、服务年份作为属性的角度材料树来显示组织结构。

class Employee {
  name: string;
  position: string;
  salary: number;
  yearofServices: number;
  reports: Employee[];
 }

例如,

[ 
  {id: 1,
   name:'employee1',
   position: 'president',
   salary: 250000,
   yearofServices: 20,
   reports: [
    {
      id: 2,
      name:'employee2',
      position: 'manager',
      salary: 200000,
       yearofServices: 10,
    },
   {
      id: 3,
      name:'employee3',
      position: 'manager',
      salary: 190000,
       yearofServices: 15,
    }
   ];
]

我们要显示四列:

姓名 职位 薪金 YearOfService

名称列是根据组织报告层次结构的树结构。例如,如果一个经理有三个报告,则经理节点将有三个子节点。

是否可以使用角度材质树控制来做到这一点?

【问题讨论】:

  • 我发布了一个答案,但后来我觉得有点不清楚你在哪里问mat-tree 是为了显示一棵树,所以当然你可以有子节点,子节点有子节点等等。唯一的问题是你想在你的名字栏中显示什么?仅报告内的名称或所有信息。为什么需要列为什么不使用单个树结构?
  • @JSmith 我需要在类似结构的表格中显示一行中的每条记录。对于第一列,它看起来更像一个树结构。用户可以扩大或缩小行。

标签: angular angular-material treeview angular-cdk


【解决方案1】:

有可能,请在此处查看示例:https://stackblitz.com/edit/angular-m77g7e-semvxp?file=app%2Ftable-basic-example.html

引用于https://github.com/angular/components/issues/15187

另一种选择是拥有两个mat-tree 组件,一个用于遍历层次结构,另一个用于固定列。您可以使用treeControl.isExpanded(node) 来切换两个组件的可见性。 (这也适用于cdk-tree 组件。)

【讨论】:

    【解决方案2】:

    当然可以!它只是一个允许 HTML 的指令。

    我并没有一直存根,但它的要点非常简单:

      const TREE_DATA: Employee[] = [
      {
        name: 'Bob',
        position:'President of Lattes'    
      }, {
        name: 'Becky',
        position: 'Manager Of Latte Presidents',
        reports: [{
            name: 'Bob',
            position:'President of Lattes'  
          }, {
            name: 'Steve',
            position: 'President of Mocha-Locha'
          },
          {
            name: 'Arnie',
            position: 'President of Tepid-Teas',
            reports: [
               {
                  name: 'Mick',
                  position: 'Loose orders for loose leaf'
               },
               {
                  name: 'Michelle',
                  position: 'The First With The Green'
               }
            ]
          }
          ]
      },
    ];
    

    还有 HTML

    <mat-tree [dataSource]="dataSource" [treeControl]="treeControl" class="example-tree">
    
    
    <!-- This is the tree node template for leaf nodes -->
      <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
        <li class="mat-tree-node">
          <!-- use a disabled button to provide padding for tree leaf -->
          <button mat-icon-button disabled></button>
          <div class="container-fluid">
              <div class="row">
                  <div class="col">{{node.name}}</div>
                  <div class="col">{{node.position}}</div>
                  <div class="col">salary here</div>
                  <div class="col">Years of Service</div>
              </div>
            </div>
        </li>
      </mat-tree-node>
      <!-- This is the tree node template for expandable nodes -->
      <mat-nested-tree-node *matTreeNodeDef="let node; when: hasChild">
        <li>
          <div class="mat-tree-node ">
            <button mat-icon-button matTreeNodeToggle
                    [attr.aria-label]="'toggle ' + node.name">
              <mat-icon class="mat-icon-rtl-mirror">
                {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
              </mat-icon>
            </button>
            <div class="container-fluid">
              <div class="row">
                  <div class="col">{{node.name}}</div>
                  <div class="col">{{node.position}}</div>
                  <div class="col">salary</div>
                  <div class="col">0</div>
              </div>
            </div>
          </div>
          <ul [class.example-tree-invisible]="!treeControl.isExpanded(node)">
            <ng-container matTreeNodeOutlet></ng-container>
          </ul>
        </li>
      </mat-nested-tree-node>
    </mat-tree>
    

    要真正看到它,您可以查看StackBlitz

    【讨论】:

    • 问题是列没有垂直对齐。我使用左边距得到了类似的显示。
    • @user3097695 我同意这是一个开箱即用的问题。但是,您在原始问题中根本没有提出。请花点时间思考一下你实际问的是什么,而不是你的意思。我建议为此提出一个更完整的问题(因为看起来你的意思是别的)而不是移动你当前问题的目标帖子......(并在你认为合适的时候关闭/接受/删除这个)跨度>
    • 感谢您这么快提出解决方案。我对你的解决方案投了赞成票。但是,我仍然需要解决我的问题。
    • @user3097695 我明白了!我会问一个问题,如何将嵌套在树中的列与您当前的代码对齐。这样人们就可以尝试回答您的实际问题
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 2020-01-31
    • 2019-03-07
    • 2022-10-05
    • 2019-06-14
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多