亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Angular 中打印所選行的行號

在 Angular 中打印所選行的行號

Go
紅顏莎娜 2023-08-21 15:05:23
我正在使用 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()"因此,當我選中任何特定行中的復選框時,就會調用該函數。但該函數的輸出是所選的行號:這意味著不顯示實際的行號。我計劃在此之后實現的功能將需要行號或所選行中的某種值。誰能告訴我為什么沒有檢索到行號信息?下面是表的屏幕截圖和行選擇的輸出。
查看完整描述

1 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

可以參考下面的代碼,


? <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;

? ?}



?}

}

查看完整回答
反對 回復 2023-08-21
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號