0

Inside Meteor, I wish to change the order of a collection that is rendered inside a loop dynamically:

Template.chartPage.helpers({

    employee:function(){

        sortArr = [];
    var timePeriod = Session.get("period");
    var currentPage = Session.get("current_page");
        sortArr[currentPage+'.'+timePeriod] = "asc";
        return EmployeeCollection.find({}, sortArr).fetch();
    },
});

HTML:

{{#each employee}}
    {{first_name}}
{{/each}}

I want it so that when the session variable for period and current_page change, so does the sort order in the helper - the session variables do get updated, but the template isn't rerendered into a sort.

4

1 回答 1

1

在流星中,数据库(目前)基于 mongodb。升序是1,降序是-1。您的最终数组必须类似于:

return EmployeeCollection.find({}, {sort : {number: -1, size: 1} })

应该为您提供以下带有numbersize字段的订单(每行是一个文档)。

{ number : 5, size: 1, _id: ...}
{ number : 4, size: 2, _id: ...}
{ number : 3, size: 3, _id: ...}

有关如何使用 mongodb 订购的更多详细信息,请访问: http: //docs.mongodb.org/manual/reference/method/cursor.sort/

fetch()另一方面,当将光标返回到模板助手时,您不必使用。Meteor 将自动解析它,而无需将它作为一个数组进行解析(使用 fetch)

Session只要遵循此结构,您就可以使用原样构建排序查询。一旦您更改Session哈希,订单将自动更改并重新呈现。

于 2013-04-29T14:32:11.473 回答