JSON
[ {
"code" : "TEST",
"projectId" : "PROJECT",
"name" : "TEST",
"description" : "test",
"level1" : "1 ",
"level2" : "2 ",
"level3" : "3",
"level4" : "4",
"level5" : "5",
"active" : false
}, {
"code" : "test",
"projectId" : "PROJECT",
"name" : "Test",
"description" : "test 11",
"level1" : "1 ",
"level2" : "2 ",
"level3" : "3",
"level4" : "4",
"level5" : "5",
"active" : true
}, {
"code" : "asdasd2122",
"projectId" : "PROJECT",
"name" : "TEST12",
"description" : "ashdasdhl",
"level1" : "1 ",
"level2" : "2 ",
"level3" : "3",
"level4" : "4",
"level5" : "5",
"active" : false
}, {
"code" : "test1a",
"projectId" : "PROJECT",
"name" : "TEST122",
"description" : "a,sndkajlskdjlkajd",
"level1" : "1 ",
"level2" : "2 ",
"level3" : "3",
"level4" : "4",
"level5" : "5",
"active" : true
}, {
"code" : "dasdasd",
"projectId" : "PROJECT",
"name" : "test123123",
"description" : "asdasdasd",
"level1" : "1 ",
"level2" : "2 ",
"level3" : "3",
"level4" : "4",
"level5" : "5",
"active" : true
},
{
"code" : "asdasdTES",
"projectId" : "PROJECT",
"name" : "TEST123",
"description" : "asdasdasd",
"level1" : "1 ",
"level2" : "2 ",
"level3" : "3",
"level4" : "4",
"level5" : "5",
"active" : true
} ]
HTML
<mat-table [dataSource]="offersColumnRowData" matSort matSortActive="NAME">
<ng-container matColumnDef="SELECT">
<mat-header-cell *matHeaderCellDef> SELECT </mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-checkbox>
</mat-checkbox>
</mat-cell>
</ng-container>
<ng-container matColumnDef="NAME">
<mat-header-cell *matHeaderCellDef> NAME </mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.name }} </mat-cell>
</ng-container>
<ng-container matColumnDef="CODE">
<mat-header-cell *matHeaderCellDef> CODE </mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.code }} </mat-cell>
</ng-container>
<ng-container matColumnDef="DESCRIPTION">
<mat-header-cell *matHeaderCellDef> DESCRIPTION </mat-header-cell>
<mat-cell *matCellDef="let element"> {{ element.description}} </mat-cell>
</ng-container>
<ng-container matColumnDef="CREATEDATE">
<mat-header-cell *matHeaderCellDef> CREATEDATE </mat-header-cell>
<mat-cell *matCellDef="let element"> </mat-cell>
</ng-container>
<ng-container matColumnDef="UNAME">
<mat-header-cell *matHeaderCellDef> UNAME </mat-header-cell>
<mat-cell *matCellDef="let element"> </mat-cell>
</ng-container>
<ng-container matColumnDef="ISACTIVE">
<mat-header-cell *matHeaderCellDef> ISACTIVE </mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-checkbox type="checkbox">
</mat-checkbox>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="offersColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: offersColumns;" (mouseover)="row.hovered = true" (mouseout)="row.hovered = false"></mat-row>
</mat-table>
我正在尝试将值绑定到复选框!布尔值(TRUE | FALSE)形式的值
我尝试将with绑定到复选框,但是它不起作用
说明:转到我的堆栈闪电->如果我从SELECT列中选择了任何复选框,则应该选择代码列(我只想在控制台日志中打印选择列行CODE)
2)如何将布尔值绑定到如果active为false的列名称为ISACTIVE的复选框列中,如果active为true则不选中该复选框->选中复选框!
这是我的stackblits链接,对您来说很简单!
https://stackblitz.com/edit/angular-gbf9kz-a2s4zc?file=app%2Ftable-basic-example.ts
答案 0 :(得分:1)
根据要处理的复杂程度,可以采用表单方法(推荐),也可以在复选框中添加简单的更改处理程序。
组件
selectedCodes: string[] = [];
receiveCheckboxChange(code: string, value: MatCheckboxChange) {
console.log(code, value.checked);
this.updateCodes(code, value.checked);
}
updateCodes(code: string, selected: boolean) {
const inArrayIndex: number = this.selectedCodes.indexOf(code);
if (selected && inArrayIndex === -1) {
this.selectedCodes.push(code);
} else if (inArrayIndex !== -1) {
this.selectedCodes.splice(inArrayIndex, 1);
}
}
isSelected(code: string) {
return this.selectedCodes.indexOf(code) !== -1;
}
模板
<ng-container matColumnDef="ISACTIVE">
<mat-header-cell *matHeaderCellDef> ISACTIVE </mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-checkbox type="checkbox" (change)="receiveCheckboxChange(element.code, $event)" [checked]="isSelected(element.code)">
</mat-checkbox>
</mat-cell>
</ng-container>
这里的关键部分是(change)="receiveCheckboxChange(element.code, $event)"
-您可以从element
中提供所需的任何信息,或者甚至可以将整个element
对象本身提供给函数。然后,您可以根据自己的意愿去做。
然后,变量selectedCodes
中将具有一组选定的代码。从那里开始,您对他们的处理取决于您。
答案 1 :(得分:1)
<ng-container matColumnDef="SELECT">
<mat-header-cell *matHeaderCellDef> SELECT </mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-checkbox [(ngModel)]="element.Isselected" (change)="change()">
</mat-checkbox>
</mat-cell>
</ng-container>
在ts文件中
change() {
const result = this.offersColumnRowData.filter(x => x.Isselected == true);
console.log('result', result);
}
json中的选定属性
"Isselected": false,
"code" : "TEST",
"projectId" : "PROJECT",
"name" : "TEST",
"description" : "test",
"level1" : "1 ",
"level2" : "2 ",
"level3" : "3",
"level4" : "4",
"level5" : "5",
"active" : false
答案 2 :(得分:0)
使用[已选中]和(更改)显示是否为element.active = true并更改element.active的值。并使用条件运算符显示(或不显示)element.code的值
res/raw
更新:最好使用ngModel-而不是[选中]和(更改)-
<ng-container matColumnDef="CODE">
<mat-header-cell *matHeaderCellDef> CODE </mat-header-cell>
<mat-cell *matCellDef="let element">
{{ element.active?element.code:"" }}
</mat-cell>
</ng-container>
...
<ng-container matColumnDef="ISACTIVE">
<mat-header-cell *matHeaderCellDef> ISACTIVE </mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-checkbox type="checkbox" [checked]="element.active"
(change)="element.active=$event.checked">
</mat-checkbox>
</mat-cell>
</ng-container>
答案 3 :(得分:-1)
我更新了您的堆叠闪电战,请检查:https://stackblitz.com/edit/angular-gbf9kz-qg9fvw?file=app%2Ftable-basic-example.ts
使用[checked]="element.active"
在复选框html标记上绑定数据
使用(change)="change(element)"