I'm working on a users module that has a users view for administrative purposes. I can see my data, so that works fine, but I'm not understanding how the search filter is supposed to work. The following is my code. I'm a novice to typeScript and Angular so I'm using examples from online, but failing. SMH! Anyway, I type into the search filter and nothing happens.
users.component.ts
import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { FormControl } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { Subject } from 'rxjs';
import { Router } from "@angular/router";
import { UserService } from '../../users/user.service';
import { MatInput } from "@angular/material/input";
import { MatSort } from '@angular/material/sort';
import { BehaviorSubject } from "rxjs";
import { Observable } from 'rxjs';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.scss']
})
export class UsersComponent implements OnInit {
public users: any[] = [];
searchControl = new FormControl();
filteredOptions: Observable<string[]>;
MatTableDataSource: Observable<string[]>;
resetUsers: any;
@Input() header = "Users View";
@Input() columns = [
{ userName : "User Name" },
{ firstName: "First Name" },
{ lastName: "Last Name" },
{ phone: "Phone" },
{ email: "Email" },
];
@Input() data: any[];
@ViewChild(MatInput, { static: false }) search: MatInput;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
displayData = [];
constructor(
private userService: UserService,
private httpClient: HttpClient
) { }
dataSource = new MatTableDataSource<string[]>();
ngOnInit(): void {
this._getUsers();
}
ngAfterViewInit() {
this.userService.getUsers().subscribe(data => {
this.users = data.users;
});
this.search.stateChanges.subscribe(() => {
this.displayData = [];
this.users.forEach((entry) => {
if (entry.header.toLowerCase().includes(this.search.value.toLowerCase()))
{
this.displayData.push(entry);
}
});
});
}
private _getUsers()
{
this.userService;
this.userService.getUsers().subscribe(data => {
this.users = data.users;
});
}
}
user.component.html
<form>
<mat-form-field>
<div class="search-input">
<mat-label>Search</mat-label>
<input type="text"
matInput
[matAutocomplete]="auto" />
</div>
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
<div class="users">
<app-extend-table [header]="header"
[data]="users"
[columns]="columns">
</app-extend-table>
</div>