我正在进行ajax调用并拉下总页数:

$.get(path, function( data ) {
    ractive.set({
    'articles' : data.articles,
    'totalpages' : data.totalpages
    });
});


有什么办法可以从总页数中呈现分页按钮?类似于(假设总页数= 4):

{{#if loop totalpages times:num}}
  <a href="#">{{num}}</a> |
{{/if}}


将输出

<a href="#">1</a> | <a href="#">2</a> | <a href="#">3</a> | <a href="#">4</a>


我看了一下Mustache文档,但是Mustache不太一样。

谢谢,

最佳答案

在组件或Ractive实例中使用计算属性:

computed: {
    total: 'new Array(${totalPages})'
}


然后使用:index(或任何您想要的名称)为每个索引的别名加上别名:

{{#each total:index}}
<a href="#">{{index+1}}</a>
{{/each}}


编辑:上面的total计算属性是Ractive的简写:

computed: {
    total: function(){
        return new Array(this.get('totalPages'));
    }
}

关于javascript - Ractive.js模板循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34146956/

10-12 00:25
查看更多