filter Customer table

This commit is contained in:
Michael Hoennig 2019-04-03 11:53:36 +02:00
parent d583c9ec47
commit 301f5facd4
2 changed files with 40 additions and 2 deletions

View File

@ -19,6 +19,12 @@
<th jhiSortBy="prefix"><span jhiTranslate="hsadminNgApp.customer.prefix">Prefix</span> <fa-icon [icon]="'sort'"></fa-icon></th> <th jhiSortBy="prefix"><span jhiTranslate="hsadminNgApp.customer.prefix">Prefix</span> <fa-icon [icon]="'sort'"></fa-icon></th>
<th></th> <th></th>
</tr> </tr>
<tr>
<th></th>
<th><input type="text" class="form-control" [(ngModel)]="filterValue.number" (keyup)="filter($event)"></th>
<th><input type="text" class="form-control" [(ngModel)]="filterValue.prefix" (keyup)="filter($event)"></th>
<th><button class="btn btn-primary float-left" (click)="resetFilter()">Reset Filter</button></th>
</tr>
</thead> </thead>
<tbody infinite-scroll (scrolled)="loadPage(page + 1)" [infiniteScrollDisabled]="page >= links['last']" [infiniteScrollDistance]="0"> <tbody infinite-scroll (scrolled)="loadPage(page + 1)" [infiniteScrollDisabled]="page >= links['last']" [infiniteScrollDistance]="0">
<tr *ngFor="let customer of customers ;trackBy: trackId"> <tr *ngFor="let customer of customers ;trackBy: trackId">

View File

@ -1,7 +1,7 @@
import { Component, OnInit, OnDestroy } from '@angular/core'; import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpErrorResponse, HttpHeaders, HttpResponse } from '@angular/common/http'; import { HttpErrorResponse, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Subscription } from 'rxjs'; import { Subscription, Subject } from 'rxjs';
import { filter, map } from 'rxjs/operators'; import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
import { JhiEventManager, JhiParseLinks, JhiAlertService } from 'ng-jhipster'; import { JhiEventManager, JhiParseLinks, JhiAlertService } from 'ng-jhipster';
import { ICustomer } from 'app/shared/model/customer.model'; import { ICustomer } from 'app/shared/model/customer.model';
@ -24,6 +24,9 @@ export class CustomerComponent implements OnInit, OnDestroy {
predicate: any; predicate: any;
reverse: any; reverse: any;
totalItems: number; totalItems: number;
filterValue: any;
filterValueChanged = new Subject<string>();
subscription: Subscription;
constructor( constructor(
protected customerService: CustomerService, protected customerService: CustomerService,
@ -40,11 +43,25 @@ export class CustomerComponent implements OnInit, OnDestroy {
}; };
this.predicate = 'id'; this.predicate = 'id';
this.reverse = true; this.reverse = true;
this.resetFilter();
}
resetFilter() {
this.filterValue = {
number: null,
prefix: null
};
this.loadAll();
} }
loadAll() { loadAll() {
let criteria = {
...(this.filterValue.number && { 'number.equals': this.filterValue.number }),
...(this.filterValue.prefix && { 'prefix.contains': this.filterValue.prefix })
};
this.customerService this.customerService
.query({ .query({
...criteria,
page: this.page, page: this.page,
size: this.itemsPerPage, size: this.itemsPerPage,
sort: this.sort() sort: this.sort()
@ -55,6 +72,10 @@ export class CustomerComponent implements OnInit, OnDestroy {
); );
} }
filter($event) {
this.filterValueChanged.next($event.target.value);
}
reset() { reset() {
this.page = 0; this.page = 0;
this.customers = []; this.customers = [];
@ -72,6 +93,15 @@ export class CustomerComponent implements OnInit, OnDestroy {
this.currentAccount = account; this.currentAccount = account;
}); });
this.registerChangeInCustomers(); this.registerChangeInCustomers();
this.subscription = this.filterValueChanged
.pipe(
debounceTime(500),
distinctUntilChanged((previous: any, current: any) => previous === current)
)
.subscribe(() => {
this.loadAll();
});
} }
ngOnDestroy() { ngOnDestroy() {
@ -97,6 +127,8 @@ export class CustomerComponent implements OnInit, OnDestroy {
protected paginateCustomers(data: ICustomer[], headers: HttpHeaders) { protected paginateCustomers(data: ICustomer[], headers: HttpHeaders) {
this.links = this.parseLinks.parse(headers.get('link')); this.links = this.parseLinks.parse(headers.get('link'));
this.totalItems = parseInt(headers.get('X-Total-Count'), 10); this.totalItems = parseInt(headers.get('X-Total-Count'), 10);
this.page = 0;
this.customers = [];
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
this.customers.push(data[i]); this.customers.push(data[i]);
} }