使用 Meteor 按数组对象中的日期字段排序
Sort by date field in object of array with Meteor
我在我的 Meteor 应用程序中向我的用户集合添加了一个对象数组,名为 contacts
。现在看起来像这样:
{
"_id" : "p6c4cSTb3cHWaJqpG",
"createdAt" : ISODate("2016-05-11T11:30:11.820Z"),
"services" : {
.....
},
"username" : "admin",
"emails" : [
{
"address" : "email@email.com",
"verified" : true
}
],
"points" : 28,
"contacts" : [
{
"when" : ISODate("2016-06-02T12:22:53.747Z"),
"who" : "4YBufbE9PByJBkasy"
},
{
"when" : ISODate("2016-06-02T12:00:41.833Z"),
"who" : "EvF7DbFazmiuG86mD"
},
{
"when" : ISODate("2016-06-02T12:21:41.415Z"),
"who" : "MNFTSzjjzmYWgDvey"
}
]
}
我可以很好地在我的页面上显示联系人,但它们是按照它们在集合中出现的顺序排列的。 我想按 when
字段中的日期对它们进行排序。这可能吗?
我的辅助方法:
Template.contacts.helpers({
'cont': function(){
var user = Meteor.user();
return user;
}
});
和我的模板:
<template name="contacts">
{{#each cont.contacts}}
<h1><a href="/message/{{who}}">{{who}}</a></h1>
{{/each}}
</template>
您可以直接从助手发送联系人数组并像这样对其进行预排序:
Template.contacts.helpers({
'cont': function(){
var user = Meteor.user();
var contacts = user.contacts.sort({'when':'-1'})
return contacts;
}
});
这样一来,你的blaze就会显得简单多了:
<template name="contacts">
{{#each contacts}}
<h1><a href="/message/{{who}}">{{who}}</a></h1>
{{/each}}
</template>
Akram Saouri was on the right track, I just needed to dig a little deeper. So I'll post the 100% working solution I came up with off that. The docs are your friend
Client.js:
Template.contacts.helpers({
'cont': function(){
var contacts = Meteor.user().contacts;
var result = contacts.sort(function (a,b) {
if (a.when > b.when){
return -1;
}
if (a.when < b.when){
return 1;
}
return 0;
});
return result;
}
});
Blaze 模板:
<template name="contacts">
{{#each cont}}
<h1><a href="/message/{{who}}">{{who}}</a></h1>
{{/each}}
</template>
我在我的 Meteor 应用程序中向我的用户集合添加了一个对象数组,名为 contacts
。现在看起来像这样:
{
"_id" : "p6c4cSTb3cHWaJqpG",
"createdAt" : ISODate("2016-05-11T11:30:11.820Z"),
"services" : {
.....
},
"username" : "admin",
"emails" : [
{
"address" : "email@email.com",
"verified" : true
}
],
"points" : 28,
"contacts" : [
{
"when" : ISODate("2016-06-02T12:22:53.747Z"),
"who" : "4YBufbE9PByJBkasy"
},
{
"when" : ISODate("2016-06-02T12:00:41.833Z"),
"who" : "EvF7DbFazmiuG86mD"
},
{
"when" : ISODate("2016-06-02T12:21:41.415Z"),
"who" : "MNFTSzjjzmYWgDvey"
}
]
}
我可以很好地在我的页面上显示联系人,但它们是按照它们在集合中出现的顺序排列的。 我想按 when
字段中的日期对它们进行排序。这可能吗?
我的辅助方法:
Template.contacts.helpers({
'cont': function(){
var user = Meteor.user();
return user;
}
});
和我的模板:
<template name="contacts">
{{#each cont.contacts}}
<h1><a href="/message/{{who}}">{{who}}</a></h1>
{{/each}}
</template>
您可以直接从助手发送联系人数组并像这样对其进行预排序:
Template.contacts.helpers({
'cont': function(){
var user = Meteor.user();
var contacts = user.contacts.sort({'when':'-1'})
return contacts;
}
});
这样一来,你的blaze就会显得简单多了:
<template name="contacts">
{{#each contacts}}
<h1><a href="/message/{{who}}">{{who}}</a></h1>
{{/each}}
</template>
Akram Saouri was on the right track, I just needed to dig a little deeper. So I'll post the 100% working solution I came up with off that. The docs are your friend
Client.js:
Template.contacts.helpers({
'cont': function(){
var contacts = Meteor.user().contacts;
var result = contacts.sort(function (a,b) {
if (a.when > b.when){
return -1;
}
if (a.when < b.when){
return 1;
}
return 0;
});
return result;
}
});
Blaze 模板:
<template name="contacts">
{{#each cont}}
<h1><a href="/message/{{who}}">{{who}}</a></h1>
{{/each}}
</template>