gpt4 book ai didi

javascript - Angular Material : How to refresh a drop-down list after a certain condition?

转载 作者:行者123 更新时间:2023-11-30 19:29:47 35 4
gpt4 key购买 nike

我有一个下拉列表,我想刷新该值,更确切地说,我有两种语言,对于每种语言,我都有这些元素 [name-for English, and namecz for Czech],如果值为“English” “捷克语的英语将是捷克语,到目前为止,我已经设法以每种语言显示列表,甚至选择了正确的值,我的问题是,如果我更改语言,所选值保持不变,不会改变.

这是我想要得到的类似示例。 https://stackblitz.com/edit/angular-jfetuh?file=app%2Fselect-value-binding-example.html

这是我的有效负载:

[{id: 19, name: "Travel Recreation and Leisure", nameCZ: "test19"},…]
0: {id: 19, name: "Travel Recreation and Leisure", nameCZ: "test19"}
1: {id: 20, name: "Wholesale and Distribution", nameCZ: "test20"}
2: {id: 1, name: "Agriculture and Mining", nameCZ: "test1", description: "hello"}
3: {id: 2, name: "Business Services", nameCZ: "test2"}
4: {id: 3, name: "Computer and Electronics", nameCZ: "test3"}
5: {id: 4, name: "Consumer Services", nameCZ: "test4"}
6: {id: 5, name: "Education", nameCZ: "test5"}
7: {id: 6, name: "Energy and Utilities", nameCZ: "test 6"}
8: {id: 7, name: "Financial Services", nameCZ: "test 7"}
9: {id: 8, name: "Government", nameCZ: "test 8"}
10: {id: 9, name: "Health, Pharmaceuticals, and Biotech", nameCZ: "test9"}
11: {id: 10, name: "Manufacturing", nameCZ: "test10"}
12: {id: 11, name: "Media and Entertainment", nameCZ: "test11"}
13: {id: 12, name: "Non-profit", nameCZ: "test12"}
14: {id: 13, name: "Other", nameCZ: "test13"}
15: {id: 14, name: "Real Estate and Construction", nameCZ: "test14"}
16: {id: 15, name: "Retail", nameCZ: "test15"}
17: {id: 16, name: "Software and Internet", nameCZ: "test16"}
18: {id: 17, name: "Telecommunications", nameCZ: "test17"}
19: {id: 18, name: "Transportation and Storage", nameCZ: "test18"}

预期结果:19: {id: 18, name: "Transportation and Storage", nameCZ: "test18"} 如果语言是英语并且我们有选择的值“Transportation and Storage”,如果我改变语言我希望选择的值以某种方式翻译/选择“test18”

在 HTML 我有:

<div class="field-box">

<mat-form-field formGroupName="industry" *ngIf="language">
<input type="text" placeholder="Search for Industries " aria-label="Number" matInput
formControlName="searchIndustriesInput" [matAutocomplete]="industryAutoComplete">
<mat-autocomplete #industryAutoComplete="matAutocomplete" [displayWith]="displayIndustries.bind(this)"
(optionSelected)="industrySelected($event)">
<div *ngIf="showCurrentLang">
<mat-option *ngFor="let item of filteredIndustries | async" [value]="item">
{{item.name}}
</mat-option>
</div>
<div *ngIf="!showCurrentLang">
<mat-option *ngFor="let item of filteredIndustries | async" [value]="item">
{{item.nameCZ}}
</mat-option>
</div>
</mat-autocomplete>
<div
*ngIf="industryForm.controls.id.invalid && (industryForm.controls.id.dirty || industryForm.controls.id.touched)"
class="alert alert-danger">
<mat-error *ngIf="industryForm.controls.id.errors.required">Must fill this field</mat-error>
</div>
</mat-form-field>
</div>
</div>

TS 我有

export class CustomerQuestionnaireComponent implements OnInit {
selectedValueEmployeeValue: string;
selectedValueIndustryValue: string;
selectedValueLanguageValue: string;
selectedValueAppsValue: string;
show: boolean;
showCurrentLang: boolean;
private language: string;
afterDisplayIndustries: boolean;
filteredIndustries: Observable<IIndustry[]>;
selectedApps: IAppIntegratedApp[] = [];

@ViewChildren(MatChip) children: QueryList<MatChip>;

form: FormGroup;

@Input() isLoading: boolean;
@Input() industries: IIndustry[];
@Input() errMsg: string;
@Input() updateMsg: string;
@Input() customerPreference: CustomerPreference;
@Input() avaliableLanguages: ILanguage[];
@Input() filteredApps: IAppIntegratedApp[];

@Output() searchApps: EventEmitter<string> = new EventEmitter<string>();

@ViewChild('auto') matAutocomplete: MatAutocomplete;
@ViewChild('searchAppInput') searchAppInput: ElementRef;

companySizeValues: IDataSourceOne[] = [
{ key: 'INDIVIDUAL', value: 'Individual(1)' },
{ key: 'MICRO', value: 'Micro(2-9)' },
{ key: 'SMALL', value: 'Small(10-49)' },
{ key: 'MEDIUM', value: 'Medium-sized(49-249)' },
{ key: 'BIG', value: 'Big(250+)' },
];

separatorKeysCodes: number[] = [ENTER, COMMA];

constructor(private fb: FormBuilder, private langService: LangService ,
private translate: TranslateService) {
this.langService.currentLang.subscribe((lang) => {
this.language = lang;
if (this.language === 'en') {
this.showCurrentLang = true ;
} else {
this.showCurrentLang = false ;
}
if (this.afterDisplayIndustries) {
this.displayIndustries((<FormGroup>this.form.controls.industry).value.searchIndustriesInput);
this.industrySelected();
}
});

}
get isFormReady() {
return this.avaliableLanguages !== undefined;
}

ngOnInit() {
this.buildForm();
this.toggleOtherIndustryField();

this.filteredIndustries = (<FormGroup>this.form.controls.industry).controls.searchIndustriesInput.valueChanges
.pipe(
startWith(''),
map(value => this.filter(value)),
);
}

private filter (value: string | IIndustry): IIndustry[] {
if (this.language === 'en') {
console.log(this.language);
if (value && (<IIndustry>value).name) {
return this.industries;
}

const filterValue = (<string>value).toLowerCase();

return this.industries.filter(option => option.name.toLowerCase().includes(filterValue));
}

if (value && (<IIndustry>value).nameCZ) {
return this.industries;
}

const filterValue = (<string>value).toLowerCase();

return this.industries.filter(option => option.nameCZ.toLowerCase().includes(filterValue));

}

displayIndustries (industry: IIndustry) {
if (industry) {
this.afterDisplayIndustries = true;
console.log((this.language === 'en') ? industry.name : industry.nameCZ);
return (this.language === 'en') ? industry.name : industry.nameCZ;
}
return undefined;
}

buildForm(): void {
this.form = this.fb.group({
industry: this.fb.group({
searchIndustriesInput: this.customerPreference.industry,
id: [this.customerPreference.industry.id, [
Validators.required,
]],
});



}





get industryForm(): FormGroup {
return this.f.industry as FormGroup;
}



get f() {
return this.form.controls;
}

industrySelected(e?) {
const industryForm = <FormGroup>this.form.controls.industry;
industryForm.controls.id.setValue(industryForm.controls.searchIndustriesInput.value.id);
this.toggleOtherIndustryField();
}


}

有没有人可以帮助我..提前致谢。

最佳答案

一个简单的 *ngIf 就可以完成这项工作,请记住,当我们选择一个选项时,我们是在选择数组中的一个项目——英语或捷克语描述是特定项目的子项;为了显示完整的演示,我在顶部插入了一个单选按钮以在两种语言之间切换...

相关HTML:

<mat-radio-group aria-label="Select an option" [(ngModel)]="selectedLanguage">
<mat-radio-button value="english">English</mat-radio-button>
<mat-radio-button value="czech">Czech</mat-radio-button>
</mat-radio-group>
<br/>
<p> selected language: {{selectedLanguage}} </p>
<br/>

<mat-form-field>
<mat-label>select option</mat-label>
<mat-select [formControl]="langSelect">
<mat-option *ngFor="let item of dualArray" [(value)]="item">
<!-- {{item.id}} -->
<ng-container *ngIf="selectedLanguage == 'english'">
{{item.name}}
</ng-container>
<ng-container *ngIf="selectedLanguage == 'czech'">
{{item.nameCZ}}
</ng-container>
</mat-option>
</mat-select>
</mat-form-field>
<p>You selected:
<ng-container *ngIf="selectedLanguage == 'english'">
{{langSelect.value?.name}}
</ng-container>
<ng-container *ngIf="selectedLanguage == 'czech'">
{{langSelect.value?.nameCZ}}
</ng-container>
</p>

相关TS:

import { Component } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

/** @title Select with 2-way value binding */
@Component({
selector: 'select-value-binding-example',
templateUrl: 'select-value-binding-example.html',
styleUrls: ['select-value-binding-example.css'],
})
export class SelectValueBindingExample {
selected = '';
selectedLanguage = 'english';
langSelect = new FormControl('', []);
dualArray = [
{ id: 19, name: "Travel Recreation and Leisure", nameCZ: "test19" }
, { id: 20, name: "Wholesale and Distribution", nameCZ: "test20" }
, { id: 1, name: "Agriculture and Mining", nameCZ: "test1", description: "hello" }
, { id: 2, name: "Business Services", nameCZ: "test2" }
, { id: 3, name: "Computer and Electronics", nameCZ: "test3" }
, { id: 4, name: "Consumer Services", nameCZ: "test4" }
, { id: 5, name: "Education", nameCZ: "test5" }
, { id: 6, name: "Energy and Utilities", nameCZ: "test 6" }
, { id: 7, name: "Financial Services", nameCZ: "test 7" }
, { id: 8, name: "Government", nameCZ: "test 8" }
, { id: 9, name: "Health, Pharmaceuticals, and Biotech", nameCZ: "test9" }
, { id: 10, name: "Manufacturing", nameCZ: "test10" }
, { id: 11, name: "Media and Entertainment", nameCZ: "test11" }
, { id: 12, name: "Non-profit", nameCZ: "test12" }
, { id: 13, name: "Other", nameCZ: "test13" }
, { id: 14, name: "Real Estate and Construction", nameCZ: "test14" }
, { id: 15, name: "Retail", nameCZ: "test15" }
, { id: 16, name: "Software and Internet", nameCZ: "test16" }
, { id: 17, name: "Telecommunications", nameCZ: "test17" }
, { id: 18, name: "Transportation and Storage", nameCZ: "test18" }
];
}

完成working stackblitz here

关于javascript - Angular Material : How to refresh a drop-down list after a certain condition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56539434/

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