Cannot set Angular Material matColumnDef property

主宰稳场 提交于 2021-02-08 11:52:38

问题


I have a datatable and want to change the value for matColumnDef property. However, I cannot change it when using it with [matColumnDef] as shown below:

<ng-container *ngIf="hasButton" [matColumnDef]="action">

On th other hand, when change its value without bracket matColumnDef, the related part is not rendered on the page. So, what is the difference between [matColumnDef] and matColumnDef? And why I cannot set it by free value?

<ng-container *ngIf="hasButton" matColumnDef="action">

回答1:


I am not exactly sure how your table is setup, but I could get it working in this generic example

Controller

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: "Hydrogen", weight: 1.0079, symbol: "H" },
  { position: 2, name: "Helium", weight: 4.0026, symbol: "He" },
  { position: 3, name: "Lithium", weight: 6.941, symbol: "Li" },
  { position: 4, name: "Beryllium", weight: 9.0122, symbol: "Be" },
  { position: 5, name: "Boron", weight: 10.811, symbol: "B" },
  { position: 6, name: "Carbon", weight: 12.0107, symbol: "C" },
  { position: 7, name: "Nitrogen", weight: 14.0067, symbol: "N" },
  { position: 8, name: "Oxygen", weight: 15.9994, symbol: "O" },
  { position: 9, name: "Fluorine", weight: 18.9984, symbol: "F" },
  { position: 10, name: "Neon", weight: 20.1797, symbol: "Ne" }
];

/**
 * @title Basic use of `<table mat-table>`
 */
@Component({
  selector: "table-basic-example",
  styleUrls: ["table-basic-example.css"],
  templateUrl: "table-basic-example.html"
})
export class TableBasicExample {
  action: string;
  displayedColumns: string[] = ["position"];
  dataSource = ELEMENT_DATA;

  showColumn(column: string) {
    this.action = column;
    this.displayedColumns = ["position", column];
  }
}

Template

<button md-raised-button color="primary" (mouseup)="showColumn('name')">
  Show Name
</button>
<button md-raised-button color="primary" (mouseup)="showColumn('weight')">
  Show Weight
</button>
<button md-raised-button color="primary" (mouseup)="showColumn('symbol')">
  Show Symbol
</button>

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  <!--- 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]="action">
    <th mat-header-cell *matHeaderCellDef>{{ action }}</th>
    <td mat-cell *matCellDef="let element">{{element[action]}}</td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

Working example: Stackblitz

Important thing to note is to modify the displayedColumns variable according to the columns to be displayed.



来源:https://stackoverflow.com/questions/64367386/cannot-set-angular-material-matcolumndef-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!