如何使用Angular 2在下拉列表中仅显示唯一值 [英] How to display only unique values in the dropdown using Angular 2

查看:123
本文介绍了如何使用Angular 2在下拉列表中仅显示唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON数据,正在使用* ngFor从其中显示"accountNumber"到下拉列表中.由于JSON数据中有多个具有相同帐号的条目,因此在下拉列表中我多次看到相同的帐号. 在此处输入图片描述

I have a JSON data from which I am displaying 'accountNumber' into a dropdown using *ngFor. Since there are multiple entries in JSON data with the same account number, I am seeing the same account number multiple times in the dropdown. enter image description here

html:

<div class="btn btn-default dropdown-toggle" type="button" 
  id="dropdownMenu" data-toggle="dropdown" aria-expanded="true">

  <span>Select</span>
  <span class="caret"></span>
  <ul class="select-menu" aria-labelledby="dropdownMenu">
    <li *ngFor="#account of accounts">{{account.accountNumber}}</li>
  </ul>
</div>

json:

`[
{
    "accountNumber": 7890,
    "transactionDate": "4/2/2016",
    "postingDate": "4/3/2016",
    "description": "Pok Pok Thai",
    "category": "Restaurants",
    "amount": 15.00
},
{
    "accountNumber": 7890,
    "transactionDate": "4/3/2016",
    "postingDate": "4/4/2016",
    "description": "Pok Pok Hai",
    "category": "Hotel",
    "amount": 25.00
},

{
    "accountNumber": 8901,
    "transactionDate": "4/6/2016",
    "postingDate": "4/7/2016",
    "description": "Pok Pok Fai",
    "category": "Dairy",
    "amount": 55.00
},
{
    "accountNumber": 8901,
    "transactionDate": "4/7/2016",
    "postingDate": "4/8/2016",
    "description": "Pok Pok Aai",
    "category": "Automotive",
    "amount": 65.00
},

{
    "accountNumber": 4567,
    "transactionDate": "4/9/2016",
    "postingDate": "4/10/2016",
    "description": "Pok Pok Cai",
    "category": "Healthcare",
    "amount": 85.00
},
{
    "accountNumber": 4567,
    "transactionDate": "4/10/2016",
    "postingDate": "4/11/2016",
    "description": "Pok Pok Dai",
    "category": "Healthcare",
    "amount": 95.00
},

{
    "accountNumber": 8901,
    "transactionDate": "4/12/2016",
    "postingDate": "4/13/2016",
    "description": "sit amet",
    "category": "Software",
    "amount": 115.00
}
 ]`

如何避免在下拉列表中显示重复的帐号值?我假设这将需要自定义管道,但不确定如何操作.

How can I avoid displaying duplicate values of the account number in the dropdown?I am assuming it will require a custom pipe but not sure how to do that.

我是Angular 2的新手,曾尝试寻找解决方案,但找不到适合我需要的任何内容.

I am new to Angular 2 and tried looking for the solution but couldn't find anything that suits my need.

推荐答案

已经有示例发布了管道的基础解释:

There is already post explaing basic of pipes with examples: How to apply filters to *ngFor

查看适用于您的情况的打孔器 http://plnkr.co/edit/E7HlWeNJV2N3zwPfI51Q? p = preview ..我已经使用过 lodash 库及其uniqBy函数,那么管道真的就这么简单:

See the working plunker for your case http://plnkr.co/edit/E7HlWeNJV2N3zwPfI51Q?p=preview .. I have used lodash library and its uniqBy function, then the pipe is really that simple:

declare var _: any; // lodash, not strictly typed

@Pipe({
    name: 'uniqFilter',
    pure: false
})
@Injectable()
    export class UniquePipe implements PipeTransform {
        transform(items: any[], args: any[]): any {

        // lodash uniqBy function
        return _.uniqBy(items, args);
    }
}

..及其组件中的用法:

.. and the usage in your component:

<div>
    <ul>
        <li *ngFor="let account of accounts | uniqFilter: 'accountNumber'">{{ account.accountNumber }}</li>
    </ul>
</div>

编辑:我已经将插件添加到最新的Angular版本,并向管道中添加了过滤参数.

EDIT: I've updated the plunker to latest Angular version, and added filtering parameter to the pipe.

这篇关于如何使用Angular 2在下拉列表中仅显示唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆