2017-03-24 37 views
0

我有一個下拉菜單中的項目列表,當前每當我點擊/選擇其中一個下拉項目時,它被添加到不同的數組,然後我想從列表中刪除它。刪除下拉菜單中的拼接項目

這是我有:

<li *ngFor="let item of dropdownlist "> 
    <a (click)="select(item) " class="dropdown-item "> 
     <i class="fa fa-fw " [ngClass]="{ 'fa-check': item.checked, 'glyphicon-none': !item.checked} "></i> 
    </a> 
</li> 

select(item: any) { 
    item.checked = !item.checked; 
    this.containers.push(item); 
    this.dropdownlist.splice(item.checked); 
    } 

dropdownlist: Array<any> = []; 

containers: Array<Panel> = [ 
    new Panel(1, 'Test1', 'export data', 'test data in modal'), 
    new Panel(2, 'Test2','export image', 'more test data'), 
    new Panel(3, 'Test3', 'export data', 'more and more data') 
]; 

我的問題是,當下拉只有一個項目,我嘗試將其添加到另一個陣列它不會從下拉列表中刪除。

回答

1

.splice()可以採取多個參數:

array.splice(start) 
array.splice(start, deleteCount) 
array.splice(start, deleteCount, item1, item2, ...) 

在你的情況,以確保您只刪除您想要的項目,確保第二個參數= 1和您在該項目的索引通路要刪除:

<li *ngFor="let item of dropdownlist; let i = index "> 
    <a (click)="select(item, i) " class="dropdown-item "> 
     <i class="fa fa-fw " [ngClass]="{ 'fa-check': item.checked, 'glyphicon-none': !item.checked} "></i> 
    </a> 
</li> 

select(item: any, index: number) { 
    item.checked = !item.checked; 
    this.containers.push(item); 
    this.dropdownlist.splice(index, 1); 
} 
+0

謝謝你這個完美的工作! – bluePearl