如何替换流星集合查询中用于排序的关键字字段? [英] How to replace the Key for sort field in a meteor collection query?
本文介绍了如何替换流星集合查询中用于排序的关键字字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中,我想将名称键替换为通过参数this.params.sortby
传递的值,但是我无法使其正常工作.寻找帮助.
In the code below, I want to replace the name key with a value being passed in via a parameter this.params.sortby
but I can't get it working. Looking for some help.
因此,如果:
this.params.sortby=location
我想要这个:
Template.MyTemplate.helpers({
data: function () {
return MyCollection.find({},{sort:{name: 1 }});
}
});
要成为这个人
Template.MyTemplate.helpers({
data: function () {
return MyCollection.find({},{sort:{location: 1 }});
}
});
推荐答案
您可以使用带有方括号符号的对象来实现动态键命名,如下所示:
You can use an object with the bracket notation to achieve dynamic key naming, like this :
var sorter={};
sorter[this.params.sortby]=1;
假设this.params.sortby
等于String
年龄",您将具有以下sorter
对象:
Assuming that this.params.sortby
is equal to the String
"age", you'd have the following sorter
object :
var sorter={
age:1
};
然后,您可以使用它来对收藏进行适当的排序:
Then you can use to sort your collection appropriately :
MyCollection.find({},{sort:sorter});
这篇关于如何替换流星集合查询中用于排序的关键字字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文