javascript - 从切换列表中获取值

标签 javascript angular ionic-framework toggle

我不知道如何从切换列表中检索值。每个切换代表学生是否在类。

下面有一个提交按钮,按下该按钮时,会将那些开关上值为“false”的学生发送到数据库中。

我不知道如何获取这些信息,使用 Ionic Angular 似乎很棘手。

我尝试向 .ts 文件上的函数添加 if 函数,但它不喜欢我添加的“值”。

<ion-list>
    <ion-item *ngFor="let student of students; let i = index">
      <ion-label>{{ student.nom }}</ion-label>
      <ion-toggle (ionChange)="onChange($event, i)">
      </ion-toggle>
    </ion-item>
    <ion-button (click)="signalAbsence($event, i)">
      Confirmer
    </ion-button>
  </ion-list>

onChange(event, i) {
if (event.target.checked) {
  // this.buttonOn(event, i);
  this.students.value = true;
  console.log(this.students.value, i);
}
else {
  // this.buttonOff(event, i)
  this.students.value = false;
  console.log(this.students.value, i);
}

}

signalAbsence(event, i) {
  event.preventDefault();
//  loop through students to find which have false value, add to array ??
    if (this.students[i].value === false) {
      const date = this.students[0].date;
      const time = this.students[0].time;
      const cours = this.students[0].cours;
      const lieux = this.students[0].lieux;
      const etudiant_nom = this.students[0].nom;
      const etudiant_id = this.students[0].id;
      const url = window.location.href;
      const id = url.substring(url.lastIndexOf('/') + 1);
      this.Auth.updateAbsenceDb(date, time, cours, lieux, id, etudiant_nom, 
    etudiant_id);
      alert("Mis à jour effectué");
      this.router.navigate(['cours/', id]);
} else {
      const url = window.location.href;
      const id = url.substring(url.lastIndexOf('/') + 1);
      alert("Mis à jour effectué");
      this.router.navigate(['cours/', id]);
  }
}

所以我需要知道哪些学生有错误值,然后如何使用该信息并使用此代码底部的 signalAbsence 函数将其发送到数据库。

最佳答案

还有另一种更简单的方法。 在 signalAbsence(event,i) 中,您可以添加一个数组并将真值推送到该数组中,并将假值推送到另一个数组中,然后将您想要的值推送到数据库中。

关于javascript - 从切换列表中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57199914/

相关文章:

html - Ionic : ion-content + ion-list, 如何将静态 div 和可滚动列表放在一起?

javascript - 无法访问 ng-init 值

javascript - 如何在 Rails 中安装 jschr 的 bootstrap-modal

javascript - 使用php和mysql的ajax搜索功能

javascript - 区域.js : 140 Uncaught TypeError: Cannot read property 'remove'

Angular 2继承启用禁用 ionic 菜单滑动

Angular(ng 测试)调试 - VS 代码不会在断点处停止

javascript - 如何从 event.target 获取元素的 ID

node.js - 用于在 ionic 应用程序中控制 Bot Framework UX 的选项

android - Ionic 2 安卓性能