Paginate records on Client side issue

后端 未结 1 1518
一整个雨季
一整个雨季 2020-12-12 06:25

I am working on a search,filter operation using php,mysql.

My classes for pagination are

class Paginator{
var $items_per_page;
var $items_total;
var          


        
相关标签:
1条回答
  • 2020-12-12 06:46

    As stated in my comments.

    You could do the following:

    $(document).ready(function()
    {
        $('.paginate').live('click', function(e)
        {
            e.preventDefault();
            var btnPage = $(this);
            $.ajax(
            {
                url : btnPage.attr('href'),
                success : function(resp)
                {
                    // replace current results with new results.
                    $('#project_section').html(resp);
                },
                error : function()
                {
                    window.location.href = btnPage.attr('href');
                }
            });
        });
    });
    

    The above will replicate you clicking on each of the pagination links.

    What I would advise to do next is to separate the PHP code and HTML that generates your "results" list into a separate file.

    This way, on the page that displays the results, you can simply use include('path-to-results-file.php'); which will work for non-ajax requests and then you could do:

    Process.php

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
    {
        include('path-to-results-file.php');
        die();
    }
    

    The above would detect if an ajax request has been made and if so, instead of displaying the whole page including the results, it will simply display only the results and the pagination.

    Updated to include a better explanation

    Below is a VERY simple example of what I mean.

    Current process.php

        <?
        // currently contains all of the code required
        // to query the database etc.
    ?>
    <html>
    <head>...</head>
    <body>
        <!-- header content -->
        <table>
        <?
            // currently contains all of the code required to display
            // the results table and pagination links.
        ?>
        </table>
        <!-- footer content -->
    </body>
    </html>
    

    New process.php

    <?
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
        {
            include('path-to-results-file.php');
            die();
        }
    ?>
    <html>
    <head>...</head>
    <body>
        <!-- header content -->
        <? include('path-to-results-file.php'); ?>
        <!-- footer content -->
    </body>
    </html>
    

    New path-to-results-file.php

    <?
        // currently contains all of the code required
        // to query the database etc.
    ?>
    <table>
    <?
        // currently contains all of the code required to display
        // the results table and pagination links.
    ?>
    </table>
    

    Now... When you go to process.php normally via your browser, or when javascript is disabled. It will simply work the same way it does without Javascript now.

    When you go to process.php and then click on one of the pagination links (with javascript enabled), process.php will detect that you are using Ajax and only send back the table of results.

    0 讨论(0)
提交回复
热议问题