[英]How to change ajax pagination to load more or infinite scroll functionality
[英]infinite ajax scroll pagination looping
我正在使用無限的ajax滾動。 它正在工作,因為我可以點擊加載更多項目,這將加載內容。 然而,我應該停在最后一頁,並顯示“沒有更多的帖子”,而是仍然顯示“加載更多項目”鏈接,點擊時再次從第2頁開始分頁。
安慰:
Welcome at page 2, the original url of this page would be: /website/home/2
Welcome at page 3, the original url of this page would be: /website/home/3
我應該顯示“沒有更多的帖子” - 而是繼續如下:
Welcome at page 4, the original url of this page would be: /website/home/2
Welcome at page 5, the original url of this page would be: /website/home/3
IAS:
<script type="text/javascript">
var ias = $.ias({
container: "#posts",
item: ".post",
pagination: "#pagination",
next: ".next a"
});
ias.extension(new IASSpinnerExtension());
ias.extension(new IASTriggerExtension({offset: 1}));
ias.extension(new IASNoneLeftExtension({text: 'There are no more posts.'}));
jQuery.ias().extension(new IASPagingExtension());
jQuery.ias().on('pageChange', function(pageNum, scrollOffset, url) {
console.log(
"Welcome at page " + pageNum + ", " +
"the original url of this page would be: " + url
);
});
</script>
我試圖實現以下,它不起作用:
if(url == '/website/home/2' && pageNum > 2) {
jQuery.ias().destroy();
}
我正在使用來自http://www.phpeasystep.com/phptu/29.html的分頁
分頁:
$targetpage = "home.php";
$limit = 10;
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit;
else
$start = 0;
$query2 = $pdo->prepare("select count(*) from table where...");
$query2->execute(array(':id' => $id));
$total_pages = $query2->fetchColumn();
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class=\"pagination\">";
if ($page > 1)
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$prev\">«</a></li>";
else
$pagination.= "<li class=\"disabled\"><a href=\"#\">«</a></li>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li class=\"active\"><a href=\"#\">$counter<span class=\"sr-only\">(current)</span></a></li>";
else
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li class=\"active\"><a href=\"#\">$counter<span class=\"sr-only\">(current)</span></a></li>";
else
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
$pagination.= "";
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$lpm1\">$lpm1</a></li>";
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$lastpage\">$lastpage</a></li>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=1\">1</a></li>";
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=2\">2</a></li>";
$pagination.= "";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li class=\"active\"><a href=\"#\">$counter<span class=\"sr-only\">(current)</span></a></li>";
else
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
$pagination.= "";
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$lpm1\">$lpm1</a></li>";
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$lastpage\">$lastpage</a></li>";
}
else
{
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=1\">1</a></li>";
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=2\">2</a></li>";
$pagination.= "";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li class=\"active\"><a href=\"#\">$counter<span class=\"sr-only\">(current)</span></a></li>";
else
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
}
}
if ($page < $counter - 1)
$pagination.= " <li class=\"next\"><a href=\"$targetpage?page=$next\">»</a></li>";
else
$pagination.= "<li class=\"disabled\"><a href=\"#\">»</a></li>";
$pagination.= "</ul>\n";
}
請嘗試為無限ajax滾動添加一個以下的事件處理程序,如下所示:
jQuery.ias.on('noneLeft', function() {
console.log('We hit the bottom!');
});
並查看是否在瀏覽器控制台中打印了這個。 如果沒有打印,那么您的分頁鏈接就會出現問題。 交叉驗證。 使用firebug檢查您的HTML並檢查是否生成了有效鏈接。
如果你的分頁是正確的,如果你看到上面的noneLeft
事件,那么嘗試使用以下代碼來停止滾動:
pageNum = 0;
jQuery.ias.on('next', function(url) {
if (url.indexOf("/website/home/2") > -1 && pageNum > 2) {
return false;
}
else{
pageNum++;
}
})
我有一個純jquery的自定義解決方案,這里是如何工作的,
/** Javascript Block **/
//make a request(load_next_set_records) to records that go for content loading by scroll or click of button
$(".load_more_button").on('click',load_next_set_records);
function scrollDetectLimit(){//trigger load_next_set_records(); when scroll reaches a target}
load_next_set_records = function(){
$.ajax({
url : '/url_that_returns_json_records',
data:{
//required to return next paginated records assumed that records are wrapped within div.record_block that resembles loaded records
next_records_load_pointer:$(".record_block").length
},
dataType : 'json',
beforeSend:function(){},
success:function(response){
if(//response has many records)
//actions when records exists, append new set of records wrapped with div.record_block
else
//simply disable loadmore button see below in error call back
},
error:function(xhr){
//simply disable loadmore button
$(".load_more_button").before('No more to load');
$(".load_more_button").fadeOut();
}
});
}
我希望你得到代碼邏輯,這需要根據你的要求進一步更新代碼,讓我知道是否有困難進入它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.