简体   繁体   English

AngularJS按日期过滤

[英]AngularJS filter by date

    <div class="well calendar" ng-model="dt">
         <datepicker min="minDate" show-weeks="showWeeks"></datepicker>
    </div>
    <div class="well">
        <ul class="unstyled">
            <li ng-repeat="todo in todos | filter: dt">
                <input type="checkbox" ng-model="todo.done">
                <span class="done-{{todo.done}}">{{todo.text}}</span>
                <span class="done-{{todo.done}}">{{todo.date | date:'fullDate'}}</span>
            </li>
        </ul>
    </div>

I have an angular-ui datepicker, and I want to filter my todos list by date. 我有一个angular-ui日期选择器,我想按日期过滤待办事项列表。 But filter doesn't work. 但是过滤器不起作用。

I think there is a mistake, you need to move ng-model from div to datepicker to ensure the model is updated. 我认为有一个错误,您需要将ng-model从div移至datepicker以确保模型已更新。

In your case, you could do the filtering inside controller using $watch. 在您的情况下,您可以使用$ watch在控制器内部进行过滤。 Like this: 像这样:

function testController($scope,$filter){
    $scope.todos = [
        {done:true,text:"test 1",date:new Date(2013,11,24)},
        {done:false,text:"todo 2",date:new Date(2013,11,25)},
        {done:false,text:"todo 3",date:new Date(2013,11,26)}
   ];

   $scope.filterTodos = [];

    $scope.$watch("dt",function(newValue,oldValue,scope){
        if (newValue){
            scope.filterTodos = $filter('filter')(scope.todos, function(todo){
                console.log(todo.date.getDate());
                console.log(newValue);
                return todo.date.getDate() == newValue;
            });
        }
        else{
           scope.filterTodos = scope.todos; //no filter
        }
    });
}

DEMO 演示

In this demo, I allow users to enter free text and filter by date.getDate() , you could modify the logic to filter in any way you want. 在此演示中,我允许用户输入自由文本并按date.getDate()进行过滤,您可以修改逻辑以任何所需的方式进行过滤。

I think you don't need the watch as since the view is re-painted every-time the filter changes. 我认为您不需要手表,因为每次过滤器更改时都会重新绘制视图。 So just adding the appropriate filter should work. 因此,只需添加适当的过滤器即可。

And yes you should move ng-model="dt" to the input element, datepicker in your case so that every time you enter any date or modify it, it update the model. 是的,您应该将ng-model =“ dt”移至输入元素,以您的情况为datepicker,以便每次输入或修改日期时,都会更新模型。 And you are using the model as filter so when model changes resulting to change in filter, the view is repainted filtering the results. 而且您将模型用作过滤器,因此当模型更改导致过滤器更改时,将重新绘制视图以过滤结果。

I've experienced this most times; 我经历了很多次。 read a lot about creating custom app filter. 阅读了很多有关创建自定义应用过滤器的信息。 But I think the quickest way, especially if you have access to the date source, and if it is coming from MySQL, just return the date from the database as an ISO Date and angularjs can then filter the date correctly. 但是我认为最快的方法,特别是如果您可以访问日期源,并且它来自MySQL,只需从数据库中以ISO日期返回日期,然后angularjs就可以正确过滤日期。

date("c", strtotime($mysqlDateValueFromDb))

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

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