Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor JS cannot sort data

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?

Here's the 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');
}
});

And here's the 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.

like image 803
Xero1 Avatar asked Apr 14 '26 09:04

Xero1


1 Answers

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}}); 
  }
});
like image 90
TomJ Avatar answered Apr 16 '26 21:04

TomJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!