Angular Material 使用对象代码和说明自动完成
Angular Material Autocomplete with Object Code and Description
我正在尝试使用 Angular Material 自动完成和 class。我在滚动下拉列表中显示了描述,并希望在 formValue 中获取的实际值是代码。
出于某种原因,一切正常,自动完成将在滚动中显示所有描述,但当我实际上 select 一个描述值时,代码呈现在文本框中。它应该仍然显示描述,但将代码值存储在表单中。这在使用 mat-select 下拉菜单时工作正常,但是转移到自动完成会导致一些问题。
有人知道如何解决这个问题吗?
打字稿:
readonly dataSource = DataSource;
dataSourceFilteredOptions: Observable<Array<BaseParentLookupVm>>;
private _filterDataSource(value: string): Array<BaseParentLookupVm> {
const filterValue = value?.toLowerCase() ?? '';
let data = this.dataSource.filter(option => option.description.toLowerCase().includes(filterValue));
return data;
}
createDataSourceFilteredOptions() {
this.dataSourceFilteredOptions = this.processBatchForm.get('dataSourceField').valueChanges
.pipe(
startWith(''),
map(value => this._filterDataSource(value))
);
}
public initializeNewBatchForm(): void {
this.processBatchForm = this.formBuilder.group({
'dataSourceField': [null, [Validators.required]],
});
}
HTML:
<mat-form-field>
<mat-label>Data Source</mat-label>
<input
type="text"
placeholder="Select"
matInput
formControlName="'dataSourceField'"
[matAutocomplete]="templateDataSource">
<mat-autocomplete
#templateDataSource="matAutocomplete"
>
<mat-option>Select</mat-option>
<mat-option
*ngFor="let dataSourceItem of dataSourceFilteredOptions | async"
[value]="dataSourceItem.code"
>
{{dataSourceItem.description}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
补充码:
export interface BaseParentLookupVm {
displayOrderId?: number;
code: string;
description: string;
}
export const DataSource: Array<BaseParentLookupVm> = [
{
displayCode: '1',
code: '1111',
description: '1111 Description'
},
{
displayCode: '2',
code: '2222',
description: '2222 Description'
},
{
displayCode: '3',
description: '3333 Description'
},
{
displayCode: '4',
description: '4444 Description'
}
];
资源:
这个答案直接从值映射到描述,所以他们的答案有效。
正在尝试使用文本框更新 [DisplayWith],Angular mat-autocomplete : How to display the option name and not the value in the input
这个答案没有说明如何使用文本框,给出另一个问题How to display using [displayWith] in AutoComplete Material2
要使用 displayWith,您必须将代码更改为:
<mat-form-field>
<mat-label>Data Source</mat-label>
<input
type="text"
placeholder="Select"
matInput
formControlName="'dataSourceField'"
[matAutocomplete]="templateDataSource">
<mat-autocomplete #templateDataSource="matAutocomplete" [displayWith]="displayFn">
<mat-option>Select</mat-option>
<mat-option
*ngFor="let dataSourceItem of dataSourceFilteredOptions | async"
[value]="dataSourceItem">
{{dataSourceItem.description}}
</mat-option>
</mat-autocomplete>
TS
myControl = new FormControl();
options = [{
displayOrderId: 1,
code: "1111",
description: "1111 Description"
},
{
displayOrderId: 2,
code: "2222",
description: "2222 Description"
},
{
displayOrderId: 3,
code: "3333",
description: "3333 Description"
},
{
displayOrderId: 4,
code: "4444",
description: "4444 Description"
}
];
filteredOptions: Observable < any > ;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges.pipe(
startWith(""),
map(value => this._filter(value))
);
}
private _filter(value: any) {
let filterValue = '';
if (typeof value === "string") {
filterValue = value.toLowerCase();
} else {
filterValue = value.description.toLowerCase();
}
return this.options.filter(
option => option.description.toLowerCase().indexOf(filterValue) === 0
);
}
displayFn(value: any) {
return value ? value.description : undefined;
}
我正在尝试使用 Angular Material 自动完成和 class。我在滚动下拉列表中显示了描述,并希望在 formValue 中获取的实际值是代码。
出于某种原因,一切正常,自动完成将在滚动中显示所有描述,但当我实际上 select 一个描述值时,代码呈现在文本框中。它应该仍然显示描述,但将代码值存储在表单中。这在使用 mat-select 下拉菜单时工作正常,但是转移到自动完成会导致一些问题。
有人知道如何解决这个问题吗?
打字稿:
readonly dataSource = DataSource;
dataSourceFilteredOptions: Observable<Array<BaseParentLookupVm>>;
private _filterDataSource(value: string): Array<BaseParentLookupVm> {
const filterValue = value?.toLowerCase() ?? '';
let data = this.dataSource.filter(option => option.description.toLowerCase().includes(filterValue));
return data;
}
createDataSourceFilteredOptions() {
this.dataSourceFilteredOptions = this.processBatchForm.get('dataSourceField').valueChanges
.pipe(
startWith(''),
map(value => this._filterDataSource(value))
);
}
public initializeNewBatchForm(): void {
this.processBatchForm = this.formBuilder.group({
'dataSourceField': [null, [Validators.required]],
});
}
HTML:
<mat-form-field>
<mat-label>Data Source</mat-label>
<input
type="text"
placeholder="Select"
matInput
formControlName="'dataSourceField'"
[matAutocomplete]="templateDataSource">
<mat-autocomplete
#templateDataSource="matAutocomplete"
>
<mat-option>Select</mat-option>
<mat-option
*ngFor="let dataSourceItem of dataSourceFilteredOptions | async"
[value]="dataSourceItem.code"
>
{{dataSourceItem.description}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
补充码:
export interface BaseParentLookupVm {
displayOrderId?: number;
code: string;
description: string;
}
export const DataSource: Array<BaseParentLookupVm> = [
{
displayCode: '1',
code: '1111',
description: '1111 Description'
},
{
displayCode: '2',
code: '2222',
description: '2222 Description'
},
{
displayCode: '3',
description: '3333 Description'
},
{
displayCode: '4',
description: '4444 Description'
}
];
资源:
这个答案直接从值映射到描述,所以他们的答案有效。
正在尝试使用文本框更新 [DisplayWith],Angular mat-autocomplete : How to display the option name and not the value in the input
这个答案没有说明如何使用文本框,给出另一个问题How to display using [displayWith] in AutoComplete Material2
要使用 displayWith,您必须将代码更改为:
<mat-form-field>
<mat-label>Data Source</mat-label>
<input
type="text"
placeholder="Select"
matInput
formControlName="'dataSourceField'"
[matAutocomplete]="templateDataSource">
<mat-autocomplete #templateDataSource="matAutocomplete" [displayWith]="displayFn">
<mat-option>Select</mat-option>
<mat-option
*ngFor="let dataSourceItem of dataSourceFilteredOptions | async"
[value]="dataSourceItem">
{{dataSourceItem.description}}
</mat-option>
</mat-autocomplete>
TS
myControl = new FormControl();
options = [{
displayOrderId: 1,
code: "1111",
description: "1111 Description"
},
{
displayOrderId: 2,
code: "2222",
description: "2222 Description"
},
{
displayOrderId: 3,
code: "3333",
description: "3333 Description"
},
{
displayOrderId: 4,
code: "4444",
description: "4444 Description"
}
];
filteredOptions: Observable < any > ;
ngOnInit() {
this.filteredOptions = this.myControl.valueChanges.pipe(
startWith(""),
map(value => this._filter(value))
);
}
private _filter(value: any) {
let filterValue = '';
if (typeof value === "string") {
filterValue = value.toLowerCase();
} else {
filterValue = value.description.toLowerCase();
}
return this.options.filter(
option => option.description.toLowerCase().indexOf(filterValue) === 0
);
}
displayFn(value: any) {
return value ? value.description : undefined;
}