我的打字稿代码如下。
selectedAll: any;
selectedAllSecondname: any;
this.name = [ {name: 'as', value: 'as', selected: false },
{name: 'bs', value: 'bs', selected: false },
{name: 'cs', value: 'cs', selected: false } ];
this.Secondname = [ {name: 'dd', value: 'dd', selected: false },
{name: 'ee', value: 'ee', selected: false },
{name: 'ff', value: 'ff', selected: false } ];
this.Thirdname = [ {name: 'gg', value: 'gg', selected: false },
{name: 'hh', value: 'hh', selected: false },
{name: 'ii', value: 'ii', selected: false } ];
selectAll(){
for(var i=0; i < this.name.length; i++) {
for(var j=0; j < this.Secondname.length; j++) {
this.name[i].selected = this.selectedAll;
this.Secondname[j].selected = this.selectedAllSecondname;
}
}
}
checkIfAllSelected(){
this.selectedAll = this.names.every(function (item: any) {
this.checkIfAllSecondnameSelected();
return item.selected == true;
})
}
因此,对于已声明的每个列表,我具有相同的功能,但名称不同。
My html:
<li><input type="checkbox" [(ngModel)] = "selectedAll" (change) = "selectAll();">Select All </li>
<li *ngFor = "let a of name">
<input type="checkbox" [(ngModel)]="a.selected" (change)="checkIfAllSelected()">
{{ a.name }}
<li>
<li><input type="checkbox" [(ngModel)] = "selectedAll" (change) = "selectAllSecondname();">Select All </li>
<li *ngFor = "let d of Secondname">
<input type="checkbox" [(ngModel)]="d.selected" (change)="checkIfAllSecondnameSelected()">
{{ d.name }}
<li>
因此,我要在这里执行的操作是,如果单击“全选”,则需要选中页面中的所有其他复选框。但是它只检查名字,而不检查名字和名字。
有人可以告诉我我在哪里出问题以及如何纠正吗?
答案 0 :(得分:1)
您可以使用以下示例
HTML
<ul>
<li> <input type="checkbox" [(ngModel)]="selectedAll" (change)="selectAll();"/>
</li>
<li *ngFor="let n of names">
<input type="checkbox" [(ngModel)]="n.selected" (change)="checkIfAllSelected();">
{{n.name}}
</li>
</ul>
TS
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title: String;
names: any;
selectedAll: any;
constructor() {
this.title = "Select all/Deselect all checkbox - Angular 2";
this.names = [
{ name: 'Prashobh', selected: false },
{ name: 'Abraham', selected: false },
{ name: 'Anil', selected: false },
{ name: 'Sam', selected: false },
{ name: 'Natasha', selected: false },
{ name: 'Marry', selected: false },
{ name: 'Zian', selected: false },
{ name: 'karan', selected: false },
]
}
selectAll() {
for (var i = 0; i < this.names.length; i++) {
this.names[i].selected = this.selectedAll;
}
}
checkIfAllSelected() {
this.selectedAll = this.names.every(function(item:any) {
return item.selected == true;
})
}
}
答案 1 :(得分:0)
请为selectAll
方法尝试以下代码:-
selectAll() {
for (var i = 0; i < this.name.length; i++) {
this.name[i].selected = this.selectedAll;
}
for (var i = 0; i < this.Secondname.length; i++) {
this.Secondname[i].selected = this.selectedAll;
}
for (var i = 0; i < this.Thirdname.length; i++) {
this.Thirdname[i].selected = this.selectedAll;
}
}
答案 2 :(得分:0)
只需将selectAll()中的“ this.selectedAllSecondname”更改为“ this.selectedAll”即可:
selectAll(){
for(var i=0; i < this.name.length; i++) {
this.name[i].selected = this.selectedAll;
}
for(var j=0; j < this.Secondname.length; j++) {
this.Secondname[j].selected = this.selectedAll;
}
for(var j=0; j < this.Thirdname.length; j++) {
this.Thirdname[j].selected = this.selectedAll;
}
}