【问题标题】:Ionic (ionChange) get selected index离子(ionChange)获取选定的索引
【发布时间】:2019-03-06 06:44:15
【问题描述】:

所以我知道如何使用 ionChange 事件获取选定的值,但是如何获取选定的索引。例如,如果我要在下拉列表中选择第三个值,我如何才能获得所选索引 (2) 而不是所选值?

【问题讨论】:

标签: ionic-framework ionic4


【解决方案1】:

你可以这样做:-

<ion-select placeholder="Select One">
      <ion-select-option value="id1">Value1</ion-select-option>
      <ion-select-option value="id2">Value2</ion-select-option>
</ion-select>

当您选择任何特定选项时,通过此操作,您将在其更改事件中获得该对象的 id 而不是控制器中的值。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    这是你可以做的来获取 id。将局部变量添加到您的 ion-select #selectedIndex。像这样添加您的价值后 [value]=[prices.w, id]

    <ion-select slot="end" (ionChange)="changePrice($event)" #selectIndex>
      <ion-select-option text-uppercase [value]="[prices.w, id]" *ngFor="let prices of prodData?.prices; let id = index" >{{prices.w}}</ion-select-option>
    </ion-select>
    

    在你的 ts.导入 ViewChild

    import { Component, OnInit, ViewChild } from '@angular/core';
    

    引用你的局部变量后

    @ViewChild('selectIndex') selectedId: ElementRef;
    

    然后声明您的 ionChange() 函数 changePrice(event) 并添加以下代码

    public changePrice(event) {
    const childCount = this.selectedId['el']['childElementCount'];
    this.selectedId['el']['childNodes'][`${childCount}`]['defaultValue'] = event.detail.value[0];
    this.selectedId['el']['shadowRoot']['children'][1]['innerHTML'] = event.detail.value[0];
    this.selectedId['el']['shadowRoot']['children'][1]['innerText'] = event.detail.value[0];
    console.log(event.detail.value[1]);
    }
    

    记录您的索引

    console.log(event.detail.value[1]);
    

    希望一切顺利

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-21
      • 2017-01-20
      • 2015-09-24
      • 2015-11-26
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多