自定义 Angular 日期范围过滤器,以仅显示该日期范围内的项目

Custom Angular date range filter to display items only in that date range

本文关键字:日期 显示 范围内 项目 Angular 范围 过滤器 自定义      更新时间:2023-09-26

im 刚接触 Angularjs 和 im 构建一个通过 json 收集文章的应用程序,json 和我希望能够在单击按钮时显示特定日期范围内的项目.我有逻辑,但我不知道如何从角度前景来处理它。我在一个div中有日期过滤器,在另一个div中有内容。请参阅下面的代码。提前致谢

我的网页

      <div class="col-md-3" data-ng-controller="FeedCtrl">
     <fieldset class="standard">
        <div layout="row" layout-wrap flex >
          <div flex="100">
             <md-checkbox aria-label="SPORTS">
                All ({{((feeds | filter:searchFunction).length) + ((videos | filter:searchFunction).length )+ ((articleContent | filter:searchFunction).length) }})
            </md-checkbox>
            <md-checkbox aria-label="TECH">
                Business News (2)
            </md-checkbox>
            <md-checkbox aria-label="MOVIES">
                Lifestyle News (5)
            </md-checkbox>
             <md-checkbox aria-label="MOVIES">
                 Sport News (1)
            </md-checkbox>
             <md-checkbox aria-label="MOVIES">
                 Tech News (3)
            </md-checkbox>
          </div>            
        </div>
    </fieldset>
      <fieldset class="standard" >
        <div layout="row" layout-wrap flex>
          <div flex="75">
             <md-checkbox aria-label="Anytime" href ng-click="eventDateFilter('all')">
                Anytime
            </md-checkbox>
            <md-checkbox aria-label="24Hourz">
                Last 24 Hours
            </md-checkbox>
            <md-checkbox aria-label="5days">
                Past 5 Days
            </md-checkbox>
             <md-checkbox aria-label="PastWeek" href ng-click="eventDateFilter('pastWeek')">
                 Past Week
            </md-checkbox>
             <md-checkbox aria-label="PastMonth">
                 Past Month
            </md-checkbox>
          </div>            
        </div>
    </fieldset>
  </div>
  <div class="col-md-9" data-ng-controller="FeedCtrl">
       <form name="searchForm">
          <md-content>
            <h1>You searched for: {{searchFunction}}</h1>
            <md-input-container class="md-block">
                  <label>Enter your search terms</label>
                  <input required name="clientName" ng-model="searchFunction">
            </md-input-container>
          </md-content>
       </form>
        <md-content>
          <md-tabs md-dynamic-height md-border-bottom>
            <md-tab label="Videos">
              <md-content class="md-padding">
                <h1 class="md-display-2">Videos</h1>
                 <md-list>
                    <md-list-item class="md-3-line">
                      <div class="md-list-item-text">
                       <div class="row">
                       <div ng-repeat="video in videos | filter:searchFunction" >
                           <a href="{{video.link}}">
                             <div class="col-md-4 video-item" >
                                <img src="images/video.png" width="240" class="face" style="padding-bottom:10px">
                                <div class="md-list-item-text">
                                  <h3>{{video.title}}</h3>
                                  <h4>{{video.publishedDate |  date : format : timezone}}</h4>
                                  <p>{{video.contentSnippet}}</p>
                                </div>
                             </div>
                           </a>
                         </div>
                       </div>
                      </div>
                      <md-divider ng-if="!$last"></md-divider>
                    </md-list-item>
                  </md-list>
              </md-content>
            </md-tab>
            <md-tab label="Images">
              <md-content class="md-padding">
                <h1 class="md-display-2">Images</h1>
                <md-list>
                    <md-list-item class="md-3-line" data-ng-controller="searchCtrl">
                       <div class="md-list-item-text">
                         <div class="row">
                         <a href="{{articles.link}}">
                           <div class="col-md-4 video-item" ng-repeat="articles in articleContent | filter:searchFunction">
                              <img src="{{articles.image}}" width="240" class="face" style="padding-bottom:10px">
                              <h3>{{articles.title}}</h3>
                              <p>{{articles.teaser}}</p>
                           </div>
                         </a>
                         </div>
                      </div>
                      <md-divider ng-if="!$last"></md-divider>
                    </md-list-item>
                </md-list>
              </md-content>
            </md-tab>
            <md-tab label="Articles">
              <div >
              <md-content class="md-padding">
                <h1 class="md-display-2">Articles</h1>
                <md-list>
                    <div ng-repeat="feed in feeds | filter:searchFunction" >
                    <a href="{{feed.link}}">
                    <md-list-item class="md-3-line list-item"  style="margin-bottom:20px;">
                      <img ng-src="{{feed.mediaGroups.url}}" class="face" style="padding-right:20px;">
                      <div class="md-list-item-text">
                        <h3>{{feed.title}}</h3>
                        <h4>{{feed.publishedDate |  date : format : timezone}}</h4>
                        <p>{{feed.contentSnippet}}</p>
                      </div>
                     <md-button class="md-warn md-raised md-hue-2">Read more</md-button>
                      <md-divider ng-if="!$last"></md-divider>
                    </md-list-item>
                    </a>
                    </div>
                </md-list>
              </md-content>
              </div>
            </md-tab>
          </md-tabs>
        </md-content>
  </div>

我的 Angular js 代码

   var Search = angular.module('searchApp', ['ngMaterial', 'ngMessages']);
   var CNNsport = "http://rss.cnn.com/rss/edition_sport.rss";
   var CNNlifestyle = "http://rss.cnn.com/rss/edition_entertainment.rss";
   var CNNtech = "http://rss.cnn.com/rss/edition_technology.rss";
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth()+1;
    var curr_year = d.getFullYear();
      Search.controller('searchCtrl',['$scope', '$location', function($scope, $location) {
        $scope.sortType     = 'name'; // set the default sort type
        $scope.sortReverse  = false;  // set the default sort order
        $scope.searchFunction   = '';     // set the default search/filter term
        $scope.redirectMe = function(evt){
            if (evt.which === 13) {  $location.path( "/search_page.html" );  }
          };
      }]);

      Search.controller("FeedCtrl", ['$scope','FeedService', function ($scope,Feed) {    
        $scope.feedSrc= "http://rss.cnn.com/rss/edition_sport.rss";
        $scope.feedvideo= "http://rss.cnn.com/rss/cnn_freevideo.rss";
        $scope.loadFeed = function () {
            Feed.parseFeed($scope.feedSrc).then(function (res) {
                $scope.feeds = res.data.responseData.feed.entries;
            });
        };
        $scope.loadvideoFeed = function () {
            Feed.parseFeed($scope.feedvideo).then(function (res) {
                $scope.videos = res.data.responseData.feed.entries;
            });
        };
         $scope.loadFeed();
         $scope.loadvideoFeed();
         $scope.dateToday = Date.parse(curr_month + "/" + curr_date + "/" + curr_year);
         $scope.dateRange = "";
          $scope.eventDateFilter = function(column) {
              if(column === 'today') {
                  $scope.dateRange = $scope.dateToday;
              } else if (column === 'pastWeek') {
                  //need logic
              } else if (column === 'pastMonth') {
                  //need logic            
              } else if (column === 'future') {
                  //need logic
              } else {
                  $scope.dateRange = "";
              }
          };
                      $scope.articleContent = [
          { 
            title: 'Lorem 1', 
            type: 'Image', 
            image: 'http://dummyimage.com/150x150/000/fff&text=Sample+Images', 
            teaser: 'orem ipsum dolor sit amet, consectetur adipisicing elit. Eum soluta minima qui', 
            catergory: 'sport', 
            date: 'Feb 03, 2016' 
          },
          { 
            title: 'Anthony 2', 
            type: 'Video', 
            image: 'http://dummyimage.com/150x150/000/fff&text=Sample+Images',
            teaser: 'orem ipsum dolor sit amet, consectetur adipisicing elit.  soluta minima qui',
            catergory: 'tech',   
            date: 'Feb 03, 2016' 
          },
          { 
            title: 'Munei 3', 
            type: 'article', 
            image: 'http://dummyimage.com/150x150/000/fff&text=Sample+Images', 
            teaser: 'orem ipsum dolor sit amet, consectetur adipisicing elit. Eum soluta minima qui',
            catergory: 'business',  
            date: 'Feb 03, 2016' 
          },
          { 
            title: 'Lorem 4', 
            type: 'Image', 
            image: 'http://dummyimage.com/150x150/000/fff&text=Sample+Images', 
            teaser: 'orem ipsum dolor sit amet, consectetur adipisicing elit. Eum soluta  qui', 
            catergory: 'lifestyle', 
            date: 'Feb 03, 2016' 
          },
          { 
            title: 'Test 5', 
            type: 'video', 
            image: 'http://dummyimage.com/150x150/000/fff&text=Sample+Images', 
            teaser: 'orem ipsum dolor sit amet, consectetur adipisicing elit. Eum soluta minima qui',
            catergory: 'tech',  
            date: 'Feb 03, 2016' 
          },
          { 
            title: 'Breaking 6', 
            type: 'Image', 
            image: 'http://dummyimage.com/150x150/000/fff&text=Sample+Images', 
            teaser: 'orem ipsum dolor sit amet, consectetur adipisicing elit. Eum soluta minima ',
            catergory: 'sport',  
            date: 'Feb 03, 2016' 
          }
        ];   
    }]);

    Search.factory('FeedService', ['$http', function($http){
          return {
        parseFeed: function (url) {
            return $http.jsonp('http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=6&callback=JSON_CALLBACK&q=' + encodeURIComponent(url));
        }
    };
      }]);
我不知道

这是否是您正在寻找的答案,但我很快就对您的数组进行了基本的日期过滤。我根本不使用任何过滤器,而是将数组复制到另一个进行过滤的数组中:

if (articleDate >= startDate && articleDate <= endDate) {
    $scope.displayArticles.push(article);
};

见小提琴:https://jsfiddle.net/oligustafsson/eh67fLx3/

通常,我只将角度过滤器用于简单的任务,例如显示货币等,并且不包含任何基本逻辑。在您的情况下,我的方法可能是拥有一个项目集合和一个过滤器集合,并将它们包装在服务中并在控制器中使用它。

您只需要在ng-repeat中添加一个额外的过滤器:

<div class="col-md-4 video-item" ng-repeat="articles in articleContent | searchFunction | dateRangeFilter">
    <!-- your code -->
</div>

并将该过滤器定义为一个函数,如果要过滤掉文章,则返回false,如果要保留文章,则返回true

$scope.dateRangeFilter = function (article) {
  if (/* want to keep */) {
    return true;
  }
  return false;
}