angular - 在垫表行下方插入组件仅插入到单列中

标签 angular typescript angular-material

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

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

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

enter image description here

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 Working Example

尝试过的事情:

  • 交换<tr mat-row><mat-row>
  • 使用 MatTableDataSource 并插入新行
    • 此方法使新行的样式变得困难
    • 代码明显增多
    • 即使空行也会显示新行

最佳答案

您需要做的就是将 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>

enter image description here

关于angular - 在垫表行下方插入组件仅插入到单列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61937429/

相关文章:

angular - 在自动保存场景中以何种顺序使用哪些 rxjs 运算符

javascript - 尝试在 Angular2 中以 HTML 格式呈现 JSON

javascript - 为什么 'emit' 仅在我第一次单击按钮时不显示名称?

javascript - Typescript 提供的参数与调用目标的任何签名都不匹配

arrays - 数组到数组的 Observable(Rxjs)

typescript - 在 Angular Material 2 中创建自定义主题

javascript - 从日历上的点击日期开始在 md-dialog 中显示事件

javascript - Angular 2 : why use switchMap when retrieving route params?

css - Angular Material如何垂直对齐md-button

angular - 带有多个选项的 mat-select 中的样式复选框