页面加载时的Ajax分页?

时间:2013-10-15 19:57:55

标签: javascript jquery ajax

好的,首先我会告诉它应该如何工作:我有一个带有图像链接的页面,点击图像,该链接的信息出现在另一个div中。我使用jquery / ajax将链接id发布到php文件并将该数据返回到所选的div。 链接应该分页,以便一次显示4个。

这就是IS发生的事情:帖子部分没问题,当我点击一个链接时,正确的数据显示在所选的div中。我不知道如何使链接div paginate。我需要在页面加载时对它们进行分页,现在当页面加载所有链接时显示,然后当我点击链接时显示正确的数量(4)!

这是我的带有2个div的html:

<div class="dogsrehomeandrehomed">
<?php
include 'inc/connect.php';
$q = mysqli_query($link, "SELECT filename, id, name, age, sex  FROM gallery WHERE 
gallery = 1 ORDER BY id DESC") or die (mysql_error());
while($row = mysqli_fetch_array($q)){
$data = $row['filename'];
$file = substr($data, strpos($data, "/") + 1);
echo"<div class='homedogs'>",
"<a href={$row['id']} class='dogchoice'>",
"<img class='nailthumb-container3' src='$file' alt='{$row['name']}. Image' />",
"</a>",
"<br />",
'NAME: ',$row['name'],"<br />",'AGE: ',$row['age'],"<br />",'SEX: ',$row['sex'],
"</div>";
}
?>
</div>

<div class="dog">
<?php
include 'inc/connect.php';
$q = mysqli_query($link, "SELECT *  FROM gallery WHERE gallery = 1 ORDER BY id DESC 
LIMIT 1") or die (mysql_error());
while($row = mysqli_fetch_array($q)){
$data = $row['filename'];
$file = substr($data, strpos($data, "/") + 1);
echo"<div class='rehomediv'>",
"<img class='nailthumb-container2' src='$file' alt='{$row['name']}. Image' />","<br   
/>",
"<div class='nameagesex'>",
'NAME: ',$row['name'],"<br />",'AGE: ',$row['age'],"<br />",'SEX: ',$row['sex'],
"</div>",
"<div class='description'>",
nl2br($row['description']),
"</div>",
"</div>";
}
?>          
</div>
<script src="js/dog.js"></script>

这是我的dog.js文件:

$('a.dogchoice').click(function(e) {
e.preventDefault();
var linkClass = $(this).attr("class");
var linkText = new String(this);
var categoryValue = linkText.substring(linkText.lastIndexOf('/') + 1);
var params = {};
params[linkClass] = categoryValue;  
$.post('inc/dogchoice.php', params, function(data) {
    var totalRecords = $(data).length;
    var pageSize = 4;
    var numOfPages = Math.ceil(totalRecords / pageSize);
    var i,
      pageLinks = '<div class="pageLinks">';
    for (i = 0; i < numOfPages; i++) {
      pageLinks += '<a href="#" onclick="showDogLinks(' + i + ');return false;">' + (i 
      + 1) + '<\/a> ';
    }
    pageLinks += '<\/div>';
    $('.dog').html(pageLinks + data);
    showDogLinks(0);
});

});

//function to slice up records into pages
function showDogLinks( pageNo ) { 
var perPage = 4; 
var start = pageNo * perPage; 
var end = start + perPage; 
$('.homedogs').hide().filter(function(index) { 
    return ( (index > (start-1)) && ( index < end ) ); 
} ).show(); 
}

有人可以帮忙吗? 谢谢你看

1 个答案:

答案 0 :(得分:0)

我已经对它进行了分类。我只是使用php对链接容器进行了分页。

感谢您的光临..