[英]AngularJS Retrieve JSON Records Based On Date
I have only used ng-repeat
to retrieve results in AngularJS so far however I am left with a little bit more of a complex HTML structure this time.到目前为止,我只使用
ng-repeat
来检索 AngularJS 中的结果,但是这次我留下了更多复杂的 HTML 结构。
Here is an example of my HTML:这是我的 HTML 示例:
<section ng-controller="LandingController as vm">
<div class="container-fluid">
<div class="row" id="blog-headliner">
<div class="col-md-6 col-sm-12 blog-headline-item">
///// JSON object with the earliest update date and has a featured value of 1
</div>
<div class="col-md-3 col-sm-4 blog-headline-item" ng-mouseenter="blogHeadlinePClass1 = 'blog-headline-p-reveal'" ng-mouseleave="blogHeadlinePClass1 = ''">
///// JSON object with the 2nd earliest update date and has a featured value of 1
</div>
<div class="col-md-3 col-sm-4 blog-headline-item" ng-mouseenter="blogHeadlinePClass2 = 'blog-headline-p-reveal'" ng-mouseleave="blogHeadlinePClass2 = ''">
///// JSON object with the 3rd earliest update date and has a featured value of 1
</div>
<div class="col-md-3 col-sm-4 blog-headline-item" ng-mouseenter="blogHeadlinePClass3 = 'blog-headline-p-reveal'" ng-mouseleave="blogHeadlinePClass3 = ''">
///// JSON object with the 4th earliest update date and has a featured value of 1
</div>
</div>
</div>
</section>
As you can see the blog-headline-item
elements are different so using ng-repeat will be no good for me in this case.正如你所看到的
blog-headline-item
元素是不同的,所以在这种情况下使用 ng-repeat 对我没有好处。
I then have this JSON data:然后我有这个 JSON 数据:
[
{
"id": 3,
"author_id": 1,
"title": "Hello I am featured",
"subtitle": "Yeahaaaa!",
"content": "<p>Content<\/p>\r\n",
"featured_image": "1513fefbb4c4ced364ca7b976e1b3b68bed9c7c3.jpg",
"slug": "hello-i-am-featured-2",
"published": 1,
"featured": 1,
"created_at": "2016-03-31 12:35:02",
"updated_at": "2016-03-31 12:44:44",
"published_at": "2016-03-31 12:35:06"
},
{
"id": 4,
"author_id": 1,
"title": "Article",
"subtitle": "This is an article",
"content": "<p>This is an article about an article.<\/p>\r\n",
"featured_image": "cf6b8210c93ad19a3b71922f36a96229abdc148a.jpg",
"slug": "article",
"published": 1,
"featured": 0,
"created_at": "2016-03-31 13:52:24",
"updated_at": "2016-03-31 13:52:29",
"published_at": "2016-03-31 13:52:29"
}
]
NOTE: there are more records than this at the api url I just wanted to show an example.注意:在 api url 上有比这更多的记录,我只是想展示一个例子。
So down to what I'd like to achieve... I want to pull through 4 records to the front end that have an object with a featured
value of 1
and display them based on the updated_at
time, the 4 closest to today's date should show.所以归结为我想要实现的目标......我想将 4 条记录拉到前端,这些记录具有
featured
值为1
的对象,并根据updated_at
时间显示它们,最接近今天日期的 4 条记录应该展示。 If you look at the HTML structure you will see that the elements with the class of blog-headline-item
have a comment with the requirements for each one in them.如果您查看 HTML 结构,您将看到具有
blog-headline-item
类的元素有一个注释,其中包含每个元素的要求。
As I am unable to use ng-repeat I am unsure how else I can do this, any ideas would be much appreciated.由于我无法使用 ng-repeat,我不确定我还能怎么做,任何想法都将不胜感激。
I really like to use lodash for manipulating my data collections.我真的很喜欢使用lodash来操作我的数据集合。 So, using it, I would do:
所以,使用它,我会这样做:
working example: https://jsfiddle.net/relferreira/d0c0xmmx/2/工作示例: https : //jsfiddle.net/relferreira/d0c0xmmx/2/
But you can really do using the $filter service, or a simple foreach.但是你真的可以使用 $filter 服务,或者一个简单的 foreach。
HTML: HTML:
<div data-ng-app="app">
<section ng-controller="MainController as mainVm">
<div class="container-fluid">
<div class="row" id="blog-headliner">
<div class="col-md-6 col-sm-12 blog-headline-item">
///// JSON object with the earliest update date and has a featured value of 1
{{mainVm.filterData[0]}}
</div>
<div class="col-md-3 col-sm-4 blog-headline-item" ng-mouseenter="blogHeadlinePClass1 = 'blog-headline-p-reveal'" ng-mouseleave="blogHeadlinePClass1 = ''">
///// JSON object with the 2nd earliest update date and has a featured value of 1
{{mainVm.filterData[1]}}
</div>
<div class="col-md-3 col-sm-4 blog-headline-item" ng-mouseenter="blogHeadlinePClass2 = 'blog-headline-p-reveal'" ng-mouseleave="blogHeadlinePClass2 = ''">
///// JSON object with the 3rd earliest update date and has a featured value of 1
{{mainVm.filterData[2]}}
</div>
<div class="col-md-3 col-sm-4 blog-headline-item" ng-mouseenter="blogHeadlinePClass3 = 'blog-headline-p-reveal'" ng-mouseleave="blogHeadlinePClass3 = ''">
///// JSON object with the 4th earliest update date and has a featured value of 1
{{mainVm.filterData[3]}}
</div>
</div>
</div>
</section>
</div>
JS: JS:
angular.module('app', []);
angular.module('app')
.controller('MainController', mainController);
mainController.$inject = ['$scope'];
function mainController($scope){
var vm = this;
vm.data = [ ... ];
vm.filterData = _.chain(vm.data)
.orderBy('updated_at', ['desc'])
.filter(function(e) { return e.featured == 1})
.value();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.