在 Angular 中打印所选行的行号

我正在使用 Angular 8 构建一个单页应用程序。我有一个表,其值现在硬编码在 .ts 文件中,如下所示:


 displayedColumns = ['select', 'position', 'name', 'weight', 'symbol'];

  dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);

  selection = new SelectionModel<Element>(true, []);




const ELEMENT_DATA: Element[] = [

  {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'},

];

我在 .ts 文件中编写了一个函数来输出所选行,如下所示:


selectedRow() {


        const rowNumber = this.selection.selected;

        console.log("row number that is selected : " + rowNumber);

    }

然后,html 文件实现代码来显示该表以及复选框,如下所示:


<div class="example-container mat-elevation-z8">

  <mat-table #table [dataSource]="dataSource">


    <!-- Checkbox Column -->

    <ng-container matColumnDef="select">

      <mat-header-cell *matHeaderCellDef>

        <mat-checkbox (change)="$event ? masterToggle() : null"

                      [checked]="selection.hasValue() && isAllSelected()"

                      [indeterminate]="selection.hasValue() && !isAllSelected()">

        </mat-checkbox>

      </mat-header-cell>

      <mat-cell *matCellDef="let row">

        <mat-checkbox (click)="$event.stopPropagation(); selectedRow()"

                      (change)="$event ? selection.toggle(row) : null"

                      [checked]="selection.isSelected(row)">

        </mat-checkbox>

      </mat-cell>

    </ng-container>


    <!-- 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>



现在应用程序中会显示一个行表。SelectedRow() 函数在以下行中实现


<mat-checkbox (click)="$event.stopPropagation(); selectedRow()"

因此,当我选中任何特定行中的复选框时,就会调用该函数。但该函数的输出是所选的行号:这意味着不显示实际的行号。我计划在此之后实现的功能将需要行号或所选行中的某种值。谁能告诉我为什么没有检索到行号信息?下面是表的屏幕截图和行选择的输出。

https://img4.mukewang.com/64e30cdc0001963005840441.jpg

红颜莎娜
浏览 101回答 1
1回答

芜湖不芜

可以参考下面的代码,  <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" --><ng-container matColumnDef="select">      <mat-header-cell *matHeaderCellDef>        <mat-checkbox (change)="selectAll()"                      [checked]="allSelected"                      [indeterminate]="!allSelected && oneSelected">        </mat-checkbox>       </mat-header-cell>      <mat-cell *matCellDef="let element">        <mat-checkbox (change)="change(element)" [checked]="element.selected">        </mat-checkbox>      </mat-cell>    </ng-container>  <!-- 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></table>打字稿export interface PeriodicElement {  name: string;  position: number;  weight: number;  symbol: string;  selected?:boolean}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 {  displayedColumns: string[] = ['select','position', 'name', 'weight', 'symbol'];  dataSource = ELEMENT_DATA  oneSelected=false;  allSelected=false; change(element:PeriodicElement){   let e= this.dataSource.find(item=>item.position===element.position);   if(e){      e.selected=true;   }   this.oneSelected=this.dataSource.filter(item=>item.selected).length>0 } selectAll(){   if(this.allSelected || !this.oneSelected){   this.dataSource.forEach(item=>item.selected= false);     this.allSelected=true;   }else{     this.dataSource.forEach(item=>item.selected= true);     this.allSelected=false;     this.oneSelected=false;   } }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go