angular - Ionic 4 检查按钮显示多个选择值
问题描述
我需要显示带有选择选项的列表,所以我使用检查按钮。但问题是我可以选择多个值,我需要在控制台中显示所有选定的值这是我的代码
<ion-list (ionChange)="checkValue($event)">
<ion-item *ngFor="let usr of usersList" >
<ion-label>{{usr.username}}</ion-label>
<ion-checkbox slot="start" value={{usr.userId}} ></ion-checkbox>
</ion-item>
</ion-list>
.ts
checkValue(event){
console.log(event.detail.value);
}
代码中的问题是它不能选择多个值,我需要在控制台中显示类似数组。
解决方案
您需要在后台维护一个数组,将所选项目添加到其中并从中删除取消选择的项目。这将用于添加项目:
selectedItems: any;
checkValues(event) {
console.log(event.detail.value);
if(!this.selectedItems) this.selectedItems = new Array();
this.selectedItems.push(event.detail.value);
}
要在未选择时删除项目,请循环遍历 selectedItems 数组以查找匹配项(使用过滤器或 for 循环)并在找到时拼接出该值。
推荐阅读
- ajax - Laravel 使用 ajax 进行评论
- jquery - 选择的 jQuery 逆
- neo4j - 添加关系时neo4j增加计数
- mapreduce - Apache Crunch:如何设置多个输入路径?
- callback - chromecast 中的视频标签不会触发“等待”事件
- html - Bootstrap - 为反向行模式创建移动适配
- vue.js - 在 vue 中使用 props 发送数据
- asp.net-mvc - 是否可以在 MVC 中将 ViewBag 值从一个页面保留到另一个页面?
- html - 使用 Django,如何在我的视图中静默提交表单而不返回 HTTP 响应,以便在调用视图函数时不重新加载网页
- excel - VBA 访问数据透视表编码