首页 文章

刷新Handsontable Angular 4

提问于
浏览
2

我正在使用角度为4的n2-handsontable进行项目 . 我似乎无法弄清楚当数据发生变化时如何刷新表格 . 我发现这篇文章:

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 回答

  • 3

    我最初遇到了同样的问题,但无法弄清楚如何访问渲染功能 . 在component.ts文件中使用以下行为我提供了一些技巧 .

    hot.getHandsontableInstance().render();
    

    我不确定的是为什么传递#hot为你返回undefined . 我在下面列出我的代码片段 .

    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();
    }
    
  • 4

    您需要使用 ViewChild decorator在代码中获取对 hot 的引用 . 您可以将其存储在同名变量中 .

    @ViewChild('hot') hot
    

    现在,您可以将类中的组件实例作为 this.hot 来访问 .

相关问题