【问题标题】:How dynamicly check checkboxes with compare object and value?如何通过比较对象和值动态检查复选框?
【发布时间】:2017-08-17 09:36:37
【问题描述】:

我有一个这样的组列表:

{"data":
  [
    {"id":"221","title":"group1"},
    {"id":"18","title":"Group2"},
    {"id":"306","title":"Group3"},
    {"id":"19","title":"Group4"}
  ]
}

在模板中,我循环遍历上述对象的所有组并将它们显示如下:

<ion-select [(ngModel)]="user.group" multiple="true" name="group">
  <ion-option *ngFor="let group of groups" [value]="group.id" [selected]="ifIsInGroup(group.id)">
    {{ group.title }}
  </ion-option>
</ion-select>

在我的页面类中,我有一个对象,其中包含用户已分配到的组: { "mygroups":[{"title":"Group2","id":"18"}]}选择或不选择选项的功能:

ifIsInGroup(itemID){
  if(what to do here?){
    return true
  } else { 
    return false;
  }
}

如何检查数据(所有组)列表中是否存在 mygroups 项?并返回 true sow 选项被选中?

【问题讨论】:

    标签: angular filter


    【解决方案1】:

    如果我理解正确,您想检查所有组列表中的任何项目是否存在 itemId?

    您可以更新您的 ifIsInGroup 函数以使用 some 函数,该函数返回一个布尔值:

    this.allGroups.some(x => x.id == itemID;
    

    因此,如果列表中存在项目,则不要执行 if/else 语句来返回 true 或 false,而是将函数更新为:

    ifIsInGroup(itemID){
      return this.allGroups.some(x => x.id == itemID;  //returns true if it exists, false if it doesn't exist in the list
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多