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



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

请问还有其他建议吗?

你可以这样做:

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

    <p-table #pTableId></p-table>
    
  2. 然后在组件中:

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

为什么要添加此 id? 你可以做这样的事情

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

并在您的组件中

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

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

最新更新