简体   繁体   中英

Meteor: How do you roll data thru a table?

If I'm doing a find() and returning the last five records why aren't all five records updated when {{# each}} is updated? Isn't the find() triggered to return the last 5 records?

Basically, I'm just trying to roll my data through the table as an item is added, so if items 1 thru 5 displayed in the table and you add an item, the table will display item 2 thru 6.

You can use 'limit' and 'sort' ( docs ):

{{#each something}}
    <tr>
      ....
    </tr>
{{/each}}

Then your find, make sure you specify 'sort' since you need to tell meteor how it needs to 'roll' them, ie by date? (this example assumes you have a date field):

Template.yourtemplate.helpers({
    something: function() {
        return YourCollection.find({}, {limit: 5, sort:{date:-1}})
    }
});

The -1 is the sorting order, the higher (or later) dates come up at the top, if you use 1 the earliest come up at the top.

I actually ended up using Justin McCandless' answer from this thread where the sort wasn't working at all.

Meteor.js: Find all documents and return in reverse natural order

return WeighIn.find({}, {sort:{weight: -1}, limit:5}).fetch().reverse();

Adding the reverse() made the table behave the way I needed it to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM