簡體   English   中英

ionic3通過`radio`更改頁面數據

[英]ionic3 change page data by `radio`

當我單擊“ 1月”表示“一月”時,我希望ion-grid顯示一月的志願者數據。 console.log已經獲得了正確的數據,但是ion-grid沒有更新。

這是登錄Web瀏覽器

在此處輸入圖片說明

HTML頁面

<ion-header>
    <ion-toolbar color="danger">
      <ion-buttons>
        <button ion-button navPop icon-only>
          <ion-icon ios="ios-arrow-back" md="md-arrow-back"></ion-icon>
        </button>
      </ion-buttons>
      <ion-buttons style="background: transparent;" end> 
        <button style="background: transparent;" (click)="doRadio()">月份選擇</button>
      </ion-buttons>
        <ion-title text-wrap>志願者評選</ion-title>
    </ion-toolbar>
</ion-header>

<ion-content>

  <ion-grid>
    <ion-row>
      <ion-col>
        <ion-card (click)="goToVolunteerVoteDetail(volunteer)" style="width:26%" class="pin" *ngFor="let volunteers of volunteer">
          <img src="{{volunteers.Preview_image1}}" height="100">
          <div *ngIf="volunteers.title" class="volunteer-title">
            <small>{{volunteers.title}}</small>
          </div>
          <div class="volunteer-title">{{volunteers.like_number}}分</div> <!-- 志願者評選頭像像素:505x505px -->
        </ion-card>
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>

ts頁面

import { Component } from '@angular/core';
import { NavController, NavParams, ToastController, AlertController, ModalController } from 'ionic-angular';
import { NewsDataProvider } from '../../providers/news-data/news-data';
import { VolunteerVoteDetailPage } from '../volunteer-vote-detail/volunteer-vote-detail';


@Component({
  selector: 'page-volunteer-vote',
  templateUrl: 'volunteer-vote.html',
})
export class VolunteerVotePage {
  volunteer:any;
  volunteer1:any;
  //volunteer2:any;
  // testRadioOpen = false;
  // testRadioResult: any;

  constructor(
    public navCtrl: NavController, public navParams: NavParams,
    public newsData:NewsDataProvider,public toastCtrl: ToastController,
    public alertCtrl: AlertController,public modalCtrl: ModalController
  ){
    this.getVolunteerVote();
    this.getVolunteerVote1();
    //this.getVolunteerVote2();
  }

  getVolunteerVote(): Promise<any> {
    return this.newsData.getVolunteerVote().then(data => {
      this.volunteer = data;
    });
  }
  getVolunteerVote1(): Promise<any> {
    return this.newsData.getVolunteerVote1().then(data => {
      this.volunteer1 = data;
    });
  }
  // getVolunteerVote2(): Promise<any> {
  //   return this.newsData.getVolunteerVote2().then(data => {
  //     this.volunteer2 = data;
  //   });
  // }

  goToVolunteerVoteDetail(volunteerItem:any) {
    this.navCtrl.push(VolunteerVoteDetailPage,{
      volunteer:volunteerItem
    });
  }

  doRadio() {
    const alert = this.alertCtrl.create();
    alert.setTitle('請選擇月份');

    alert.addInput({
      type: 'radio',
      label: '1月',
      value: this.volunteer1,
      checked: true
    });

    // alert.addInput({
    //   type: 'radio',
    //   label: '2月',
    //   value: this.volunteer2
    // });

    alert.addButton({
      text: '確認',
      handler: (data: any) => {
        console.log('Radio data:', data);
        // this.testRadioOpen = false;
        // this.testRadioResult = data;
      }
    });
    alert.addButton('取消');

    alert.present();
  }


}

提供者頁面

getVolunteerVote() {
    return new Promise(resolve => {
      this.http.get('http').map(res => res.json().data).subscribe(data => {
          this.data = data;
          resolve(this.data);
        });
    });
  }
  getVolunteerVote1() {
    return new Promise(resolve => {
      this.http.get('http').map(res => res.json().data).subscribe(data => {
          this.data = data;
          resolve(this.data);
        });
    });
  }

您總是在ngFor遍歷volunteer ,但是單擊確認按鈕后不會更改其值。

我建議您添加一個selectedVolunteer字段,以保留選定月份的數據,並循環瀏覽以顯示選定的數據。 並在單擊確認按鈕時替換其值。

<ion-card (click)="goToVolunteerVoteDetail(volunteer)" style="width:26%" class="pin" *ngFor="let volunteers of selectedVolunteer">

export class VolunteerVotePage {
  selectedVolunteer: [];             // keep selected data here

  getVolunteerVote(): Promise<any> {
    return this.newsData.getVolunteerVote().then(data => {
      this.volunteer = data;
      this.selectedVolunteer = data;     // show January data be default;
    });
  }

  doRadio() {

    alert.addButton({
      text: '確認',
      handler: (data: any) => {
        console.log('Radio data:', data);
        this.selectedVolunteer = data;
      }
    });
    alert.addButton('取消');

    alert.present();
  }
}

參考演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM