【问题标题】:How to get all the information of the item from the ion select?如何从离子选择中获取项目的所有信息?
【发布时间】:2019-03-06 01:59:35
【问题描述】:

我从Firestore 加载项目列表并将它们放入离子选择中,问题是由于每个项目都有一个 id,我需要获取选择的 id。

这是我在控制器中获取项目的代码。

this.MesasColeccion = this.mesasser.getMesaListfromFirestore();
this.MesasColeccion.snapshotChanges().subscribe(mesasLista => {
  this.mesas  = mesasLista.map(mesa => {
    return {
      nombre: mesa.payload.doc.data().nombre,
      capacidad: mesa.payload.doc.data().capacidad,
      estado: mesa.payload.doc.data().estado,
      union:mesa.payload.doc.data().union,
      id: mesa.payload.doc.id
    }
  });

  this.mesas2  = mesasLista.map(mesa2 => {
    return {
      nombre: mesa2.payload.doc.data().nombre,
      capacidad: mesa2.payload.doc.data().capacidad,
      estado: mesa2.payload.doc.data().estado,
      union:mesa2.payload.doc.data().union,
      id: mesa2.payload.doc.id
    }
  });
});
this.MesasObservable = this.MesasColeccion.valueChanges();

这是在离子选择中显示列表的代码:

<ion-card>
<ion-card-content>
  <ion-item id="mesas-select1">
  <ion-label >
    Mesa #1:
  </ion-label>
  <ion-select  [(ngModel)]='Gruposobj.nombrem1' name="Mesa#1" placeholder="Selecciona la mesa"  >
    <ion-option   *ngFor="let mesa of mesas">
      {{mesa.nombre}}
    </ion-option>
  </ion-select>
  </ion-item>

  <ion-item id="mesas-select1">
    <ion-label >
      Mesa #2:
    </ion-label>
    <ion-select [(ngModel)]='Grupos2obj.nombrem2' name="Mesa#2" placeholder="Selecciona la mesa" >
      <ion-option *ngFor="let mesa2 of mesas2" >
        {{mesa2.nombre}}
      </ion-option>
    </ion-select>
    </ion-item>

ngmodel 将所选选项分配给 GruposModel 中的属性。在这种情况下,vinculacionmodel 的对象是 Gruposobj 和 Grupos2obj。当用户选择一个 ionic-select 的项目时,我需要从一个项目中获取所选的 id。因为我会将这个 mesa.id && mesa2.id 分配给带有 Gruposobj.mesa1 && Gruposobj.mesa2 的 vinculacionmodel。

这是vinculacionmodel:

export interface vinculacionmodel {
    
    id?:string;
    mesa1:string;
    mesa2:string;
    mesa3:string;
    nombrem1:string;
    nombrem2:string;
    nombrem3:string;
}

【问题讨论】:

    标签: firebase ionic-framework google-cloud-firestore ion-select


    【解决方案1】:

    如果您想从对象中获取 ID,您可以在您的 ion-select 上实现 ionChange 事件,并将获得选定的对象。

    详情请参考以下代码。

    <ion-select [(ngModel)]='Grupos2obj.nombrem2' name="Mesa#2" placeholder="Selecciona la mesa" (ionChange)="onSelectChange($event)">
    <ion-option *ngFor="let mesa2 of mesas2" [value]="mesa2['id']">
        {{mesa2.nombre}}
    </ion-option>
    </ion-select>
    

    .ts 文件中的 ionChange 事件:

    onSelectChange(selectedValue: any) {
        //Selected Value Id will get as param ==> selectedValue
        //Selected Object
        var item = this.mesas2.find(item => item['id'] === selectedValue);
        //Position of object in array
        var postion = this.mesas2.findIndex(item => item['id'] === selectedValue);
    }
    

    希望这将帮助您从 ios-select 获取您的 ID。

    【讨论】:

    • 感谢您的帮助!如果我需要在 ion-option 中使用 mesa2['capacidad'] 的其他值定义其他值??
    • 是的,您可以,但一次只能使用 ion-select 绑定一个值。
    • 但问题是我需要绑定其他值,当我打印项目(“console.log(item)”)时,结果是一个具有所有 mesasmodel 属性的结构,我如何访问项目['capacidad'],谢谢
    • 你可以像item['capacidad']一样直接访问它会给你提供的属性值。
    猜你喜欢
    • 2023-04-03
    • 2017-01-23
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2018-10-14
    • 2023-03-21
    相关资源
    最近更新 更多