2017-09-01 76 views
1

我正在使用n2-handsontable角4爲一個項目。當數據發生變化時,我似乎無法弄清楚如何刷新表格。我發現這個職位:刷新Handsontable角4

Refresh a handsontable

這似乎是我想要的,但我無法弄清楚如何訪問handsontable對象。我最初的想法是使用#進行綁定,但它不按預期工作,它只是將'undefined'傳遞給函數。

component.html

<button class="btn btn-default" (click)="add(hot)">Add</button> 
<hotTable [data]="_dataHot" 
        [colHeaders]="_columnHeadersHot" 
        [columns]="_columnsHot" 
        [options]="{contextMenu: true}" 
        #hot> 
</hotTable> 

component.ts

add(hot){ 
    //do stuff 
    hot.render(); 
} 

編輯:我可能會迫使一個做ngIf技巧渲染,但似乎並不像一個很好的解決方案。

回答

2

我最初有同樣的問題,並無法弄清楚如何訪問渲染功能。在component.ts文件中使用以下行對我來說是個訣竅。

hot.getHandsontableInstance().render(); 

我不確定的是爲什麼通過#hot返回未定義給你。我在下面列出我的代碼片段。

component.html

<hotTable 
    [data]="data" 
    (afterChange)="afterChange($event, hot)" 
    [colHeaders]="colHeaders" 
    [columns]="columns" 
    [options]="options" 
    [colWidths]="colWidths" 
    #hot> 
</hotTable> 

component.ts

private afterChange(e: any, hot) { 
    // some commands 
    hot.getHandsontableInstance().render(); 
} 
+0

我很笨,我沒有意識到我需要使用#hot,並在使用@viewchild()時將對象傳遞給函數。謝謝! – Calidus

2

您需要使用ViewChild裝飾器在代碼中獲取對hot的引用。您可以將其存儲在相同名稱的變量中。現在

@ViewChild('hot') hot 

可以爲this.hot在類訪問該組件的實例。

+0

感謝您的幫助,'ViewChild'是在方向上的步驟。看起來'render()'不是handtable組件的有效函數。有一個'getHandsontableInstance()'返回'render()'''handsontable'類,但是'getHandsontableInstance()'在嘗試和使用時返回undefined。 – Calidus