gpt4 book ai didi

angular - 为 Angular Material 的 组件实现搜索过滤器

转载 作者:太空狗 更新时间:2023-10-29 17:40:23 25 4
gpt4 key购买 nike

尝试使用 Angular Material 在 Angular 2 中实现一个简单的应用程序。
我实现了一个带有分页的简单表格。

我还使用了 mat-select 组件,但为此我想实现一个搜索过滤器以从列表中键入和搜索所需的选项。

下面显示的是我的 .html 文件

<table>
<tr><td> Department</td>
<td>
<mat-form-field>
<mat-select placeholder=" ">
<mat-option> </mat-option>
<mat-option *ngFor="let dep of dept" [value]="dep">{{dep}}</mat-option>
</mat-select>
</mat-form-field><br/>
</td>
</tr>


</table>

<br><br>

<button >Search</button>

<button >Reset</button>

<button >Close</button>


<mat-card>
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource">

<!-- Account No. Column -->
<ng-container matColumnDef="accno">
<mat-header-cell *matHeaderCellDef> Account No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.accno}} </mat-cell>
</ng-container>

<!-- Account Description Column -->
<ng-container matColumnDef="accdesc">
<mat-header-cell *matHeaderCellDef> Account Description </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.accdesc}} </mat-cell>
</ng-container>

<!-- Investigator Column -->
<ng-container matColumnDef="investigator">
<mat-header-cell *matHeaderCellDef> Investigator </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.investigator}} </mat-cell>
</ng-container>

<!-- Account CPC Column -->
<ng-container matColumnDef="accCPC">
<mat-header-cell *matHeaderCellDef> Account CPC </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.accCPC}} </mat-cell>
</ng-container>

<!-- Location Column -->
<ng-container matColumnDef="location">
<mat-header-cell *matHeaderCellDef> Location </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.location}} </mat-cell>
</ng-container>


<!-- Client Dept ID Column -->
<ng-container matColumnDef="cdeptid">
<mat-header-cell *matHeaderCellDef> ClientDeptID </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.cdeptid}} </mat-cell>
</ng-container>


<!-- Dept Description Column -->
<ng-container matColumnDef="depdesc">
<mat-header-cell *matHeaderCellDef> Dept Description </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.depdesc}} </mat-cell>
</ng-container>


<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

<mat-paginator #paginator
[pageSize]="10"
[pageSizeOptions]="[5, 10, 20]">
</mat-paginator>
</div>
</mat-card>

下图是我的.ts文件

import {Component, ViewChild} from '@angular/core';
import {MatPaginator, MatTableDataSource} from '@angular/material';

@Component({
selector: 'app-account',
templateUrl: './account.component.html',
styleUrls: ['./account.component.scss']
})

export class AccountComponent {



dept = [
'Administrative Computer',
'Agosta Laboratory',
'Allis Laboratory',
'Bargaman Laboratory',
'Bio-Imaging Resource Center',
'Capital Projects',
'Casanova Laboratory',
'Darst Laboratory',
'Darnell James Laboratory',
'Deans Office',
'Energy Consultant',
'Electronic Shop',
'Facilities Management',
'Field Laboratory'
];


displayedColumns = ['accno', 'accdesc', 'investigator', 'accCPC','location','cdeptid','depdesc'];
dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);

@ViewChild(MatPaginator) paginator: MatPaginator;

ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
}
}

export interface Element {
accno: number;
accdesc: string;
investigator: string;
accCPC: string;
location:string;
cdeptid: number;
depdesc: string;
}

const ELEMENT_DATA: Element[] = [
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'}

];

谁能帮我在我的应用程序中使用 mat-select 组件实现搜索过滤器?

最佳答案

HTML

<h4>mat-select</h4>
<mat-form-field>
<mat-label>State</mat-label>
<mat-select>
<input (keyup)="onKey($event.target.value)"> // **Send user input to TS**
<mat-option>None</mat-option>
<mat-option *ngFor="let state of selectedStates" [value]="state">{{state}}</mat-option>
</mat-select>
</mat-form-field>

TS

states: string[] = [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware',
'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky',
'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico',
'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania',
'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

**// Initially fill the selectedStates so it can be used in the for loop**
selectedStates = this.states;

**// Receive user input and send to search method**
onKey(value) {
this.selectedStates = this.search(value);
}

**// Filter the states list and send back to populate the selectedStates**
search(value: string) {
let filter = value.toLowerCase();
return this.states.filter(option => option.toLowerCase().startsWith(filter));
}

解决方案相当简单。应该像魅力一样工作:)

关于angular - 为 Angular Material 的 <mat-select> 组件实现搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48442794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com