Add search filter to user management

This commit is contained in:
Lebeau Elise 2021-12-09 15:13:02 +00:00 committed by Sylvain MATHIEU
parent d46502b804
commit 4e2d043561
6 changed files with 57 additions and 1 deletions

View File

@ -273,6 +273,7 @@ import { UserManagementComponent } from './components/user-management/user-manag
import { UserService } from './services/user.service'; import { UserService } from './services/user.service';
import { LoggedUserComponent } from './components/users/logged-user/logged-user.component'; import { LoggedUserComponent } from './components/users/logged-user/logged-user.component';
import { AddUserDialogComponent } from './components/user-management/add-user-dialog/add-user-dialog.component'; import { AddUserDialogComponent } from './components/user-management/add-user-dialog/add-user-dialog.component';
import { UserFilterPipe } from './filters/user-filter.pipe';
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -462,6 +463,7 @@ import { AddUserDialogComponent } from './components/user-management/add-user-di
UserManagementComponent, UserManagementComponent,
ProjectReadmeComponent, ProjectReadmeComponent,
AddUserDialogComponent, AddUserDialogComponent,
UserFilterPipe,
], ],
imports: [ imports: [
BrowserModule, BrowserModule,

View File

@ -8,9 +8,15 @@
</div> </div>
</div> </div>
<form>
<mat-form-field class="full-width">
<input matInput placeholder="Search by name" [(ngModel)]="searchText" [ngModelOptions]="{ standalone: true }" />
</mat-form-field>
</form>
<div class="default-content"> <div class="default-content">
<div class="mat-elevation-z8"> <div class="mat-elevation-z8">
<mat-table #table [dataSource]="dataSource" matSort> <mat-table #table [dataSource]="dataSource | userFilter: searchText" matSort>
<ng-container matColumnDef="name"> <ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef mat-sort-header> Name </mat-header-cell> <mat-header-cell *matHeaderCellDef mat-sort-header> Name </mat-header-cell>
<mat-cell *matCellDef="let row"> <mat-cell *matCellDef="let row">

View File

@ -3,3 +3,9 @@
width: 160px; width: 160px;
margin: 20px; margin: 20px;
} }
.full-width {
width: 940px;
margin-left: -470px;
left: 50%;
}

View File

@ -35,6 +35,8 @@ export class UserManagementComponent implements OnInit {
userDatabase = new UserDatabase(); userDatabase = new UserDatabase();
displayedColumns = ['name', 'email', 'is_active', 'is_superadmin', 'last']; displayedColumns = ['name', 'email', 'is_active', 'is_superadmin', 'last'];
searchText: string = '';
@ViewChild(MatSort, { static: true }) sort: MatSort; @ViewChild(MatSort, { static: true }) sort: MatSort;
constructor( constructor(
@ -77,6 +79,7 @@ export class UserManagementComponent implements OnInit {
instance.server = this.server; instance.server = this.server;
dialogRef.afterClosed().subscribe(() => this.refresh()); dialogRef.afterClosed().subscribe(() => this.refresh());
} }
} }
export class UserDatabase { export class UserDatabase {

View File

@ -0,0 +1,8 @@
import { UserFilterPipe } from './user-filter.pipe';
describe('UserFilterPipe', () => {
it('create an instance', () => {
const pipe = new UserFilterPipe();
expect(pipe).toBeTruthy();
});
});

View File

@ -0,0 +1,31 @@
/*
* Software Name : GNS3 Web UI
* Version: 3
* SPDX-FileCopyrightText: Copyright (c) 2022 Orange Business Services
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This software is distributed under the GPL-3.0 or any later version,
* the text of which is available at https://www.gnu.org/licenses/gpl-3.0.txt
* or see the "LICENSE" file for more details.
*
* Author: Sylvain MATHIEU, Elise LEBEAU
*/
import { Pipe, PipeTransform } from '@angular/core';
import {UserDataSource} from "@components/user-management/user-management.component";
@Pipe({
name: 'userFilter'
})
export class UserFilterPipe implements PipeTransform {
transform(items: UserDataSource, searchText: string) {
if (!items) return [];
if (!searchText) return items;
searchText = searchText.toLowerCase();
return items.userDatabase.data.filter((item) => {
return (item.username && item.username.toLowerCase().includes(searchText))
|| (item.email && item.email.toLowerCase().includes(searchText));
});
}
}