gpt4 book ai didi

javascript - 如何在 Angular 和 Bootstrap 3.3.7 中应用父级和子级的复选框

转载 作者:行者123 更新时间:2023-12-03 00:35:11 25 4
gpt4 key购买 nike

这是我的要求:-

当我单击父元素时,它的子元素也会被选中,并且我们也可以取消选择我们不想要的内容。单击按钮,我们必须获取所选的 parent 和 child 的姓名 对象

到目前为止我做了什么:-

我创建了一个 parent 和 child 的列表,并为 parent 提供了复选框,并在选中n取消选中并获取值后获取 parent 值

我陷入困境,如何选择该父级中的子级并获取父级和子级的值

下面是我的代码

.ts

 @ViewChildren('myItem') item;
selectedIds = [];
display:any;
data:any;

ngOnInit(){


// dynamic json

this.data = {
"info": {
"laptop": {
},
"config": {
"properties": {
"ram": {
},
"processor": {
},
"hdd": {

}
}
},
"link": {

},
"name": {

},
"company": {
"properties": {
"model": {

},
"maker": {
"type": "integer"
},
"country": {
"type": "text"
},
"enterprise": {

}

}
}
}
};


// varible

this.display = this.data['info'];
console.log(this.display);

}

change(data,event){
if (event.target.checked === true) {
this.selectedIds.push({id: data, checked: event.target.checked});
console.log('Selected data ', JSON.stringify(this.selectedIds));
}
if (event.target.checked === false) {
this.selectedIds = this.selectedIds.filter((item) => item.id !== data);
}


}
getData(){
const filtered = this.selectedIds.filter(item => item.id)
console.log(JSON.stringify(filtered));
}

.html代码

<ul class="list-group" *ngFor="let parent of display | keyvalue">
<li class="list-group-item"> <input type="checkbox" [value]="parent.key" [(ngModel)]="parent.check" #myItem (change)="change(parent.key,$event)">{{parent.key}}</li>
<ng-container *ngIf="parent.check && parent.value.hasOwnProperty('properties')">
<ul *ngFor="let child of parent.value.properties | keyvalue">
<li>
{{child.key}}
</li>
</ul>
</ng-container>
</ul>

<hr>

<button class="btn btn-primary"(click)="getData()">get Data</button>

我的 stackblitz 链接

https://stackblitz.com/edit/angular-8vqwsc

parent and child

最佳答案

您唯一需要的是将“ child ”传递给您的函数

(change)="change(parent.key,parent.value.properties,$event)"

然后在你的函数中改变变得像

  change(data, children, event) {
if (event.target.checked === true) {
this.selectedIds.push({ id: data, checked: event.target.checked });
for (let child in children) {
this.selectedIds.push({ id: child, checked: event.target.checked });
}
}
if (event.target.checked === false) {
this.selectedIds = this.selectedIds.filter((item) => item.id !== data);
for (let child in children) {
this.selectedIds = this.selectedIds.filter((item) => item.id !== child);
}
}

显示所选值时出现问题。为此,您需要创建一个函数

  isChecked(child) {
let item = this.selectedIds.find(x => x.id == child.key)
return item ? item.checked : false
}

并添加(单击)到“子项”以更改 this.selected

  <ul *ngFor="let child of parent.value.properties | keyvalue">
<li>
<input type="checkbox" [checked]="isChecked(child)" (change)="change(child.key,null,$event)"/>
{{child.key}}
</li>
</ul>

但我认为有更好的方法来做这些事情。想象一下您的数据变成了

this.data2 =[ 
{
"id":"laptop","checked":false
},
{"id":"config","checked":false,"properties": [
{"id":"ram","checked":false},
{"id":"processor","checked":false},
{"id":"ram","checked":false}
]
},
{"id":"link","checked":false},
{"id":"name","checked":false},
{"id":"company","checked":false,"properties":[
{"id":"model","checked":false},
{"id":"maker","checked":false},
{"id":"country","checked":false},
{"id":"enterprise","checked":false}

]}
]

(我放置了已检查的属性,但这不是必需的)您的 .html 变得更容易

<ul *ngFor="let parent of data2">
<li><input type="checkbox" [(ngModel)]="parent.checked" (change)="changedChild(parent.checked,parent.properties)"> {{parent.id}}
<ng-container *ngIf="parent.checked">
<ul *ngFor="let child of parent.properties">
<li>
<input type="checkbox" [(ngModel)]="child.checked" />
{{child.id}}
</li>
</ul>
</ng-container>
</li>
</ul>

并且函数更改了Child

changedChild(value,children)
{
if (children)
children.forEach(x=>x.checked=value)
}

更新将对象添加到 this.selectedChild函数变为

change(data, children, event) {
if (event.target.checked === true) {
this.selectedIds.push({ id: data, checked: event.target.checked });
for (let child in children) {
this.selectedIds[this.selectedIds.length-1][child]=event.target.checked;

}
}
if (event.target.checked === false) {
this.selectedIds = this.selectedIds.filter((item) => item.id !== data);
}

}
changeChild(parentKey,childKey,event)
{
let item:any = this.selectedIds.find(x => x.id == parentKey)
if (event.target.checked)
item[childKey]=event.target.checked;
else
delete item[childKey];

}

isChecked(parentKey,childKey) {
let item:any = this.selectedIds.find(x => x.id == parentKey)
return item ? item[childKey] : false
}

和 html

<ul class="list-group" *ngFor="let parent of display | keyvalue">
<li class="list-group-item"> <input type="checkbox" [value]="parent.key" [(ngModel)]="parent.check" #myItem (change)="change(parent.key,parent.value.properties,$event)">{{parent.key}}</li>
<ng-container *ngIf="parent.check && parent.value.hasOwnProperty('properties')">
<ul *ngFor="let child of parent.value.properties | keyvalue">
<li>
<input type="checkbox" [checked]="isChecked(parent.key,child.key)" (change)="changeChild(parent.key,child.key,$event)"/>
{{child.key}}

</li>
</ul>
</ng-container>
</ul>

参见forked stackblitz

关于javascript - 如何在 Angular 和 Bootstrap 3.3.7 中应用父级和子级的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53693553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com