2017-09-25 102 views
1

我恳请需要支持。我已经按照YouTube教程编写了分页,除了再次回退之外,该教程还可以正常工作。它只有2个按钮,previousnext,当点击下一个按钮时,它可以正常工作,但前一个按钮只向后退一次。假设我在分页集合中有20条记录一次显示5个,下一个按钮可以到达第四个页面的结尾,但上一个按钮不会向后传递一个。请问我该怎么做分页体验?只要用户点击,上一个按钮就会导航到最后一页。流星js定制分页

活动的分页按钮

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

大火分页按钮

<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'); 
    } 
    } 
} 
+0

你能证明模板和帮助呢? – Styx

+0

THanks,Styx,我已经更新了代码,添加了帮手。 – kehinde

+0

你的收藏中有多少物品? – Styx

回答

1

你有6个文件总计每页2个文档,共有3页。

if条件previous按钮单击处理程序阻止你去到第一页:

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

对于skip第2页将等于2,并在下次点击这个条件会false,来自回去预防。

当你在第三页上时 - 你可以仅在第二页上创建一个按钮只能工作一次的印象。

它应该是这样的:

if (Session.get('skip') > 0) { 
    ... 
} 
+0

THANKs Styx。我非常感谢你的专业贡献。为了您的回答,我只添加了这个'Session.get('skip')> 0 || Session.get('skip')> 2'。它现在工作完美。 – kehinde

+1

@kehinde这个增加没有意义。如果“skip> 0”,那么条件的第二部分甚至不会评估。 – Styx

+0

是的。真正做到。我已经回复了你的建议。 – kehinde