使用 PrimeNG p-table 将 id 属性添加到 <table> 标签

Add id attribute to <table> tag using PrimeNG p-table

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

还有其他建议吗?

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

 <p-table #some-id> </p-table>

并在您的组件中

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

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

你可以这样做:

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

    <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');
        }
    }