user3527538
user3527538

Reputation: 39

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 ?

Upvotes: 1

Views: 5690

Answers (2)

Bogdan
Bogdan

Reputation: 696

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

Upvotes: 6

Dima Shivrin
Dima Shivrin

Reputation: 99

Why are you trying to add this id? You can do something like this

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

and in your component

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

You can Refer to the table in your function (this.myTable) and look for children if that's what you are looking for. Let me know if that helped

Upvotes: 0

Related Questions