问题 如何动态绑定Angular 2复选框的值


大家好:我有一个组件。组件视图有一个表:

<div class="container">
<h2>Recipients List</h2>
<br>
<table class="table table-striped">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Phone</th>
            <th>Status</th>
            <th>Select</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="#rp of arrAll">   
            <td>{{rp.id}}</td>             
            <td>{{rp.name}}</td>
            <td>{{rp.phone}}</td>                
            <td>{{rp.isActivated?'Active':'Inactive'}}</td>             
            <td>
                <input #{{rp.id}}  type="checkbox" (change)="checkbox(value)">
            </td>
        </tr>          
    </tbody>
</table>

<button class="btn btn-success" (click)="newRecipient()">New Recipient</button>
<button class="btn btn-danger pull-right" (click) ="deleteRecipients()">Delete Recipients</button>

这是一个图像的链接 表视图 这是使用* ngFor生成的。

业务逻辑是“单击删除按钮时,必须删除所有已检查的收件人”。我想将一个参数传递给我的删除函数(我认为应该是一个包含已检查的收件人ID的数组)
 

这是我的组件代码:

import {Component} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';
import { Router, RouteParams } from 'angular2/router';
import {RecipientsService}    from'../../services/recipients/recipients.service';
import {Recipient} from '../../models/recipient/recipient';

@Component({
selector: 'recipient-list-view',
templateUrl: './app/components/recipient-list-view/recipient-list-view.html',
styleUrls: ["./app/components/recipient-list-view/style.css"]
})

export class RecipientListViewComponent {

arrAll: Recipient[];

constructor(private recipientsService: RecipientsService, params:    RouteParams, private router: Router) {
    this.arrAll = recipientsService.getAll();        
}

newRecipient() {
    this.router.navigate(['../NewRecipient']);
}

deleteRecipients() {  //need to know which recipients are checked

    console.log("delete");
}


}

我想知道如何实现这一目标。

干杯


7053
2018-01-28 08:55


起源

<input#{{rp.id}} type =“checkbox”(change)=“checkbox(value)”>#{{rp.id}}就是这种语法 - Bob Zhang


答案:


添加一个属性 selected 给你的收件人。在复选框更改时,将其设置为true。

用户单击删除所有收件人后,只需筛选所选收件人的列表。

像这样的东西:

<div class="container">
    <h2>Recipients List</h2>
    <br>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Phone</th>
                <th>Status</th>
                <th>Select</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="#rp of arrAll">   
                <td>{{rp.id}}</td>             
                <td>{{rp.name}}</td>
                <td>{{rp.phone}}</td>                
                <td>{{rp.isActivated?'Active':'Inactive'}}</td>             
                <td>
                    <input #{{rp.id}}  [(ngModel)]="rp.selected" type="checkbox" (change)="checkbox(rp)">
                </td>
            </tr>          
        </tbody>
    </table>

    <button class="btn btn-success" (click)="newRecipient()">New Recipient</button>
    <button class="btn btn-danger pull-right" (click) ="deleteRecipients()">Delete Recipients</button>
</div>

和组件:

export class RecipientListViewComponent {

     arrAll: Recipient[];

     constructor(private recipientsService: RecipientsService, params: RouteParams, private router: Router) {
        this.arrAll = recipientsService.getAll();        
    }

    newRecipient() {
        this.router.navigate(['../NewRecipient']);
    }

    deleteRecipients() {  //need to know which recipients are checked
        let selected = this.arrAll.filter((x) => x.selected)
        console.log(selected);
    }

    checkbox(recipient) {
        recipient.selected = (recipient.selected) ? false : true;
    }
}

11
2018-01-28 09:17



它的工作原理谢谢:-) - Bob Zhang
为简单起见:recipient.selected =!receipient.selected; - sascha10000


如果您可以向“收件人”模型添加一个属性,则可以非常轻松地跟踪所选记录。

在您的收件人模型中添加了“选定”属性,并双向将复选框绑定到所选属性。

<input #{{rp.id}}  type="checkbox" [(ngModel)]="rp.selected">

现在在组件中添加一个方法以获取所有选定的记录

private getSelected() {
    return this.arrAll.filter((item) => item.selected);
}

然后获取所选记录

deleteRecipients() {  
    console.log(this.getSelected());
}

2
2018-01-28 09:21





在控制器中添加一个功能,在选中复选框时会触发该功能(单击复选框)。根据复选框的值,您将知道是否正在选中/取消选中它。维护已检查收件人列表的另一个模型。如果选中,则将收件人附加到列表,如果未选中则删除。调用delete时只删除该模型中的那些?


0
2018-01-28 09:05