这是我编辑过的代码请看看并帮我实现搜索和重置按钮..请
import {Component, ViewChild} from '@angular/core';
import {MatPaginator, MatTableDataSource} from '@angular/material';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/operator/map';
export class Dep {
constructor(public name: string) { }
}
@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'
];
deptControl: FormControl = new FormControl();
filtereddept: Observable<string[]>;
ngOnInit() {
this.filtereddept = this.deptControl.valueChanges
.startWith(null)
.map(val => val ? this.filter(val) : this.dept.slice());
}
filter(val: string): string[] {
return this.dept.filter(option =>
option.toLowerCase().indexOf(val.toLowerCase()) === 0);
}
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'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'},
{accno: 5400343, accdesc: 'ASTRALIS LTD', investigator:'Kruger, James G.', accCPC: 'OR',location:'ON',cdeptid: 110350,depdesc: 'Kruger Laboratory'}
];
<table>
<tr><td>Account Maintenance</td><td></td></tr>
<tr><td>Client Account ID</td>
<td>
<mat-form-field>
<input matInput placeholder=" ">
</mat-form-field><br/>
</td>
</tr>
<tr><td> Account Description</td>
<td>
<mat-form-field>
<input matInput placeholder=" ">
</mat-form-field><br/>
</td>
</tr>
<tr><td> Investigator</td>
<td>
<mat-form-field>
<input matInput placeholder=" ">
</mat-form-field><br/>
</td>
</tr>
<tr><td> Department</td>
<td>
<mat-form-field>
<input type="text" placeholder="Search.." matInput [formControl]="deptControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let dep of filtereddept | async" [value]="dep">
{{ dep }}
</mat-option>
</mat-autocomplete>
</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>
这里我想显示表格,因为我按下搜索按钮,想要在按下重置按钮时重置表格的值。
请帮助我...
答案 0 :(得分:2)
您可以使用mat-autocomplete
。将模板更改为:
<form>
<mat-form-field>
<input type="text" placeholder="Search.." matInput [formControl]="deptControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let dep of filtereddept | async" [value]="dep">
{{ dep }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
...和你的打字稿代码:
import {Component} from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/operator/map';
@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'
];
deptControl: FormControl = new FormControl();
filtereddept: Observable<string[]>;
ngOnInit() {
this.filtereddept = this.deptControl.valueChanges
.startWith(null)
.map(val => val ? this.filter(val) : this.dept.slice());
}
filter(val: string): string[] {
return this.dept.filter(option =>
option.toLowerCase().indexOf(val.toLowerCase()) === 0);
}
}
请注意,您需要在模块中添加 ReactiveFormsModule
。链接到工作StackBlitz demo。