【发布时间】:2017-12-18 09:54:50
【问题描述】:
我在行列上有一个 select dropdowd,我正在尝试添加一个带有新选择框的新行,其中删除了先前选择的值。我使用 ngModelChange 调用了一个函数,该函数将所选值分配给我的 ngModel 并将其从选择列表中删除。问题是每次我添加新行时,都会从所有行中删除值。
注意:我无法为我的项目创建 plunker
我的网格如下所示:
<tbody>
<tr *ngFor="let row of rows">
<td></td>
<td>
<select [ngModel]="row.color" (ngModelChange)="onSelectColor(row, $event)"> // value is a string or number
<option *ngFor="let obj of availableColors">{{obj}}</option>
</select>
</td>
<td *ngFor="let headItem of headList"><input class="input" type='text' #qty/></td>
</tr>
</tbody>
<button (click)='addRow()'>Add a row</button>
我的组件我有这个:
export class ReferenceTableComponent implements OnInit {
observable: Observable<any>;
//couleurs exp: ['first', 'seconde']
@Input() public datas: Array<string>;
public availableColors: Array<string>;
//exp: ['1' ,'2', '3']
@Input() public headList: Array<string>;
public rows: Array<any> = [{}];
public rowIDs: Array<any> = [];
constructor(private injector: Injector) {
}
ngOnInit() {
this.computeAvailableColors();
}
addRow() {
this.rows.push({});
}
computeAvailableColors() {
this.availableColors = _.concat([''], this.datas);
console.log(_.map(this.rows, 'color'))
this.rowIDs = _.map(this.rows, 'color')
this.availableColors = _.difference(this.availableColors, this.rowIDs);
}
onSelectColor(row, color) {
row.color = color;
this.availableColors = this.availableColors.filter(c => c !== color);
}
}
datas 和 headlist 由我的服务注入
如您所见,第一个选择的颜色“BLANC NEIG”已根据需要从选择中删除,但也从第一行中删除
【问题讨论】:
-
您是说不能从选择选项中删除颜色吗?
-
@kimy82 是添加新行时。
-
@kyserslick 创建一个 plunker。你的代码让我很困惑。并添加截图
-
@Aravind 无法创建 plunker,我添加了截图
-
@Aravind 是的,我在线
标签: angular typescript