【问题标题】:Add id attribute to <table> tag using PrimeNG p-table使用 PrimeNG p-table 将 id 属性添加到 <table> 标记
【发布时间】:2018-12-01 17:07:37
【问题描述】:

我需要向table 添加一个ID,我使用的是PrimeNG,所以我的组件中没有这个表标签,只有p-table。 我尝试了[id] 和[attr.id],但没有成功。

还有其他建议吗?

【问题讨论】:

    标签: angular primeng primeng-turbotable


    【解决方案1】:

    你可以这样做:

    1. 在 ngAfterViewInit 方法中使用 id 标记 p-table 以访问他的引用:

      <p-table #pTableId></p-table>
      
    2. 然后在 Component.ts 中:

      import {Table} from 'primeng/table';
      export class MyComponent  {
          @ViewChild('pTableId') pTableRef: Table;
      
          ngAfterViewInit() {
              const table = this.pTableRef.el.nativeElement.querySelector('table');
              table.setAttribute('id', 'myTableId');
          }
      }
      

    【讨论】:

      【解决方案2】:

      您为什么要尝试添加此 ID? 你可以这样做

       &lt;p-table #some-id&gt; &lt;/p-table&gt;

      在你的组件中

      @ViewChild('some-id') myTable: any;
        

      您可以参考函数中的表 (this.myTable) 并查找子项(如果这是您要查找的内容)。 让我知道这是否有帮助

      【讨论】:

      • 这是 QA 要求。此解决方案无济于事,因为我没有该表作为我的表的子表,并且我无权访问它。
      猜你喜欢
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多