我正在使用Angular 6和Material 6开发一个简单的页面。我想使用Material的自动完成功能从服务中恢复数据,但我不知道如何做得好。
从官方示例https://material.angular.io/components/autocomplete/overview我不明白如何使用服务将其与自动完成功能集成。
有人能帮助我吗?
由于
答案 0 :(得分:3)
最后,我找到了我想做的解决方案! 要将FormArray绑定到mat-table dataSource,您必须: 简而言之,这个例子是这样的:
<table mat-table [dataSource]="itemsDataSource">
<ng-container matColumnDef="itemName">
<td mat-cell *matCellDef="let element">{{ element.value.material?.name }}</td>
</ng-container>
<ng-container matColumnDef="itemCount">
<td mat-cell *matCellDef="let element">{{ element.value.itemCount }}</td>
</ng-container>
<tr mat-row *matRowDef="let row; columns: itemColumns;"></tr>
</table>
和代码:
export class ItemListComponent implements OnInit {
constructor(
private fb: FormBuilder
) { }
itemColumns = ['itemName', 'count'];
itemForm: FormGroup;
itemsDataSource = new MatTableDataSource();
get itemsForm() {
return this.itemForm.get('items') as FormArray;
}
newItem() {
const a = this.fb.group({
material: new FormControl(), //{ name:string }
itemCount: new FormControl() // number
});
this.itemsForm.push(a);
this.itemsDataSource._updateChangeSubscription(); //neccessary to render the mat-table with the new row
}
ngOnInit() {
this.itemForm = this.fb.group({
items: this.fb.array([])
});
this.newItem();
this.itemsDataSource.data = this.itemsForm.controls;
}
}
答案 1 :(得分:1)
只要用户更改输入值,您就需要从服务器数据中填写Error: no mixin named hover
Backtrace:
stdin:16
on line 16 of stdin
>> @include hover {
-------------^
。
options
在您的组件文件中,您需要处理<input type="text" matInput (input)="onInputChanged($event.target.value)" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">{{ option.title }}</mat-option>
</mat-autocomplete>
和onInputChanged(searchStr)
。
options
答案 2 :(得分:1)
好吧,假设您将来自服务器的数据作为具有类似IyourAwesomeData结构的对象返回,并且出于本示例的目的,我们将使用字段someName来过滤数据
所以你的组件看起来应该是这样的:
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormControl } from '@angular/forms';
import { startWith, debounceTime, map, switchMap, distinctUntilChanged } from 'rxjs/operators';
import { Subscription } from 'rxjs';
interface IyourAwesomeData {
someName: string;
someField: string;
someOtherField: number;
}
export class YourAutcompleteComponent implements OnInit, OnDestroy {
dataFiltered: IyourAwesomeData[]; // this data will be used inside HTML Template
data: Observable<IyourAwesomeData[]>;
yourInputCtrl = new FormControl();
private sub: Subscription;
constructor() {}
ngOnInit() {
this.data = ??? // Pass your data as Observable<IyourAwesomeData[]>;
this.sub = this.yourInputCtrl.valueChanges
.pipe(
debounceTime(500),
distinctUntilChanged(),
startWith(''),
switchMap((val) => {
return this.filterData(val || '');
})
).subscribe((filtered) => {
this.dataFiltered = filtered;
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
filterData(value: string) {
return this.data // like IyourAwesomeData[]
.pipe(
map((response) => response.filter((singleData: IyourAwesomeData) => {
return singleData.someName.toLowerCase().includes(value.toLowerCase())
})),
);
}
}
和你的HTML模板看起来应该是这样的:
<mat-form-field>
<input matInput placeholder="some placeholder" [matAutocomplete]="auto" [formControl]="yourInputCtrl">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let single of dataFiltered" [value]="single.someName">
{{ single.someName }}
</mat-option>
</mat-autocomplete>
</mat-form-field>