ionic3通过`radio`

时间:2018-05-02 03:08:33

标签: angular ionic3

当我点击'1月'意思是“1月份”,我希望ion-grid显示1月份的志愿者数据。 console.log已获得正确的数据,但ion-grid无法更新。

这是登录网络浏览器

enter image description here

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);
        });
    });
  }

1 个答案:

答案 0 :(得分:1)

您始终在volunteer处循环ngFor,但在点击确认按钮后不会更改其值。

我建议你添加一个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();
  }
}

参考 demo