【问题标题】:limiting output through templates when using json and underscore使用 json 和下划线时通过模板限制输出
【发布时间】:2016-06-30 08:41:04
【问题描述】:

我有一个 json 文件,我用它来在我的模板中填充数据。

[
     { id:1 ,imgsrc:"images/wedding.jpg" ,name:"wedding"},
     { id:1 ,imgsrc:"images/wedding.jpg" ,name:"wedding"},
     { id:1 ,imgsrc:"images/wedding.jpg" ,name:"wedding"},
     { id:1 ,imgsrc:"images/wedding.jpg" ,name:"wedding"},
     { id:1 ,imgsrc:"images/wedding.jpg" ,name:"wedding"},
     { id:1 ,imgsrc:"images/wedding.jpg" ,name:"wedding"},
     { id:1 ,imgsrc:"images/wedding.jpg" ,name:"wedding"}


]

我目前正在使用 _.each 函数来填充我的模板,但这会导致我的所有数据都加载到我的屏幕上。有没有办法限制输出,然后在某个按钮打开时以 4 的计数显示剩余数据点击了吗?

【问题讨论】:

    标签: javascript json templates underscore.js


    【解决方案1】:

    听起来您正在寻找 slice 方法 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

    var abc = ["a", "b", "c", "d", "e", "f", "g"];
    var itemsPerPage = 2;
    var page3ItemsBegin = itemsPerPage * 2;
    
    console.log(abc.slice(page3ItemsBegin, page3ItemsBegin + itemsPerPage));
    

    工作示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      相关资源
      最近更新 更多