Meteor filter for everyone

I have a problem with the filter and everyone. I want to create a filter that changes the data according to this filter.

<select id="filter">
<option value="all">all</option>
<option value="one">one</option>
<option value="two">two</option>
</select>

{{#each datas}}
<span class="badge">{{Name}}</span>
{{/each}}

Template.mytemp.created = function(){
Session.set("activefilter", "all");
};

Template.mytemp.datas = function(){
  var ac = Session.get("activefilter");
  var result = new Array();
  if(ac != undefined){
    var data = RawData.find({filter:ac}).fetch();

      for(var ii = 0; ii < data.length;ii++){
        var nData = NextData.findOne({_id : data[ii].Next_ID});
        result[ii] = {
          Name : nData.Name
        };
      }
  return result;
  }
};

i create an event handler as follows:

Template.mytemp.events({
  'change #filter':function(){
    Session.set("activefilter",$('#filter').val());
  }
});

Each time I change the filter, nothing happens, the data for each does not change. please help me how to update data when filter changes?

0
source share
1 answer

When you manually iterate over the cursor (using each, etc.), you will have to rewind the cursor to reset. Adapted from http://docs.meteor.com/#rewind

forEach, map fetch . , rewind to reset.

- :

Template.mytemp.datas = function(){
  var ac = Session.get("activefilter");
  var result = new Array();
  if(ac != undefined){
    var cursor = RawData.find({filter:ac});
    var data = cursor.fetch();

      for(var ii = 0; ii < data.length;ii++){
        var nData = NextData.findOne({_id : data[ii].Next_ID});
        result[ii] = {
          Name : nData.Name
        };
      }

    cursor.rewind(); //we rewind our cursor here so that it can be iterated again from the beginning when needed

  return result;
  }
};
+1

All Articles