2

我需要一个支持。我在 YouTube 教程之后写了一个分页,除了再次倒退时,它工作得很好。它只有 2 个按钮,previous并且next,当单击下一个按钮时,它可以正常工作,但上一个按钮只能后退一次。假设我在集合中有 20 条记录,一次分页显示 5 条,下一个按钮可以转到末尾到第四页,但上一个按钮不会向后退一步。请问我该怎么做才能有分页经验?只要用户单击,上一个按钮就会导航到最后一页。

分页按钮的事件

Template.myviews.events({
  'click .previous': function () {
    if (Session.get('skip') > 5 ) {
      Session.set('skip', Session.get('skip') - 5 );
    }
  },
  'click .next': function () {
    Session.set('skip', Session.get('skip') + 5 );
  }
});

出版

Meteor.publish('userSchools', function (skipCount) {
  check(skipCount, Number);
  user = Meteor.users.findOne({ _id: this.userId });
  if(user) {
    if(user.emails[0].verified) {
      return SchoolDb.find({userId: Meteor.userId()}, {limit: 5, skip: skipCount});
    } else {
      throw new Meteor.Error('Not authorized');
      return false;
    }
  }
});

订阅

Session.setDefault('skip', 0);
Tracker.autorun(function () {
  Meteor.subscribe('userSchools', Session.get('skip'));
});

Blaze 分页按钮

<ul class="pager">
  <li class="previous"><a href="#">Previous</a> </li>
  <li class="next"><a href="#">Next</a> </li>
</ul>

模板助手

RenderSchool: function () {
  if(Meteor.userId()) {
    if(Meteor.user().emails[0].verified) {
      return SchoolDb.find({userId: Meteor.userId()}).fetch().reverse();
    } else {
      FlowRouter.go('/');
      Bert.alert('Please verify your account to proceed', 'success', 'growl-top-right');
    }
  }
}
4

1 回答 1

1

您总共有 6 个文档,每页 2 个文档,总共 3 页。

ifprevious按钮单击处理程序中的条件阻止您转到第一页:

if (Session.get('skip') > 2 /* testing */ ) {
  ...
}

对于第二页skip将相等2,并且在下一次单击时,此条件将是false,防止返回。

当您在第 3 页时 - 您只能进入第 2 页,给人一种按钮只能使用一次的印象。

它应该是这样的:

if (Session.get('skip') > 0 ) {
  ...
}
于 2017-09-25T18:58:48.523 回答