2017-06-20 40 views
0

我正在使用智能表(1)允許用戶從不同的頁面選擇記錄,這些記錄顯示在預覽div中。如何使用智能表更改頁面後繼續選擇?

當前的問題是,當我從第一頁選擇一些記錄時,導航到秒,然後導航回到第一頁,第一頁中的選擇內容不反映先前的選擇。

當前選擇如果保存在名爲operatorSelection(2)的映射中,並且我的第一次嘗試是在onChangedSource事件(3)後再次選擇行,但即使在調用multipleSelectRow()之後,getSelectedRows()也不會返回預期的行。

頁面更改後有沒有其他的方式來保持選擇?或以其他方式從代碼中選擇行?

下面是代碼片段:

(1)成分,它的HTML

<div class="col-lg-6"> <ng2-smart-table #grid [settings]="settings" [source]="source" (userRowSelect)="onUserRowSelect($event)"> </ng2-smart-table> </div>

(2)用於行選擇的分量代碼

onUserRowSelect(event) { 
console.log('on UserRowSelect', event, this.operatorSelection); 

if (event.isSelected) { 
    this.operatorSelection.set(event.data.code, event.data); 
} else { 
    this.operatorSelection.delete(event.data.code); 
} 

this.updatePreview(); 
} 

(3)什麼我試圖在更改頁面後更新選擇

ngAfterViewInit(): void { 
this.table.grid.source.onChangedSource.subscribe(() => { 
    this.operatorSelection.forEach((row) => { 

    this.table.grid.source.data.forEach((element) => { 
     if (element.code == row.code) { 
     console.log('--- reselect ', row.code); 
     this.table.grid.multipleSelectRow(row); 
     console.log('*** selected ', this.table.grid.getSelectedRows()); 
     } 
    }); 
    }); 
}); 
} 

回答

0

Angular在遍歷組件時使用本地存儲來存儲數據,通過上面提到的數據源刷新onInit組件。

您必須使用服務來存儲您希望保留的數據,同時通過組件的onInit遍歷並將此數據設置爲數據源。

相關問題