[英]Cascading dropdown Edit is not working in angular?
This is my object: 这是我的对象:
const ELEMENT_DATA: Element[] = [
{id: 1, name: 'Hydrogen', symbol: 'H'},
{id: 2, name: 'Hydrogen', symbol: 'H1'},
{id: 3, name: 'Helium', symbol: 'He'}
];
which is displayed in datatable with Edit buttons, like so: 使用“ 编辑”按钮显示在数据表中,如下所示:
When I click Edit (for example I clicked Hydrogen ) it should populate with 当我单击“ 编辑” (例如,我单击“ 氢” )时,它应填充为
name: 'Hydrogen', symbol: 'H'
. name: 'Hydrogen', symbol: 'H'
。
But now I am getting the Symbol List dropdown empty. 但是现在我将符号列表下拉列表留空。
When I click the Add button, a pop up will come with two dropdowns: Element List and Symbol List . 当我单击添加按钮时,将弹出一个带有两个下拉列表的窗口: 元素列表和符号列表 。 Based on the Element name Symbol List will come.
基于元素名称的符号列表将会出现。
Now when I click the Edit button in datatable, that should populate that particular row in the popup. 现在,当我单击数据表中的“编辑”按钮时,应该在弹出窗口中填充该特定行。 How can I do this?
我怎样才能做到这一点?
html html
<form [formGroup]="addTaskForm" (ngSubmit)="save()" >
<mat-form-field>
<mat-select formControlName="name" placeholder="Element List" (selectionChange)="elementSelectionChange($event)">
<mat-option *ngFor="let element of Elements" [value]="element.name">
{{ element.name }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select formControlName="symbol" placeholder="Symbol List">
<mat-option *ngFor="let element of selectedElementSymbol" [value]="element.symbol">
{{ element.symbol }}
</mat-option>
</mat-select>
</mat-form-field>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">Cancel</button>
<button type="submit" mat-button cdkFocusInitial>Add</button>
</div>
</form>
The symbol list is initialized in the method elementSelectionChange
, but you are calling elementSelectionChange($event)
only when the Element list selection changes. 符号列表在
elementSelectionChange
方法中初始化,但是仅当Element列表选择更改时,才调用elementSelectionChange($event)
。
<mat-select formControlName="name" placeholder="Element List" (selectionChange)="elementSelectionChange($event)">
One way you can do it is, 一种方法是
elementSelectionChange(event) {
this.loadSymbols(event.value);
}
loadSymbols(name) {
let value = this.Elements.find(e => e.name === name);
this.selectedElementSymbol = this.Symbols.filter(e => e.id === value.id);
console.log(this.selectedElementSymbol);
}
and then call loadSymbols
in the constructor
然后在
constructor
调用loadSymbols
constructor() {
if (data.element) {
this.name = data.element.name;
this.symbol = data.element.symbol;
this.loadSymbols(this.name);
}
}
You shoule update datasource
after each value added from the dialog, change your save()
function like this: 在对话框中添加每个值之后,您应该更新
datasource
,像这样更改save()
函数:
save() {
const data = this.data.originalform.dataSource.data;
this.addTaskForm.value["id"]=data.length+1
data.push(this.addTaskForm.value);
this.data.originalform.dataSource=new MatTableDataSource<Element>(data);
console.log('working');
console.log(this.addTaskForm.value);
}
And to not forget to add a referer to the parent object originalform
from this: 并且不要忘记从此向父对象
originalform
添加引用:
let dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
width: '250px',
data: { element: element,originalform:this }
});
Hope this helps:) 希望这可以帮助:)
Edit: 编辑:
By call of this comment , i updated the fiddle in the following to fit the paginating in. 通过此评论 ,我在下面更新了小提琴以适应分页。
Adding paginator figures as following: 添加分页器图形,如下所示:
this.data.originalform.dataSource.paginator=this.data.originalform.paginator;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.