在 cakephp 3.2 中的 Ajax 自定義分頁

在這裡,我將在 cakephp 3.2 中解釋 ajax 自定義分頁。這個更容易使用和理解。

在任何函式和你需要的任何控制器中執行此程式碼。

對分頁進行 ajax 呼叫

<script type="text/javascript">

$(document).ready(function () { 
    

    $("#count-order-tr").load(strUrl + "http://test.com/Appadmins/ajaxLoadSalesListing"); //load initial records
   

    //executes code below when user click on pagination links
    $("#count-order-tr").on("click", ".pagination a", function (e) { 

        e.preventDefault(); 

        var page = $(this).attr("data-page");

           $("#count-order-tr").load(strUrl + "Appadmins/ajaxLoadSalesListing", {"page": page}, function (data) {
           
            
        });

    });
    

});

在 ajax 帖子上獲取當前請求的頁面號(1,2,3 …)

public function ajaxLoadSalesListing() {
    $this->viewBuilder()->layout('ajax');

 if(isset($_POST["page"])){

    $page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number

    if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number

}else{ 
    $page_number = 1; //if there's no page number, set it to 1 
 }

$item_per_page=5;//total number of records in a page.I have assumed it here 5

 $get_total_rows = $this->Models->find('all')->count(); //hold total records in variable

  $total_pages = ceil($get_total_rows/$item_per_page);//count total number of pages 

$this->paginate['limit'=>$item_per_page ,'page'=>$page_number,'order'=>['Models.id'=>'DESC']];//Here we can give page ,limit ,order ,conditions and contain also.

$lists=$this->paginate($this->Models);//this is important ,this will do all the stuffs.This one is used for the paginations.

然後將你的資料設定為 ajax ctp。

  $this->set(compact('Lists','item_per_page','page_number','get_total_rows','total_pages'));}

將分頁部分放在 ajax ctp 頁面中,這樣它每次都會重新整理並載入所有資料。

現在獲取 ctp 頁面中的所有程式碼(ajax_load_sales_listing.ctp)並在迴圈後將此程式碼放入其中

Custom-> paginate_function($ item_per_page,$ page_number,$ get_total_rows,$ total_pages);?>

然後從自定義幫助程式中呼叫此 paginate_function。

function paginate_function($item_per_page, $current_page, $total_records, $total_pages){
 $pagination = '';
if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number
    $pagination .= '<ul class="pagination">';
    
    $right_links    = $current_page + 3; 
    $previous       = $current_page - 3; //previous link 
    $next           = $current_page + 1; //next link
    $first_link     = true; //boolean var to decide our first link
    
    if($current_page > 1){
        $previous_link = ($previous==0)? 1: $previous;
        $pagination .= '<li class="first"><a href="javscript:;" data-page="1" title="First">&laquo;</a></li>'; //first link
        $pagination .= '<li><a href="javscript:;" data-page="'.($current_page-1).'" title="Previous">&lt;</a></li>'; //previous link
            for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links
                if($i > 0){
                    $pagination .= '<li><a href="#" data-page="'.$i.'" title="Page'.$i.'">'.$i.'</a></li>';
                }
            }   
        $first_link = false; //set first link to false
    }
    
    if($first_link){ //if current active page is first link
        $pagination .= '<li class="first active">'.$current_page.'</li>';
    }elseif($current_page == $total_pages){ //if it's the last active link
        $pagination .= '<li class="last active">'.$current_page.'</li>';
    }else{ //regular current link
        $pagination .= '<li class="active">'.$current_page.'</li>';
    }
            
    for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links
        if($i<=$total_pages){
            $pagination .= '<li><a href="javscript:;" data-page="'.$i.'" title="Page '.$i.'">'.$i.'</a></li>';
        }
    }
    if($current_page < $total_pages){ 
            $next_link = ($i > $total_pages) ? $total_pages : $i;
            $pagination .= '<li><a href="javscript:;" data-page="'.($current_page+1).'" title="Next">&gt;</a></li>'; //next link
            $pagination .= '<li class="last"><a href="javscript:;" data-page="'.$total_pages.'" title="Last">&raquo;</a></li>'; //last link
    }
    
    $pagination .= '</ul>'; 
}
return $pagination; }}//return pagination links

然後分頁設定。