Meteor JS 无法对数据进行排序 [英] Meteor JS cannot sort data

查看:35
本文介绍了Meteor JS 无法对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正文中有一个按钮,单击该按钮时应按降序排序.当我这样做时,什么也没有发生.我相信我的代码是正确的,但也许我遗漏了什么?

I have a button in the body that when clicked, should sort descending. When I do, nothing happens. I believe my code is correct, but perhaps I'm missing something?

这是js:

Tasks = new Mongo.Collection("tasks");

Template.body.events({
"click .sort_title": function () {
return Tasks.find({}, {sort: {movie_title: -1}});
console.log('Sorting has been clicked');
}
});

这是 HTML:

<table class="item_db">
<tr>
<th>Title <i class="fa fa-sort sort_title"></i></th>
</tr>
</table>

单击按钮甚至不会传递到控制台日志命令,因此它会中断任务.

clicking the button doesn't even pass to the console log command, so it breaks at the task.

推荐答案

问题是负责返回任务以显示的帮助程序没有收到排序切换.通过在点击事件中设置 Session 值,它将强制使用相同的 Session 键的任务助手再次运行.请参阅下面的示例,以及 Meteor 关于反应性的文档.

The issue is the helper which is responsible for returning the tasks to display isn't receiving the sort toggle. By setting a Session value in the click event it will force the task helper, which is using the same Session key, to run again. See below for an example, as well as Meteor's documentation on reactivity.

Tasks = new Mongo.Collection("tasks");

Template.body.events({
  "click .sort_title": function () {
    var sortValue = Session.get('sort') || 1;
    Session.set('sort', sortValue * -1);
  }
});

Template.body.helpers({ 
  tasks: function () {
    var sortValue = Session.get('sort') || 1;
    return Tasks.find({}, {sort: {movie_title: sortValue}}); 
  }
});

这篇关于Meteor JS 无法对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆