Search code examples
angularhtml-tablehyperlinkdownloadprimeng

Add link in a table Angular 8


I have a table with some columns and I have a "documents" column that can be clicked and downloaded. How can I ONLY make the document name a link?

HTML

<p-table #dt3 [columns]="colsPermessi" [value]="permessi" [paginator]="true" [scrollable]="false" [rows]="10" [autoLayout]="true">

<ng-template pTemplate="header" let-columns>
    <tr>
       <th *ngFor="let col of columns" pResizableColumn>{{col.header}} </th>
       <th>Azioni</th>
       <th>Permessi</th>
    </tr>
 </ng-template>

  <ng-template pTemplate="body" let-rowData let-columns="columns">
     <tr [pSelectableRow]="rowData">
         <td *ngFor="let col of colsPermessi">
            {{rowData[col.field]}}
         </td>
     </tr>
  </ng-template>

enter image description here

I therefore want the "document" column to be clickable and on click it calls the "downloadFile ()" function. How can I do this?


Solution

  • This can be accomplished using *ngIf to separate your cases. For documento columns, use the ngIf to insert a hyperlink tag. For all else, just insert the text. See the example below:

      <ng-template pTemplate="body" let-rowData let-columns="columns">
         <tr [pSelectableRow]="rowData">
             <td *ngFor="let col of colsPermessi">
                <span *ngIf="col.header == 'Documento'><a [href]="col.url">{{rowData[col.field]}}</a></span>
                <span *ngIf="col.header != 'Documento'>{{rowData[col.field]}}</span>
             </td>
         </tr>
      </ng-template>