Angular5 需要函数 check/uncheck mat-checkbox inside mat-table
Angular5 Need function to check/uncheck mat-checkbox inside mat-table
我可以在 table 中获取复选框以在 check/uncheck 上发出更改,但是在单击地图图钉以切换复选框状态时我遇到了往复运动问题。
my table with map
这是我的 table:
<mat-table [dataSource]="dataSource">
<ng-container matColumnDef="number">
<mat-header-cell *matHeaderCellDef> Number </mat-header-cell>
<mat-cell *matCellDef="let stock">
// #myCheckbox needs to be unique
<mat-checkbox #myCheckbox [checked] (change)="selectStock(stock, $event)"></mat-checkbox> <a href="">{{ stock.number }}</a>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="clickRow(row._id,$event)"></mat-row>
</mat-table>
然后在我的地图上,当您单击图钉时,运行 一些功能
(click)="someFunction(myCheckbox)"
在我的class
@ViewChild('myCheckbox') private myCheckbox: MatCheckbox;
someFunction(myCheckbox){
if (stock.isChecked) {
this.myCheckbox.checked = false;
} else {
this.myCheckbox.checked = true;
}
}
这是我正在处理的示例,但它对每个复选框应用相同的#id,因此只有第一个复选框被切换(我假设我需要为每个复选框设置唯一的唯一 ID?)
在网站上进行更多搜索后,我能够使用 ViewChildren 解决问题
@ViewChildren('myCheckbox') private myCheckboxes : QueryList<any>;
someFunction(item, index){
let myCheckboxes = this.myCheckboxes.toArray();
if (item.isChecked) {
myCheckboxes[index].checked = true;
} else {
myCheckboxes[index].checked = false;
}
}
好东西
下面是一个完整的工作示例。在 class 实现中 TypeScript-File:
import { Component, OnInit, Inject, EventEmitter, Output, OnDestroy, QueryList, ViewChildren } from '@angular/core';
...
export class YourClassName implements OnInit, OnDestroy {
...
selectables : Array<string> = ['ah','be','ce'];
@ViewChildren('checkboxMultiple') private checkboxesMultiple : QueryList<any>;
...
selectionChange( event ) : void {
// get index from template loop or so. In this case the value
// of the mat-checkbox is an Array with the value as first and
// the loop index as second entry
let index = event.source.value[1];
let checkboxesArray = this.checkboxesMultiple.toArray();
checkboxesArray[index].checked = false;
// You could also loop over all checkboxes and check/uncheck
// them based on your logic
}
}
在你的模板文件中:
...
<mat-checkbox #checkboxMultiple
*ngFor="let selectable of selectables; let i = index"
(change)="selectionChange($event)"
[value]="[selectable, i]">
{{ selectable }}
</mat-checkbox>
我发现您必须使用 QueryList 对象的 toArray() 方法来获取实际的复选框数组。
我可以在 table 中获取复选框以在 check/uncheck 上发出更改,但是在单击地图图钉以切换复选框状态时我遇到了往复运动问题。
my table with map
这是我的 table:
<mat-table [dataSource]="dataSource">
<ng-container matColumnDef="number">
<mat-header-cell *matHeaderCellDef> Number </mat-header-cell>
<mat-cell *matCellDef="let stock">
// #myCheckbox needs to be unique
<mat-checkbox #myCheckbox [checked] (change)="selectStock(stock, $event)"></mat-checkbox> <a href="">{{ stock.number }}</a>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="clickRow(row._id,$event)"></mat-row>
</mat-table>
然后在我的地图上,当您单击图钉时,运行 一些功能
(click)="someFunction(myCheckbox)"
在我的class
@ViewChild('myCheckbox') private myCheckbox: MatCheckbox;
someFunction(myCheckbox){
if (stock.isChecked) {
this.myCheckbox.checked = false;
} else {
this.myCheckbox.checked = true;
}
}
这是我正在处理的示例,但它对每个复选框应用相同的#id,因此只有第一个复选框被切换(我假设我需要为每个复选框设置唯一的唯一 ID?)
在网站上进行更多搜索后,我能够使用 ViewChildren 解决问题
@ViewChildren('myCheckbox') private myCheckboxes : QueryList<any>; someFunction(item, index){ let myCheckboxes = this.myCheckboxes.toArray(); if (item.isChecked) { myCheckboxes[index].checked = true; } else { myCheckboxes[index].checked = false; } }
好东西
下面是一个完整的工作示例。在 class 实现中 TypeScript-File:
import { Component, OnInit, Inject, EventEmitter, Output, OnDestroy, QueryList, ViewChildren } from '@angular/core';
...
export class YourClassName implements OnInit, OnDestroy {
...
selectables : Array<string> = ['ah','be','ce'];
@ViewChildren('checkboxMultiple') private checkboxesMultiple : QueryList<any>;
...
selectionChange( event ) : void {
// get index from template loop or so. In this case the value
// of the mat-checkbox is an Array with the value as first and
// the loop index as second entry
let index = event.source.value[1];
let checkboxesArray = this.checkboxesMultiple.toArray();
checkboxesArray[index].checked = false;
// You could also loop over all checkboxes and check/uncheck
// them based on your logic
}
}
在你的模板文件中:
...
<mat-checkbox #checkboxMultiple
*ngFor="let selectable of selectables; let i = index"
(change)="selectionChange($event)"
[value]="[selectable, i]">
{{ selectable }}
</mat-checkbox>
我发现您必须使用 QueryList 对象的 toArray() 方法来获取实际的复选框数组。