I have a component which allows users to search files by tag and I use ngx-pagination for the pagination. When I am in the first page I can search all the files but when I move to other page always the search doesn't count the previous page only the search method works to next pages. But it gives me the page number contains this tag.
but I want it to show the card which contains the file.
myComponent.ts:
export class AfficherComponent implements OnInit {
SearchTag: String;
files = [];
constructor(private uploadService: UploadFileService) {}
ngOnInit() {
this.reloadData();
}
reloadData() {
this.uploadService.getFilesList().subscribe((data) => {
this.files = data;
});
}
Search() {
if (this.SearchTag != "") {
this.files = this.files.filter((res) => {
return res.tag
.toLocaleLowerCase()
.match(this.SearchTag.toLocaleLowerCase());
});
} else if (this.SearchTag === "") {
this.ngOnInit();
}
}
}
myComponent.html
<div id="pricing-table" class="clear" data-aos="fade-right">
<div class="sel">
<div class="row">
<div class="col" style="left: -160px;">
<input
type="text"
placeholder="Search..."
[(ngModel)]="SearchTag"
(input)="Search()"
style="margin: 20px;"
/>
<div id="toggle" style="left: 450px;"></div>
</div>
</div>
</div>
<div
class="plan"
*ngFor="let file of files | paginate: { itemsPerPage: 3, currentPage: p }"
>
<h3></h3>
<button class="btn" style="background-color: #09c0a3;">Ouvrir</button>
<ul>
<li><b>Name: </b> {{ file.nom }}</li>
<li><b>Type: </b> {{ file.type }}</li>
<li><b>Departement: </b>{{ file.departement }}</li>
<li><b>Tag: </b>{{ file.tag }}</li>
</ul>
</div>
<pagination-controls
(pageChange)="p = $event"
style="float: right;"
></pagination-controls>
</div>
The docs state that there is an event called pageBoundsCorrection
:
pageBoundsCorrection
[event handler]
The expression specified will be invoked when thecurrentPage
value is found to be out-of-bounds (e.g. the collection size was reduced). The$event
argument will be the number of the closest valid page.
So you should handle that event in the same way as you handle (pageChange)
.
Some other recommendations:
p
property on your component to make it clear that the HTML is using itincludes
instead of match
in your filter (unless you want to handle regex searches)