简体   繁体   English

AngularJS 过滤数据,日期为今天、周和月

[英]AngularJS Filter Data with Date as Today , Week and Month

$scope.Data = [
{
    "title": "Algolia's Global Roadshow",
    "author": "Ryan Chen",
    "excerpt": "We've heard it from experts, industry surveys, and our most successful customers: search and discovery are key to moving the digital conversation forward.",
    "date": "2018-10-17",
    "date_timestamp": 1539734400
  },
  {
    "title": "Black Friday & Site Search: small tips, big difference",
    "author": "Matthieu Blandineau",
    "excerpt": "It’s no surprise that during the holiday season, Black Friday & Cyber Monday are absolutely critical events for any e-commerce business. According to Adobe, online retailers earned $108.15B between Nov 1 and Dec 31 2017, up 13.8% from 2016. Only in the U.S.",
    "date": "2018-10-05",
    "date_timestamp": 1538697600
  },
  {
    "title": "For better school projects, a partnership with GitHub",
    "author": "Jessica West",
    "excerpt": "Hello GitHubbers and Algolians alike! We have some exciting news we’d like to share with you. Algolia is so pleased to announce that we have partnered with GitHub’s Student Developer Pack to help students build search functionality into their projects freely and effortlessly 🎉.",
    "date": "2018-09-18",
    "date_timestamp": 1537228800
  }
]

I have 3 Anchor Button ( Today , Week , Month ) I want to Filter this data using ng-cick = GetDate(Today or Week or Month) and Bind the data我有 3 个锚按钮(今天、周、月)我想使用 ng-cick = GetDate(Today or Week or Month) 过滤此数据并绑定数据

How to filter the $scope.Data based on what i click ( Today , Week and Month )如何根据我点击的内容过滤 $scope.Data(今天、周和月)

Please help请帮忙

For filtering the data you can do something along the lines of:为了过滤数据,您可以执行以下操作:

// Set the start and end dates for the search. Limit to one day, week, etc.
const startDate = new Date(2018, 9, 4);
const endDate = new Date(2018, 9, 6);

// Filter by comparing the dates
const filteredData = $scope.Data.filter(e => {
    const date = new Date(e.date);
    return date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime();
});

console.log(filteredData);

And bind as applicable to the button clicks.并绑定适用于按钮点击。

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

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