0

在此处输入图像描述我在这里看到了类似的讨论。我认为我的独特之处足以添加为新线程。我正在处理每列都有下拉列表的表格。我已使用此处的信息来实现列的“全选”功能。我遇到的问题是“selectall”功能仅适用于第一列。这个问题有解决办法吗?

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

const ELEMENT_DATA: Array<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-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  dataSource: Array<any>;
  columnDropdownOptions: Array<any> = [];
  @ViewChild('mySel') skillSel!: MatSelect;
  allSelected: boolean = false;





constructor() {
  this.dataSource =  ELEMENT_DATA;  
}
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];

  dropDown(column:string) {
    console.log(column)
    this.columnDropdownOptions = []
    let property = column
    let rows = this.dataSource;
    console.log(rows);

    for(var index in rows) {
      this.columnDropdownOptions.push(rows[index][property])
    }
    console.log(this.columnDropdownOptions)
  }


  toggleAllSelection() {
    this.allSelected = !this.allSelected;

    if (this.allSelected) {
      this.skillSel.options.forEach( (item : MatOption) => item.select());
    } else {
      this.skillSel.options.forEach( (item : MatOption) => {item.deselect()});
    }
  }
}
 <table  mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  
  <ng-container *ngFor="let column of displayedColumns" [matColumnDef]="column">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ column }} 
      <mat-select #mySel (click)="dropDown(column)" multiple>
        <mat-checkbox (click)="toggleAllSelection()" style="padding-left: 17px;">Select all</mat-checkbox>
        <mat-option *ngFor="let option of columnDropdownOptions" [value]="option">{{option}}</mat-option>
      </mat-select>
    </th>
    <td mat-cell *matCellDef="let element"> {{ element[column] }} </td>
  </ng-container>

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

0 回答 0