【问题标题】:Ionic 3 How to select specific ion-chip on click event?Ionic 3 如何在点击事件中选择特定的离子芯片?
【发布时间】:2020-04-16 14:28:07
【问题描述】:

我有一堆由 ngFor 循环生成的离子芯片。我使用tagDefaultColor 变量添加了选择所有离子芯片功能。现在当我想选择单个离子芯片时,它会选择所有的。

我想要实现的是能够使用切换所有按钮来选择每个ion-chip 或通过点击事件一一选择它们。一旦选择了ion-chip,它的颜色就会变为主要颜色。提前谢谢你。

constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.information = navParams.data.data;
    this.children = [{ id: 1, name: 'Ginny Weasley' }, { id: 2, name: 'Harry Potter' }, { id: 3, name: 'Ronald Weasley' }, { id: 4, name: 'Luna Lovegood' }];
    this.selectButtonText = 'SELECT ALL';
    //this.tagSelectedColor = "primary";
    this.tagDefaultColor = "secondary";
    this.quantity = 0.0;
    this.shareWithFamily = true;
  }
selectAll() {
    if (this.selectButtonText === 'SELECT ALL') {
      this.selectButtonText = 'UNSELECT ALL'
      this.tagDefaultColor = "primary";
    } else {
      this.selectButtonText = 'SELECT ALL'
      this.tagDefaultColor = "secondary"
    }
  }
changeTagColor(event) {
    console.log(event.target);
    if (this.tagDefaultColor === "secondary") {
      this.tagDefaultColor = "primary"
    } else {
      this.tagDefaultColor = "secondary"
      event.target.setAttribute('color', 'secondary')
    }
  }

HTML 部分

<ion-item no-lines>
      <ion-row>
        <ion-col style="display: flex;align-items: center; justify-content: center">
          <strong>Tag Students</strong>
          <button ion-button full color="primary" class="select-all-btn" (click)="selectAll()">{{selectButtonText}}
          </button>
        </ion-col>
      </ion-row>
    </ion-item>

    <div class="students-tags">
      <ion-chip [id]="child.id" [color]="tagDefaultColor" (click)="changeTagColor($event)" *ngFor="let child of children">
        <ion-label>{{child. name}}</ion-label>
      </ion-chip>
    </div>

【问题讨论】:

    标签: angular ionic3


    【解决方案1】:

    您所有的芯片都绑定到相同的属性,因此如果您更改它们,它们都会改变。相反,您应该使用数组来单独分配它们,如下所示:

    <ion-chip [id]="child.id" [color]="tagDefaultColor[i]" (click)="changeTagColor(i)" *ngFor="let child of children;let i = index">
            <ion-label>{{child. name}}</ion-label>
          </ion-chip>
    
    changeTagColor(i:number) {
        console.log(event.target);
        if (this.tagDefaultColor[i] === "secondary") {
          this.tagDefaultColor[i] = "primary"
        } else {
          this.tagDefaultColor[i] = "secondary"
          // event.target.setAttribute('color', 'secondary') this is redundant
        }
      }
    

    编辑: 由于 ionic 遗憾地没有导出组件,因此您不能使用模板 ref 直接通过组件更改颜色。

    正如您在 cmets 中所问的,有一种简单的方法可以填充颜色数组,如下所示:

      tagDefaultColor = Array(this.children.length).fill("secondary");
    
    

    Here 是 Stackblitz 示例。

    【讨论】:

    • 感谢您的回答。然而,chip.color 为我返回 undefined。如果使用数组,我必须将 tagDefault 分配给 ['secondary', 'secondary', 'secondary', 'secondary'] 才能在单击后更改它?
    • 谢谢它工作得很好。现在我需要实现 select/deselct all 的功能。
    • @Eldar 你知道如何使用 ionic/vue 实现相同的功能吗?使用 vue3
    • @Verthon 您是否满足于手动分配 tagDefault?如果没有,您提出了什么解决方案?
    • @carlhandy 您可以使用 vue3 获得类似的解决方案。无论您是否使用组合 API,语法可能会有所不同。基本上说明您需要一个填充了默认颜色名称的数组。将您的芯片颜色绑定到该阵列的相应项目。单击时更改位置的颜色。
    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2017-09-26
    • 2015-07-14
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多