我正在使用角度js,我的数据来自json数组中的api。我需要根据日历中选定的日期范围添加过滤器。我的日期来自两个字段“from”和“to”,或者它可以是一个日期,我需要根据json中的到期日期显示记录。
如果用户选择日期范围并单击搜索,那么我需要获取所选记录。这是我在控制器文件中的js。
localStorageService.set('usersOpen', users.openUsers);
localStorageService.bind($scope, 'usersOpen');
users.myUsers = myUsers;
function myUsers(start_date,end_date){
DataService.myOpenUsers()
.then(function successCallback(response) {
$scope.usersOpen.length = 0;
$scope.usersOpen.push({'data': response.data.results});
}, function errorCallback(response) {
});
这是我需要过滤的json格式。
{
"results": [{
"id": "999896",
"description": "Description Testingggg",
"picturesUrl": [],
"assigID": [{
"userId": "67767776",
"email": "test@gmail.com",
"firstName": "test",
"lastName": "User",
"pictureUrl": "",
"notifyStatus": "None",
}],
"expiryDate": "2016-10-14T17:48:50.100Z",
"createdUserId": "8887900",
"status": "Open",
"dtModified": "2016-10-14T12:21:27.431Z"
},{
"id": "999897",
"description": "New Tested Description",
"picturesUrl": [],
"assignedTo": [{
"userId": "887997",
"email": null,
"firstName": "testing",
"lastName": "users2",
"pictureUrl": null,
"notifyStatus": "None",
"devices": []
}, {
"userId": "887997",
"email": "testing@hotmail.com",
"firstName": "Testing",
"lastName": "User2",
"pictureUrl": "",
"notifyStatus": "None",
}],
"expiryDate": "2016-10-11T09:46:09.100Z",
"createdUserId": "887997",
"status": "Open",
"location": {
"lat": 12.1,
"lon": -3.1
},
"dtModified": "2016-10-11T09:36:30.217Z"
}]
}
这是html,我正在获取记录。
<ul>
<li ng-repeat="users in usersOpen[0].data track by $index" class="{{users.status}} {{yit.id}}">
<div class="content-box">
<div class="text-box">
{{users.description}}
</div>
{{users.expiryDate}}
</div>
{{users.firstName}}
</li>
</ul>
答案 0 :(得分:1)
您可以创建自定义过滤器来过滤日期范围,例如
过滤强>
app.filter('dateFilter', function() {
return function(input, start, end) {
var inputDate = new Date(input),
startDate = new Date(start),
endDate = new Date(end),
result = [];
for (var i = 0, len = input.length; i < len; i++) {
inputDate = new Date(input[i].DepartureDateTime);
if (startDate < inputDate && inputDate < endDate) {
result.push(input[i]);
}
}
return result;
};
});
并以HTML格式申请
<div ng-controller="MyController">
<div ng-repeat="data in Combination | dateFilter:startDate:endDate ">
<div >{{data.DepartureDateTime}}</div>
</div>
</div>
<强> DEMO 强>
答案 1 :(得分:0)
你可以使用angularjs $ filter,你需要在控制器中注入$ filter并以你想要的任何格式过滤。
$filter('date')(new Date(expiryDate), "M/d/yyyy");
要在2个日期范围内获取用户数据,请使用此代码
var fromTime = new Date("2015-04-23").getTime();
var toTime = new Date("2015-04-28").getTime();
var filteredDataByDates = [];
var row, date;
for (i in data) {
row = data[i];
date = new Date(row[4]);
if (date.getTime() >= fromTime && date.getTime() <= toTime) {
filteredDataByDates.push(row);
}
}
答案 2 :(得分:-2)
对于您的问题,您可以使用本机Javascript过滤器:
response.data.results.filter(function(user){
return //add your date test here on user.expiryDate
}