简体   繁体   English

带有日期范围过滤器的AngularJS模型过滤器

[英]AngularJS model filter with date range filter

I have in the view: 我认为:

<tr dir-paginate="post in posts |orderBy:propertyName:reverse | filter: searchPost | itemsPerPage: pageSize">
<td>
        {{post.title}}
</td>
<td>
        {{post.content}}
</td>
<td>
        {{post.dateOfCreation | date:"medium"}}
</td>
</tr>

I also have filtering, which works on all the fields: 我也有过滤功能,它适用于所有领域:

     <div>
        <input type="text" placeholder="Find by title..." ng-model="searchPost.title" />
    <div>
    <div>
        <input type="text" placeholder="Find by content..." ng-model="searchPost.content" />
    </div>

    <div>
        <input type="text" placeholder="Find by date..." ng-model="searchPost.dateOfCreation" />
    </div>

However, the date is filtered only when I type the date in format: yyyy-mm-dd in the textbox, so basically it works for only 1 date. 但是,仅当我在文本框中输入格式为yyyy-mm-dd的日期时,日期才会被过滤,因此基本上它仅适用于1个日期。 My goal is to make possible for user to choose date range and show these values, however, I cannot make it work together with my searchPost filter... I am using angular-datepicker: 我的目标是让用户可以选择日期范围并显示这些值,但是,我无法使其与我的searchPost过滤器一起使用...我正在使用angular-datepicker:

<input date-range-picker class="form-control date-picker" type="text" ng-model="someDate" options = "{locale: {format: 'YYYY-MM-DD'}}"/>

   $scope.someDate = { startDate: null, endDate: null };

Datepicker which I am using is: Angular Daterangepicker 我正在使用的Datepicker是: 角Daterangepicker

You will need to implement a custom filter to do so. 您将需要实现一个自定义过滤器。

The custom filter will take care of checking if the post creation date is between the start date and end date of the range selected. 自定义过滤器将负责检查创建后的日期是否在所选范围的开始日期和结束日期之间。

In the end it will look like something like this: 最后,它看起来像这样:

 // Setup the filter var app = angular.module('testFilters', []); app.controller('postsCtrl', ['$scope',function($scope){ $scope.posts = [ {title: 'test 2010', dateOfCreation: new Date(2010, 1, 1)}, {title: 'test 2012', dateOfCreation: new Date(2012, 1, 1)}, {title: 'test 2014', dateOfCreation: new Date(2014, 1, 1)}, {title: 'test 2016', dateOfCreation: new Date(2016, 1, 1)} ]; $scope.searchPost = {title: 'test', date: {startDate: new Date(2011, 1, 1), endDate: new Date(2015, 1, 1) } }; }]); app.filter('postFilter', function() { // Create the return function and set the required parameter name to **input** return function(input, post) { var out = []; angular.forEach(input, function(p) { var filtered = true; // checking if we should check the title and cehcking // you may want to pass everything to lowercase for best search results if(post.title && p.title.indexOf(post.title) < 0){ filtered = false; } // same for the content if(post.content && p.content.indexOf(post.content) < 0){ filtered = false; } // checking the date of creation against the date range if(post.date && post.date.startDate && post.date.endDate){ if(p.dateOfCreation < post.date.startDate || p.dateOfCreation > post.date.endDate){ filtered = false } } // adding the post to the resulting array if(filtered){ out.push(p); } }); return out; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="testFilters" ng-controller="postsCtrl"> <h3>unfiltered posts:</h3> <div ng-repeat="post in posts"> ----------------------------------------------------<br> Title: {{post.title}}<br> Content: {{post.content}}<br> Created: {{post.dateOfCreation | date:'MM/dd/yyyy'}}<br> </div> <hr> <br> <h3>filtering post:</h3> <div> Title: {{searchPost.title}}<br> Content: {{searchPost.content}}<br> Range: {{searchPost.date.startDate | date:'MM/dd/yyyy'}} - {{searchPost.date.endDate| date:'MM/dd/yyyy'}}<br> </div> <hr> <br> <h3>filtered posts:</h3> <div ng-repeat="post in posts | postFilter:searchPost"> ----------------------------------------------------<br> Title: {{post.title}}<br> Content: {{post.content}}<br> Created: {{post.dateOfCreation | date:'MM/dd/yyyy'}}<br> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM