To filter data across all records while implementing pagination in Angular, you'll need to maintain two sets of data: a full list of users (for filtering) and a paginated list (for displaying on the current page).
Here’s how to approach this:
- Keep a full list of users that you can filter through based on the search keyword.
- Apply the filter to the full list of users, and then utilize the filtered result for pagination.
- You must ensure that the paginated view updates whenever the users are filtered.
Example Angular Code
Below is an updated example that meets your requirements:
Component TypeScript File
import { Component } from '@angular/core';
@Component({
selector: 'app-user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.css']
})
export class UserListComponent {
searchedKeyword: string = '';
// This should contain all users fetched from a service/API
allUsers: any[] = [];
filteredUsers: any[] = [];
// Pagination-related fields
p: number = 1; // Current page
itemsPerPage: number = 3; // Items per page
totalItems: number = 0; // Total users count
constructor() {
this.getUsers(); // Call the function to initialize users
}
// Mock method to mimic fetching users
getUsers() {
// Here, replace this with your actual API call
this.allUsers = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
{ id: 4, name: 'David' },
{ id: 5, name: 'Eve' },
{ id: 6, name: 'Frank' },
{ id: 7, name: 'Grace' },
{ id: 8, name: 'Hannah' },
{ id: 9, name: 'Igor' }
// Add more users if needed
];
// Initially set filtered users as all users
this.filteredUsers = [...this.allUsers];
this.totalItems = this.filteredUsers.length; // Set total items initially
}
// Filter users based on the search keyword
filterUsers() {
// Filter users based on the searchedKeyword
const keyword = this.searchedKeyword.toLowerCase();
this.filteredUsers = this.allUsers.filter(user =>
user.name.toLowerCase().includes(keyword)
);
// Update total items count after filtering
this.totalItems = this.filteredUsers.length;
// Reset the page to the first page if the filtered user list changes
this.p = 1;
}
}
Template HTML File
<input
[(ngModel)]="searchedKeyword"
(keyup.enter)="filterUsers()"
class="form-control adminPanel_searchInput vf_customInput"
name="searchTerm"
placeholder="Search for User .."
type="text" />
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of filteredUsers | paginate: { itemsPerPage: itemsPerPage, currentPage: p }; let i = index">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
</tr>
</tbody>
</table>
<pagination-controls (pageChange)="p = $event"></pagination-controls>
Explanation:
-
Full User List: We store the full list of users in allUsers. This represents all users fetched from your backend or an API.
-
Filtered User List: The filteredUsers array holds the current filtered results, which are updated when the user types in the search box.
-
Filtering Logic:
- The
filterUsers() function filters the allUsers based on the searchedKeyword.
- It then updates
filteredUsers and also updates totalItems to match the count of filtered results.
- Finally, it resets the current page (
p) to 1 when the filtering occurs, ensuring the user sees the beginning of the new filtered list.
-
Pagination: The table uses the filteredUsers array to display the current page of filtered results. The paginate pipe handles pagination.
-
Event on Enter Key: The filtering function is triggered when the user presses the Enter key in the search box.
Pagination Component:
Ensure you have the pagination component set up correctly, and it's configured to handle page changes. You may need to install a library like ngx-pagination to provide easy pagination support if you haven't done that already.
This implementation should meet the requirement of searching across all user data while paginating the results based on the filter applied.