問題是:我從JSON格式的數據庫中獲取一些對象的列表。如何在angularjs中按日期過濾對象列表
[{"id":1,"klassBean":{"id":1,"klassName":"11A"},"fname":"Vornic","sname":"Pavel","bdate":1499893200000,"sex":"M","telephone":111,"address":"Soroca"},{"id":2,"klassBean":{"id":1,"klassName":"11A"},"fname":"Gaidarji","sname":"Raisa","bdate":1499893200000,"sex":"F","telephone":222,"address":"Leova"}]
每個對象都包含字段類型Date。我使用standart角度過濾器通過ng-model
映射的某些輸入的輸入信息來顯示網格。用字符串類型,它工作正常。日期以毫秒爲單位進行轉換,但在輸入字段中由datapicker引入字符串。
<tbody>
<tr>
<th></th>
<th><input type="text" ng-model="fname" class="form-control" /></th>
<th><input type="text" ng-model="sname" class="form-control" /></th>
<th>
<select ng-model="sex" class="form-control">
<option></option>
<option value="F">F</option>
<option value="M">M</option>
</select>
</th>
<th><input type="text" ng-model="telephone" class="form-control" /></th>
<th><input type="text" ng-model="address1" class="form-control" /></th>
<th><input type="text" ng-model="bdate1 " class="form-control datepicker" /></th>
</tr>
</tbody>
<tfoot data-ng-repeat="pupil in pupilList | filter : {fname : fname, sname : sname, sex:sex, telephone:telephone,address:address1, bdate:bdate2} ">
<tr>
<td style="width: 10px;">
<a th:href="@{'/pupil/edit/' + {{pupil.id}}+}">
<i class="fa fa-pencil-square-o" aria-hidden="true" title="Vista"> </i>
</a>
</td>
<th>{{pupil.fname}}</th>
<th>{{pupil.sname}}</th>
<th>{{pupil.sex}}</th>
<th>{{pupil.telephone}}</th>
<th>{{pupil.address}}</th>
<th>{{pupil.bdate|date : 'yyyy-MM-dd'}}</th>
</tr>
</tfoot>
我格式化日期由日期過濾器的經典視圖,但實際上它仍然以毫秒爲單位,因此在控制我做下一件事情:以毫秒爲單位減3小時
$scope.$watch('bdate1', function (newValue) {
if (angular.isUndefined($scope.bdate1)){
$scope.bdate2=1499893200000;
return;
}
// minus 3 hours timezone
$scope.bdate2=new Date($scope.bdate1).getTime()-10800000;
});
字符串場bdate1取得日期。這個值傳遞給其他變量$ scope.bdate2並最終與它進行比較。它工作,但代碼味道。你怎麼解決這個問題 ?
你爲什麼不使用'在angularjs date'過濾器? –
我用過濾器顯示日期'
檢查出我的解決方案 –