【问题标题】:Inserting component below mat table row only inserts into single column在垫表行下方插入组件仅插入单列
【发布时间】:2020-09-08 06:08:16
【问题描述】:

我希望将组件插入整行。 相反,组件似乎被插入到新行的第一列中。

这是 Angular 9 中可重现的示例:https://stackblitz.com/edit/angular-ithddx 我最初在 Angular 8 中使用 @angular/material 8.2.3 遇到了这个问题。

单击第一行会插入组件,但会扩展第一列,而不是使用创建的整个新行。

app.component.html

<div class="example-container mat-elevation-z8">
  <table mat-table #table [dataSource]="dataSource">

    <!--- Note that these columns can be defined in any order.
          The actual rendered columns are set as a property on the row definition" -->

    <!-- Position Column -->
    <ng-container matColumnDef="position">
      <th mat-header-cell *matHeaderCellDef> No. </th>
      <td mat-cell *matCellDef="let element"> {{element.position}} </td>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <th mat-header-cell *matHeaderCellDef> Weight </th>
      <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
    </ng-container>

    <!-- Symbol Column -->
    <ng-container matColumnDef="symbol">
      <th mat-header-cell *matHeaderCellDef> Symbol </th>
      <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns; let index = index" 
      (click)="insertComponent(index)"
      #tableRow>
    </tr>
  </table>
</div>

app.component.ts:

    import {Component, ViewChildren, ViewContainerRef, ComponentFactoryResolver, ComponentFactory, Input} from '@angular/core';
    import {MatTableDataSource} from '@angular/material/table';

    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      displayedColumns = ['position', 'name', 'weight', 'symbol'];
      dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);

      @ViewChildren('tableRow', { read: ViewContainerRef }) rowContainers;
      expandedRow: number;

      constructor(private resolver: ComponentFactoryResolver) { }

      insertComponent(index: number) {
        if (this.expandedRow != null) {
          // clear old content
          this.rowContainers.toArray()[this.expandedRow].clear();
        }

        if (this.expandedRow === index) {
          this.expandedRow = null;
        } else {
          const container = this.rowContainers.toArray()[index];
          const factory: ComponentFactory<any> = this.resolver.resolveComponentFactory(InlineMessageComponent);
          const inlineComponent = container.createComponent(factory);

          inlineComponent.instance.user = this.dataSource.data[index].name;
          inlineComponent.instance.weight = this.dataSource.data[index].weight;
          this.expandedRow = index;
        }
      }
    }

InlineMessageComponent.ts

/*
* The component that will be dynamically rendered between table rows
*/ 
@Component({
  selector: 'app-inline-message',
  template: '<p>Name: {{ user }} |</p><p>| weight {{ weight }}</p>',
  styles: [`
    :host {
      display: flex;
      place-content:center;
      padding: 24px;
      color: #555555;
      font-weight: bold;
      background: rgba(0,0,0,0.1);
    }
  `]
})
export class InlineMessageComponent {
  @Input() user: string;
  @Input() weight: string;
}

我期待与我关注的这个 Angular 5 示例相同的行为。 https://stackblitz.com/edit/angular-yxznk8-rongna

尝试过的事情:

  • &lt;tr mat-row&gt;&lt;mat-row&gt; 交换
  • 使用 MatTableDataSource 并插入新行
    • 这种方法很难为新行设置样式
    • 明显更多的代码
    • 即使为空也会显示新行

【问题讨论】:

    标签: angular typescript angular-material


    【解决方案1】:

    您需要做的就是将 HTML 放到您的标记中并坚持使用 Anguler 指令:

    <div class="example-container mat-elevation-z8">
      <mat-table #table [dataSource]="dataSource">
    
        <!--- Note that these columns can be defined in any order.
              The actual rendered columns are set as a property on the row definition" -->
    
        <!-- Position Column -->
        <ng-container matColumnDef="position">
          <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
          <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
        </ng-container>
    
        <!-- Name Column -->
        <ng-container matColumnDef="name">
          <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
          <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
        </ng-container>
    
        <!-- Weight Column -->
        <ng-container matColumnDef="weight">
          <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
          <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
        </ng-container>
    
        <!-- Symbol Column -->
        <ng-container matColumnDef="symbol">
          <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
          <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
        </ng-container>
    
        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns; let index = index" 
          (click)="insertComponent(index)"
          #tableRow>
        </mat-row>
      </mat-table>
    </div>

    更容易解析示例:

      <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    

    而不是

       <tr mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    

    【讨论】:

    • 我第一次尝试时一定没有正确更新它,因为它有效。谢谢!
    • 我的猜测是,您只尝试了一些元素:例如修复扩展行,但不是父行不起作用...
    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2021-04-12
    • 2014-02-24
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    相关资源
    最近更新 更多