简体   繁体   English

使用Angular按当前日期过滤JSON数据

[英]filter JSON data by current date using Angular

I am trying to filter JSON data by current date using Angular. 我正在尝试使用Angular按当前日期过滤JSON数据。 I have seen some similar posts but I can't seem to implement the answers correctly. 我看到过类似的帖子,但似乎无法正确实现答案。 I attempted to write a custom filter comparing today which is the name of the var holding my date object with doc.date which is the JSON location of the date entry I want to filter. 我尝试编写一个自定义过滤器, today进行比较today这是保存我的日期对象的var的名称与doc.date ,而doc.date是我要过滤的日期条目的JSON位置。

My JSON data looks like this: 我的JSON数据如下所示:

var books = 

[  
   {  
      "doc":{  
         "date":"07/14",
         "title":"A Nourishing Ingredient",
         "quote":"Where humility had formerly stood for a forced feeding on humble pie, it now begins to mean the nourishing ingredient which can give us serenity.",
         "attribution":"page 74",
         "text":"How often do I focus on my problems and frustrations? When I am having a 'good day' these same problems shrink in importance and my preoccupation with them dwindles. Wouldn't it be better if I could find a key to unlock the 'magic' of my 'good days' for use on the woes of my 'bad days?'. I already have the solution! Instead of trying to run away from my pain and wish my problems away, I can pray for humility! Humility will heal the pain. Humility will take me out of myself. Humility, that strength granted to me by that 'power greater than myself,' is mine for the asking! Humility will bring balance back into my life. Humility will allow me to accept my humanness joyously."
  }
 },
{  
  "doc":{  
     "date":"07/15",
     "title":"Pride",
     "quote":"Time and again I approached the Seventh Step, only to fall back and regroup. Something was missing and the impact of the Step escaped me. What had I overlooked? A single word: read but ignored, the foundation of all the Steps, indeed the entire Alcoholics Anonymous program – that word is 'humbly.' I understood my shortcomings: I constantly put tasks off; I angered easily; I felt too much self-pity; and I thought, why me? Then I remembered, 'Pride goeth before the fall,' and I eliminated pride from my life."
  }
}
]   

My JS: 我的JS:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0, so always add + 1

var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
today = mm+'/'+dd;
console.log(today);

var myApplication = angular.module('myApp', ['ngColorThis']);

myApplication.controller("Catalog", function ($scope) {

$scope.books = books;


})

.filter('mydate', function() {

            return function (doc.date, today) {
            return doc.date < today;
        };

    });

My html: 我的html:

<div ng-app="myApp" ng-controller="Catalog">


<div  ng-repeat="book in books | mydate" color-this="background-color" data-color="book.doc.title">
    <div  >
    <div class="date">{{book.doc.date}}</div>
    <div class="title">{{book.doc.title}}</div>
    <div class="quote">{{book.doc.quote}}</div>
    <div class="attribution">{{book.doc.attribution}}</div>
    <div class="text">{{book.doc.text}}</div>
    </div>
</div>

Please find the below code. 请找到下面的代码。 Hope this will help you. 希望这会帮助你。

 var myApplication = angular.module('myApp', []); myApplication.controller("Catalog", function($scope) { var books = [{ "doc": { "date": "07/15", "title": "A Nourishing Ingredient", "quote": "Where humility had formerly stood for a forced feeding on humble pie, it now begins to mean the nourishing ingredient which can give us serenity.", "attribution": "page 74", "text": "How often do I focus on my problems and frustrations? When I am having a 'good day' these same problems shrink in importance and my preoccupation with them dwindles. Wouldn't it be better if I could find a key to unlock the 'magic' of my 'good days' for use on the woes of my 'bad days?'. I already have the solution! Instead of trying to run away from my pain and wish my problems away, I can pray for humility! Humility will heal the pain. Humility will take me out of myself. Humility, that strength granted to me by that 'power greater than myself,' is mine for the asking! Humility will bring balance back into my life. Humility will allow me to accept my humanness joyously." } }, { "doc": { "date": "07/16", "title": "Pride", "quote": "Time and again I approached the Seventh Step, only to fall back and regroup. Something was missing and the impact of the Step escaped me. What had I overlooked? A single word: read but ignored, the foundation of all the Steps, indeed the entire Alcoholics Anonymous program – that word is 'humbly.' I understood my shortcomings: I constantly put tasks off; I angered easily; I felt too much self-pity; and I thought, why me? Then I remembered, 'Pride goeth before the fall,' and I eliminated pride from my life." } }] $scope.books = books; }); myApplication.filter('mydate', function() { return function(input) { var today = new Date(); var dd = today.getDate(); var mm = today.getMonth() + 1; //January is 0, so always add + 1 var yyyy = today.getFullYear(); if (dd < 10) { dd = '0' + dd } if (mm < 10) { mm = '0' + mm } today = mm + '/' + dd; return (input == today) } }); 
 <!DOCTYPE html> <html> <head> <script data-require="angular.js@1.4.0" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular.js"></script> <link rel="stylesheet" href="style.css" /> <script src="script.js"></script> </head> <body> <div ng-app="myApp" ng-controller="Catalog"> <div ng-repeat="book in books" color-this="background-color" data-color="book.doc.title"> <div ng-show=" book.doc.date | mydate"> <div class="date">{{book.doc.date}}</div> <div class="title">{{book.doc.title}}</div> <div class="quote">{{book.doc.quote}}</div> <div class="attribution">{{book.doc.attribution}}</div> <div class="text">{{book.doc.text}}</div> </div> </div> </div> </body> </html> 

Recommended: These kind of business logic should be place in service for better performance in real world case. 建议:应将这些业务逻辑放在服务中,以便在实际情况下获得更好的性能。

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

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