1

I want to display the date in each column of my datatable. The date is comming via json and looks like:

JSON Date

1504836960000

Now i am formatting it via piping and ng-template:

<ng-template pTemplate="body" let-order="rowData">
   {{order.sla.slaEnd | date:'yMdjm'}}
</ng-template>

I am getting a date looking like this: 8.9.2017, 04:16

Whole column code

<p-column field="sla.slaEnd" header="SLA">
    <ng-template pTemplate="body" let-order="rowData">
        {{order.sla.slaEnd | date:'yMdjm'}}
    </ng-template>
</p-column>

The problem is, when sla.slaEnd == null i am getting an error and my page collapses. I have tried a lot but i didn't get it to check if sla.slaEnd != null. I want to check if it is null and when it is null he should display nothing like ''. When it is not null then just show my formatted date. Anybody knows how to solve this issue?

2
  • why not wrap it in <ng-template *ngIf="sla.slaEnd != null"> //your <p> </p></ng-template> ? Commented Nov 3, 2017 at 12:45
  • Doesn't work because sla is not a variable in my component.ts but an object of my datatable value. Commented Nov 3, 2017 at 12:54

2 Answers 2

1

Try the safe navigation operator.

{{order.sla?.slaEnd}}

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried *ngIf="":

<p-column *ngIf="sla.slaEnd" field="sla.slaEnd" header="SLA">
    <ng-template pTemplate="body" let-order="rowData">
        {{order.sla.slaEnd | date:'yMdjm'}}
    </ng-template>
</p-column>

1 Comment

This is not what i want. If sla.slaEnd is null the whole column will not be displayed. But i want it always to be there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.