使用 json 和下划线时通过模板限制输出
limiting output through templates when using json and underscore
我有一个 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 为单位显示剩余数据当点击某个按钮时?
听起来您正在寻找 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
我有一个 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 为单位显示剩余数据当点击某个按钮时?
听起来您正在寻找 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