如何在兩個日期之間過濾記錄? - How to filter records between two dates? -开发者知识库

如何在兩個日期之間過濾記錄? - How to filter records between two dates? -开发者知识库,第1张

I am working in AngularJS and i need to add filter according to selected date range from calendar. My dates are coming in two fields "from" and "to" and i need to show records according to expiry date which is coming in json array.

我在AngularJS工作,我需要根據日歷中選定的日期范圍添加過濾器。我的日期來自兩個字段“from”和“to”,我需要根據到期日期顯示記錄,該日期是json數組。

when user select the date range and click on search then i need to get selected records between that two dates.This is my js in controller file where i am getting start and end date in this format "yyyy-mm-dd".Please help.

當用戶選擇日期范圍並點擊搜索時,我需要在兩個日期之間獲取所選記錄。這是我在控制器文件中的js,我以這種格式“yyyy-mm-dd”獲取開始和結束日期。請幫助。

 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) {

            });

From data service i am getting this json 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"
  }]
}

This is html where i am getting records.

這是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> 

2 个解决方案

#1


1  

You can define a filter, using filter method:

您可以使用filter方法定義過濾器:

angular.module('app').filter('dateBetween', function() {
     return function(data, config) {
         if(!angular.isArray(data)) return data;
         var result = [];
         var start = config.start;
         var end = config.end;
         var dateProperty = config.dateProperty;
         data.forEach(function(chunk) {
             if((chunk[dateProperty].getTime() <= end.getTime() && check.getTime() >= start.getTime())
                result.push(chunk);
         });

         return result;
     }
)

Say you have a model in $scope usersOpen, you can apply the filter in your html code, and the endDate, startDate are also property from the same controller (I'dont know the logic you used to retrieve dates ):

假設你在$ scope usersOpen中有一個模型,你可以在你的html代碼中應用過濾器,而endDate,startDate也是來自同一個控制器的屬性(我們不知道你用來檢索日期的邏輯):

<ul>
   <li ng-repeat="user in usersOpen | dateBetween : { start : startDate, end : endDate }"> {{ user.name }} ... </li>
</ul>

usersOpen should be an array of users so the filter I linked (which is clear I think) to apply filtering.

usersOpen應該是一個用戶數組,所以我鏈接的過濾器(我認為很清楚)應用過濾。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复