[英]Limit number of Dynamic list Items in a Function
I would like to achieve 2 things with this Code I have been working on so not sure if to separate the Questions: 我想用我已经编写的本代码实现2件事,所以不确定是否要分开问题:
JS: JS:
function listPosts(data) {
postlimit =
var output='<ul data-role="listview" data-filter="true">';
$.each(data.posts,function(key,val) {
output += '<li>';
output += '<a href="#devotionpost" onclick="showPost(' + val.id + ')">';
output += '<h3>' + val.title + '</h3>';
output += '<p>' + excerpt + '</p>';
output += '</a>';
output += '</li>';
}); // go through each post
output+='</ul>';
$('#postlist').html(output);
} // lists all the posts
Questions: 问题:
1: I would like to limit the number of Dynamic List Posts returned to 8 1:我想将动态列表帖子的数量限制为8
2: While I limit the displayed items, I want to add a 'More...' text at the bottom so another set of 8 items is appended to already displayed list. 2:当我限制显示的项目时,我想在底部添加一个“更多...”文本,以便将另外一组8个项目附加到已显示的列表中。
I am already trying out some codes but was hoping to get some guidance 我已经尝试了一些代码,但希望得到一些指导
I am answering you on basis of pure logic and implementation of logic. 我是在纯逻辑和逻辑实现的基础上回答你的。 there could be API stuff for it , but I don't really know.
它可能有API的东西,但我真的不知道。 Secondly;
其次; It would be a good solution to find some jQuery plugin if you don't have any problems with using jQuery.
如果您在使用jQuery时没有任何问题,那么找到一些jQuery插件将是一个很好的解决方案。
call the function onMoreClick()
upon clicking the More...
html item 单击
More...
html项目时调用函数onMoreClick()
var end = 8;
var start = 1;
function onMoreClick()
{
start = end
end = end+8;
listPosts(data)
}
function listPosts(data) {
postlimit =
var output='<ul data-role="listview" data-filter="true">';
var i = start;
$.each(data.posts,function(key,val) {
if(i<end && i >=start){
output += '<li>';
output += '<a href="#devotionpost" onclick="showPost(' + val.id + ')">';
output += '<h3>' + val.title + '</h3>';
output += '<p>' + excerpt + '</p>';
output += '</a>';
output += '</li>';
i++;
}
}); // go through each post
output+='</ul>';
$('#postlist').html(output);
} // lists all the posts
function listPosts(data, postlimit) {
var $output = $('<ul class="posts" data-role="listview" data-filter="true">');
$.each(data.posts,function(key, val) {
$("<li>", {id: "post_" + val.id})
.append([
$("<h3>", {text: val.title}),
$("<p>", {text: val.excerpt})
])
.appendTo($output);
return (postlimit-- > 1);
});
$('#postlist').empty().append($output);
}
// exemplary delegated event handler
$(document).on("click", "ul.posts h3", function () {
$(this).show();
});
later ... 以后......
listPosts(data, 8);
Notes: 笔记:
$.each()
you can return true
or false
. $.each()
你可以返回true
或false
。 If you return false
, the loop stops. false
,则循环停止。 .html()
, especially if you already have DOM elements to work with. .html()
,特别是如果您已经有DOM元素可以使用。 onclick
. onclick
这样的内联事件处理程序。 At all.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.