我正在显示一些数据并通过离子选择进行过滤。我在ion select中显示数组的数据。但是有些值会重复5到6次。我只想显示一次值,有没有办法只显示一个值?可能是由过滤器完成的。有谁知道我该如何适应它来一次显示值?
<div class="col-md-3">
<div class="form-group" >
<label for="">City</label>
<select class="form-control" [(ngModel)]="userFilter.city" >
<option *ngFor="let select of data" value="{{select.city}}">{{select.city}}</option>
</select>
</div>
</div>
.ts
import { Component, OnInit } from '@angular/core';
import { ApiService } from 'app/services/api/api.service';
import { map } from 'rxjs/operators';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map'
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent {
clientData: Observable<any>;
data: any = [];
status: any = [];
countunsettled: any;
countsettled: any;
sums: any;
constructor(private modalService: NgbModal, private api:ApiService, public httpClient: HttpClient) {
this.getClaims();
}
userFilter: any = { claim_no:'', member_name:'', status:'', company_id: ''};
openDeal(deletecontent,x){
this.selectedDeal = x;
this.dealModal= this.modalService.open(deletecontent, x);
this.dealModal.result.then(r=>{
}, err=> console.log(err))
}
getClaims(){
if(this.userFilter.company_id){
let url = 'http://api.igiinsurance.com.pk:8888/insurance_IGItakaful/insurance-api/get_claims.php?company_id='+this.userFilter.company_id;
}else{
let url = 'http://api.igiinsurance.com.pk:8888/insurance_IGItakaful/insurance-api/get_claims.php?offset=0&limit=100';
}
this.clientData = this.httpClient.get(url).
subscribe(data => {
console.log(data);
this.data = data.records;
var status = 'settled';
var status2 = 'submitted';
var countsettled = this.data.filter((obj) => obj.status === status).length;
var countunsettled = this.data.filter((obj) => obj.status === status2).length;
console.log(countsettled);
this.countsettled = countsettled;
console.log(countunsettled);
this.countunsettled = countunsettled;
const sum1 = this.data.filter(item => item.status === 'settled')
.reduce((acc, item) => acc + Number(item.approved_value), 0);
console.log(sum1);
this.sum1 = sum1;
const sum2 = this.data.filter(item => item.status === 'submitted')
.reduce((acc, item) => acc + Number(item.approved_value), 0);
console.log(sum2);
this.sum2 = sum2
}
}
}
答案 0 :(得分:3)
使用单独的Set
保留城市名称,这样就可以避免重复的值,因为Set
仅保留任何类型的唯一值。
打字稿:
class ViewControllerd: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(self.view.cardCount())
}
}
模板:
cities:Set = new Set();
// .... other codes
this.clientData = this.httpClient.get(url).subscribe(data => {
this.data = data.records;
// after loading data do the following
// add city value to the set
data.forEach(d => this.cities.add(d.city))
// ... remaining code
答案 1 :(得分:1)
利用丑陋的自定义管道并在下拉菜单中显示无重复值
<div class="col-md-3">
<div class="form-group" >
<label for="">City</label>
<select class="form-control" [(ngModel)]="userFilter.city" >
<option *ngFor="let select of data | unique" value="{{select.city}}">{{select.city}}</option>
</select>
</div>
</div>
管道
@Pipe(name: 'unique')
export class FilterPipe implements PipeTransform
{
transform(value: any, args?: any): any {
let uniqueArray = value.filter(function (el, index, array) {
return array.indexOf (el) == index;
});
return uniqueArray;
}
}