Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular-如何在Angular中隐藏mat表的特定行_Angular_Typescript - Fatal编程技术网

Angular-如何在Angular中隐藏mat表的特定行

Angular-如何在Angular中隐藏mat表的特定行,angular,typescript,Angular,Typescript,我想在单击按钮时隐藏表的特定列。下面是屏幕截图和示例代码。单击“隐藏权重”按钮时,我想隐藏/删除权重列。另一方面,当我点击按钮ShowWeight时,我想显示weight列 临时文件: <form class="example-form"> <mat-form-field class="example-full-width"> <mat-label>Elements</mat-label>

我想在单击按钮时隐藏表的特定列。下面是屏幕截图和示例代码。单击“隐藏权重”按钮时,我想隐藏/删除权重列。另一方面,当我点击按钮ShowWeight时,我想显示weight列

临时文件:

<form class="example-form">
    <mat-form-field class="example-full-width">
      <mat-label>Elements</mat-label>
      <input matInput placeholder="search element here..." (keyup)="applyFilter($event)">
    </mat-form-field>

<button>hide weight</button>
 <button>show weight</button>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  
    <!-- 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;"></tr>

    <tr class="mat-row" *matNoDataRow>
      <td class="mat-cell" colspan="4">No data matching the filter "{{input.value}}"</td>
    </tr>
  </table>
  <!-- </div> -->





  

最简单的方法是让按钮改变
displayedColumns
变量的内容

更新按钮以调用函数


我尝试了这个方法,但不起作用,这个方法只隐藏列一毫秒如果是这样的话,您可能有一些设置displayedColumns回到它的默认值。如果可以找到管理此变量的方法,那么这是显示/隐藏列的最佳方法。
import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
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'},
];
@Component({
  selector: 'app-products',
  templateUrl: './products.component.html',
  styleUrls: ['./products.component.scss']
})
export class ProductsComponent implements OnInit {
  searchedResult:string="";
  dataSource = new MatTableDataSource(ELEMENT_DATA);
  constructor() { }

  ngOnInit(): void {
  }
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  // dataSource = ELEMENT_DATA;

  applyFilter(event: Event) {
    const filterValue = (event.target as HTMLInputElement).value;
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }
}
<button (click)="hideWeight()">hide weight</button>
<button (click)="showWeight()">show weight</button>
hideWeight() {
     this.displayedColumns = ['position', 'name', 'symbol'];
}

showWeight() {
     this.displayedColumns = ['position', 'name', 'weight', 'symbol'];
}