Represent boolean values in the form of 2 different icons (one for true, one for false) in PrimeNG DataTable

时间:2017-07-10 15:27:45

标签: datatable icons primeng ngif

I want to represent boolean values in the form of icons for a column field in data table primeng. Following is a piece of code:

<p-dataTable [value]="ARRAY_METADATA" rowHover="true">
    <p-column field="id" header="Field ID" [sortable]="true"></p-column>
    <p-column field="booleanField" header="Boolean Field" [sortable]="true"></p-column>
</datatable>

How am I supposed to show maybe a "Tick" for true values and "cross" for false values for the booleanField?

<span class="badge">BOOLEAN VAUE</span>

I guess the above code works well in case of pure HTML. But again how am I suppose to put the conditional statement to output two different icons for different boolean values? Any quick thoughts??

I tried using ngIf but it still does not display the way I need. It simply displays the content of ng-template:

<p-column field="someStringField" header="Some String Field">
    <div *ngIf="someStringField; else elseBlock">
        <button  type="button" pButton  icon="fa-check"></button>
    </div>
    <ng-template #elseBlock pTemplate="body" >
        <button  type="button" pButton  icon="fa-times"></button>
    </ng-template>
</p-column>

1 个答案:

答案 0 :(得分:0)

我相信您必须将要在列中显示的任何内容放在ng-template中

<ng-template let-col="rowData" pTemplate="body">
    <button *ngIf="col.someValue" type="button" pButton  icon="fa-check"></button>
    <button *ngIf="!col.someValue" type="button" pButton  icon="fa-times"></button>
</ng-template>