问题是:当我单击分页链接时,脚本正在生成新的html表内容,但它没有在浏览器中刷新表。
我有一个名为showUsers($limit, $offset)的类用户,它获取所需的所有数据,并将其显示在表中。
<table id="table" class="table table-striped table-bordered table-hover table-sm">
<thead class="thead-default">
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nickname</th>
<th>User ID</th>
</tr>
</thead>
<?php $user->showUsers($limit, $offset); ?>
</table>这是我的偏移量变体:$offset = ($page - 1) * $limit,$page是按下的分页链接的编号。
我就是这样生成分页链接的:
<div class="col-sm-9 mx-auto">
<?php
for ($i=1; $i <= $allPages ; $i++) {
echo "<span class='pag-link' id='$i' style='cursor: pointer; padding: 6px; border: 1px solid #ccc; margin: 3px;'>". $i. "</span>";
}
?>
</div>这是我的JS脚本:
//pagination-link handler.
$('.pag-link').click(function(){
var page = $(this).attr("id");
console.log(page);
$.ajax({
type: "POST",
url: "info.php",
data: {page: page},
success: function(data){
console.log(data); // show response from the php script.
}
});
});
setInterval(function(){ $(`#table`).load('info.php #table'); }, 1000);注意:setInterval(function(){ $("#table").load('info.php #table'); }, 1000);应该在表中获取HTML,并使用新表更新浏览器中的表。
因此,当我点击,一个页面的号码是发送到info.php文件。这将更改我的$offset变量,showUsers($limit, $offset)方法将生成具有不同数据的新表。
这是因为我看到了在javascript控制台中生成的所有新的HTML,但是setInterval(function(){ $("#table").load('info.php #table'); }, 1000);没有工作,我的表也没有更新。
注意:我希望按间隔刷新表,因为如果多个用户输入数据,我希望实时更新给每个人。
发布于 2017-03-20 20:11:09
.load('info.php #table')没有页面参数。
因此,您加载页面(" info.php "),单击下一个分页链接(url:"info.php",data:{ page : page}),然后计时器再次加载info.php,替换数据。
这将需要将"var页面“移到Javascript的顶部,在函数调用之外:
//pagination-link handler.
var page=1;
$('.pag-link').click(function(){
page = $(this).attr("id");
console.log(page);
$.ajax({
type: "POST",
url: "info.php",
data: {page: page},
success: function(data){
console.log(data); // show response from the php script.
}
});
});
setInterval(function(){ $(`#table`).load('info.php?page='+page+'#table'); }, 1000);填充$offset的PHP需要同时响应GET和POST,例如$_REQUEST。
https://stackoverflow.com/questions/42910801
复制相似问题