Angular Mat table checkbox select

本文档展示了如何在Angular应用中使用Mat Table组件实现复选框选择功能。通过`MatCheckbox`和`SelectionModel`,可以实现单选或多选元素,并提供了`isAllSelected`、`masterToggle`方法来控制全选和取消全选。同时,介绍了如何跟踪所选元素的属性,如名称和符号,并提供`updateChkBoxArrary`、`removeSelectedRows`和`printSelectedRows`方法来处理选中项的变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import { Component ,OnInit, ViewChild} from ‘@angular/core’;
import {SelectionModel} from ‘@angular/cdk/collections’;
import {MatPaginator} from ‘@angular/material/paginator’;
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-root’,
templateUrl: ‘./app.component.html’,
styleUrls: [’./app.component.css’]
})

export class AppComponent implements OnInit {
displayedColumns:string[]=[“select”,‘position’,‘name’,‘weight’,‘symbol’]
dataSource = new MatTableDataSource(Element_Data);

selection = new SelectionModel(true, []);

selectedName:string[]=[];
selectedSymbol:string[]=[];
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;

ngOnInit() {
const initialSelection = [];
const allowMultiSelect = true;
this.selection = new SelectionModel(allowMultiSelect, initialSelection);
this.dataSource.paginator = this.paginator;
}

logData(row){

console.log(row);
}

updateChkBoxArrary(row,isChecked){
if (isChecked)
{
this.selectedSymbol.push(row.symbol);
this.selection.toggle(row);
console.log(this.selectedSymbol);
}
else
{

}

}
removeSelectedRows() {
this.selection.selected.forEach(item => {

this.selectedName.push(item.name);
});
}
printSelectedRows(){

this.selection.selected.forEach(item=>{
this.selectedName.push(item.name);
});
console.log(this.selectedName)
}

/** Whether the number of selected elements matches the total number of rows. */
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}

/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}

/** The label for the checkbox on the passed row */
checkboxLabel(row?: PeriodicElement): string {
if (!row) {
return ${this.isAllSelected() ? 'select' : 'deselect'} all;
}
return ${this.selection.isSelected(row) ? 'deselect' : 'select'} row ${row.position + 1};
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值