0
我有一個數據列表。數據結構是一對多的。每個家長都有多個子項目。我嘗試隱藏重複的父項。但它隱藏了所有重複的記錄。我按照下面的教程。需要幫助。我只想隱藏父母項目,而不是刪除整個記錄。Angular2 * ngFor隱藏父項
My datatable: Failed Results: Expected Results:
Parent Child Parent Child Parent Child
Lee 123 Lee 123 Lee 123
Lee 124 Rose 111 124
Lee 125 125
Rose 111 Rose 111
代碼:
//our root app component
import { Component, NgModule, VERSION } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { Pipe, PipeTransform, Injectable } from '@angular/core'
@Pipe({
name: 'uniqFilter',
pure: false
})
@Injectable()
export class UniquePipe implements PipeTransform {
transform(items: any[], args: any[]): any {
// filter items array, items which match and return true will be kept, false will be filtered out
return _.uniqBy(items, args);
}
}
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<ul>
<li *ngFor="let account of accounts | uniqFilter:'Parent'">{{ account.Parent }} and {{ account.Child }}</li>
</ul>
</div>
`,
directives: [],
pipes: [UniquePipe]
})
export class App {
constructor() {
this.accounts = [{
"Parent": 'Lee',
"Child": "4/6/2016"
},
{
"Parent": 'Lee',
"Child": "4/7/2016"
},
{
"Parent": 'Rose',
"Child": "4/9/2016"
},
{
"Parent": 'Rose',
"Child": "4/10/2016"
},
{
"Parent": 'Lee',
"Child": "4/12/2016"
}];
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App, UniquePipe ],
bootstrap: [ App ],
providers: [ UniquePipe ]
})
export class AppModule {}
請直接添加相關的代碼,你的問題而不是隻連接到外部資源。 –