从列表中删除选定的选中项目 - Angular 2 + / Ionic 2

时间:2017-07-17 01:33:53

标签: javascript angular typescript ionic2

我有一个复选框列表。当用户选择保存时,将删除选中的项目并将其放置在我已包含的已保存选项卡中。我已设法取消功能以删除项目,只是不知道如何检查它是否已检查然后删除所述项目。感谢

HTML

<ion-content class="checks">

    <div [ngSwitch]="messages">

    <ion-list active *ngSwitchCase="'received'">
      <ion-item>
        <ion-label>Select All</ion-label>
        <ion-checkbox (click)="checkAll()" [(ngModel)]="selectedAll" ></ion-checkbox>
      </ion-item>


    <ion-item *ngFor="let rmessage of rmessages; index as i">
        <ion-label>{{rmessage.text}}</ion-label>
        <ion-checkbox [checked]="selectedAll" [(ngModel)]="singleChecked.checked" ></ion-checkbox>
    </ion-item>

        <button ion-button full (click)="save($index)" >Save</button>
    </ion-list>
  <ion-list radio-group *ngSwitchCase="'sent'">
    <ion-list-header>Saved Messages</ion-list-header>
        <ion-item>
        <p>Saved message here</p>
        </ion-item>
  </ion-list>
</div>
</ion-content>

TypeScript

import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

import { NavController, PopoverController, ViewController, ToastController } from 'ionic-angular';


@Component({
  selector: 'page-messages',
  templateUrl: 'messages.html'
})
export class MessagesPage {
selectedAll: boolean = false;
messages: string = 'messages';
findIndex: any;
singleChecked: boolean = false;
rmessages: any[] = [
    { text: 'This is a test message.' },
    { text: 'This is a second test message.' },
    { text: 'This is a third test message.' }
  ]

  constructor(public navCtrl: NavController, public popoverCtrl: PopoverController, private toastCtrl: ToastController) { 
    this.messages = 'received';
  }

 ionViewDidLoad() {
}

checkAll(){
  console.log(this.messages.length)
  if(this.selectedAll){
    this.selectedAll = true

  }
  else {
    this.selectedAll = false
  }

}

save(index){

    this.rmessages.splice(
          this.rmessages.indexOf(index), 1)

}

}

1 个答案:

答案 0 :(得分:1)

需要进行几项修改: -  您的rMessages模型需要更加智能化

rmessages: any[] = [
    { text: 'This is a test message.' , selected:false},
    { text: 'This is a second test message.', selected:false },
    { text: 'This is a third test message.', selected:false }   ] 

然后在你的ngFor

里面
<ion-checkbox [(ngModel)]="rmessage.selected"  ></ion-checkbox>
    </ion-item>

在selectall中使用循环使用foreach或其他东西并将其设置为true或false

保存

使用rmessages.filter并获取所选项目并将其保存在已保存的选项卡

rmessages.filter(function (x) { return x.selected;}).map(function(y){ return y.text;});

然后将rmessages设置为未选择项目的rmessages.filter

 rmessages =  rmessages.filter(function (x) { return !x.selected;}