Search code examples
angularprimengprimeng-turbotable

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


I need to add an ID to the table, I'm using PrimeNG so I don't have this table tag in my component, only the p-table. I tried [id] and [attr.id] but without success.

Any other suggestions please ?


Solution

  • You can do this way:

    1. Mark p-table with an id for accessing his reference in ngAfterViewInit method:

      <p-table #pTableId></p-table>
      
    2. Then in 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');
          }
      }