最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to paginate data without reloading page? - Stack Overflow

programmeradmin1浏览0评论

I have learned pagination from the link How to set condition in following code for marking current page in pagination?

using code

<?php
    $id=3;  //  test value
    $page=20;   //  test value

    $li=array();    //  temporary array for convenience
    $template='<li%s><a href="?id=%s">%s</a></li>';
    if($id>1) $li[]=sprintf($template,'',$id-1,'Previous');
    for($i=1;$i<=$page;$i++) {
        if($i==$id) $li[]=sprintf($template,' class="active"',$i,$i);   //  Current
        else $li[]=sprintf($template,'',$i,$i);
    }
    if($id<$page) $li[]=sprintf($template,'',$id+1,'Next');
    $li=implode('',$li); // convert to string for printing
?>
<ul class="pagination">
<?php print $li; ?>
</ul>

But it is showing data after reloading data? is there any way to make it show without reloading page? I am new to pagination - please help.

I have learned pagination from the link How to set condition in following code for marking current page in pagination?

using code

<?php
    $id=3;  //  test value
    $page=20;   //  test value

    $li=array();    //  temporary array for convenience
    $template='<li%s><a href="?id=%s">%s</a></li>';
    if($id>1) $li[]=sprintf($template,'',$id-1,'Previous');
    for($i=1;$i<=$page;$i++) {
        if($i==$id) $li[]=sprintf($template,' class="active"',$i,$i);   //  Current
        else $li[]=sprintf($template,'',$i,$i);
    }
    if($id<$page) $li[]=sprintf($template,'',$id+1,'Next');
    $li=implode('',$li); // convert to string for printing
?>
<ul class="pagination">
<?php print $li; ?>
</ul>

But it is showing data after reloading data? is there any way to make it show without reloading page? I am new to pagination - please help.

Share Improve this question asked Dec 9, 2017 at 12:10 SumonSumon 531 silver badge4 bronze badges 4
  • You need to use Ajax for that. When clicking on "next" or "previous", make the call using ajax, get the response and replace the list using javascript. – M. Eriksson Commented Dec 9, 2017 at 12:14
  • @MagnusEriksson is correct. Whenever you click on button for the next page, you make an AJAX request that will fetch the data and you can append the new data ing in – cdoshi Commented Dec 9, 2017 at 12:16
  • I am not familiar with ajax, I know little jquery and javascript. Can you tell me how I can add ajax with the pagination code above? – Sumon Commented Dec 9, 2017 at 12:36
  • api.jquery./jquery.ajax that's the jquery documentation on Ajax. I I would suggest making a JSON array in PHP for the items and retrieve it with Ajax in jQuery. – Sinan Samet Commented Dec 9, 2017 at 12:50
Add a ment  | 

1 Answer 1

Reset to default 4

You can use pagination without page load by ajax or jQuery. Please read the example following the link below - https://datatables/examples/basic_init/alt_pagination.html

发布评论

评论列表(0)

  1. 暂无评论