Martin Brandl Martin Brandl - 4 months ago 11
HTML Question

ng-repeat hide a property if its equals DateTime.MinValue

Im trying to hide cells (not the complete row) containg a date that is equal to

DateTime.MinValue
which is basically
0001-01-01T00:00:00
.

Here is my view model:

$scope.vm.users = [
{
"Username": "user1",
"EarliestLogin": "0001-01-01T00:00:00",
"LatestLogin": "0001-01-01T00:00:00"
},
{
"Username": "user2",
"EarliestLogin": "2016-07-15T11:18:19Z",
"LatestLogin": "2016-07-15T11:18:19Z"
},
{
"Username": "user3",
"EarliestLogin": "0001-01-01T00:00:00",
"LatestLogin": "0001-01-01T00:00:00"
}
];


The table looks like that:

<table>
<tr>
<th>User Name</th>
<th>Earliest Login</th>
<th>Latest Login</th>
</tr>
<tr ng-repeat="user in vm.users">
<td>{{user.Username}}</td>
<td>{{user.EarliestLogin | date:'short'}}</td>
<td>{{user.LatestLogin | date:'short'}}</td>
</tr>
</table>





Current Output:

User Name Earliest Login Latest Login
user1 1/1/01 12:00 AM 1/1/01 12:00 AM
user2 7/15/16 1:18 PM 7/15/16 1:18 PM
user3 1/1/01 12:00 AM 1/1/01 12:00 AM


Desired Output:

User Name Earliest Login Latest Login
user1
user2 7/15/16 1:18 PM 7/15/16 1:18 PM
user3





I was able to hide the complete row using a filter on the
ng-repeat
but I don't know how to only hide a cell if its
DateTime.MinValue
...

Here is my plnkr.

Answer

At the first glance, you can create a helper function

$scope.vm.isDateTimeMinValue = function (value) {
    return value === "0001-01-01T00:00:00";
};

And use it in your template

<tr ng-repeat="user in vm.users">
    <td>{{user.Username}}</td>
    <td><span ng-hide="vm.isDateTimeMinValue(user.EarliestLogin)">{{user.EarliestLogin | date:'short'}}</span></td>
    <td><span ng-hide="vm.isDateTimeMinValue(user.LatestLogin)">{{user.LatestLogin | date:'short'}}</span></td>
</tr>

Plunker

Note! You can define "0001-01-01T00:00:00" as an Angular JS constant using the corresponding service.