Search code examples
cssangulartypescriptprimengprimeng-datatable

How to style a row in a table?


I'm having a little issue styling a row in my table. Basically My table has 4 rows.

a) If the value under column Title 5 is less than 0 then the whole row needs to be in red color

b) If the value under column Title 5 is equal to 17 than the whole row needs to be in orange color.

c) Any other row should NOT have any color.

I have most of it working. My issue is:

If a row doesn't have any color I still want to make the check icon green.

Here's my working code:

PLUNKER

<p-panel  class="a-panel-checkIn" header='Compliance Tracking' [toggleable]="true" [collapsed]="isPanelCollapsed" [style]="{ height: '100%' }">
  <p-table [value]="complianceTracking" [columns]="cols" selectionMode="single">
  <ng-template pTemplate="header" let-columns>
  <tr>
    <th *ngFor="let col of columns">
      {{col.header}}
    </th>
  </tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-columns="columns">
  <tr [ngClass]="rowData.field5 < 0 ? 'a-danger-row' : (rowData.field5 === 17) ? 'a-yellow-row' : 'a-green-row'">
    <td *ngFor="let col of columns" [ngClass]="rowData[col.field] === '' ? 'a-check-icon' : ''">
      {{rowData[col.field]}}
    </td>
  </tr>
  </ng-template>
  </p-table>
</p-panel>

Solution

  • You can replace the following code for your second ng-template in your app.template.html:

    <ng-template pTemplate="body" let-rowData let-columns="columns">
      <tr [ngClass]="rowData.field5 < 0 ? 'a-danger-row' : (rowData.field5 === 17) ? 'a-yellow-row' : ''">
        <!-- <td *ngFor="let col of columns" [ngClass]="rowData[col.field] === '' ? 'a-check-icon' : ''"> -->
        <td *ngFor="let col of columns" [ngClass]="rowData[col.field] === '' ? ((rowData.field5 > 0 && rowData.field5 !== 17) ? 'a-check-icon a-green-check' : 'a-check-icon' ) : ''">
          {{rowData[col.field]}}
        </td>
      </tr>
    </ng-template>
    

    In your styles.css, create a new css rule as following:

    .a-green-check {
       color: #A8CF45;
    }
    

    Here is the Plunkr

    Hope this is what you wanted to achieve.