البحث بواسطة الـ Autocomplete
الـ HTML
السؤال: filter in mat-option with mat-autocomplete in angular
التوثيق:
To filter mat-option items within a mat-autocomplete in Angular, you can use Angular's reactive forms and RxJS operators to create a dynamic filtering mechanism. Here's a step-by-step guide to achieve this:
Step 1: Set Up Your Angular Component
- Import necessary modules in your
app.module.ts.
typescriptimport { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatOptionModule } from '@angular/material/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
ReactiveFormsModule,
MatFormFieldModule,
MatInputModule,
MatAutocompleteModule,
MatOptionModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
- Create the component and set up the form control and the options array.
typescriptimport { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
myControl = new FormControl();
options: string[] = ['Apple', 'Orange', 'Banana', 'Grapes', 'Mango'];
filteredOptions: Observable<string[]>;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges.pipe(
startWith(''),
map(value => this._filter(value))
);
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.options.filter(option => option.toLowerCase().includes(filterValue));
}
}
Step 2: Create the Template
Define the template to use mat-autocomplete with mat-option.
html<!-- app.component.html -->
<mat-form-field>
<input matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{ option }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Explanation:
Component Setup:
myControl: FormControl for the input field.options: Array of string options to be filtered.filteredOptions: Observable that holds the filtered options based on the input value.
Filtering Logic:
ngOnInit(): Initializes thefilteredOptionsobservable, which listens to changes in the input field and applies the_filtermethod._filter(value: string): Filters the options array based on the input value, converting both the input and options to lowercase for case-insensitive matching.
Template:
- The template includes
mat-autocompletewithmat-optionto display the filtered options. The*ngFordirective iterates over the filtered options and displays them as autocomplete suggestions.
- The template includes
Dynamic Filtering:
Whenever the user types in the input field, the myControl.valueChanges observable emits a new value, triggering the _filter method. This method filters the options array and updates the filteredOptions observable, which in turn updates the displayed autocomplete suggestions.
This approach ensures that your mat-autocomplete dynamically filters the mat-option items based on the user's input, providing a smooth and responsive autocomplete experience.
تعليقات
إرسال تعليق