在 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">«</a></li>'; //first link
$pagination .= '<li><a href="javscript:;" data-page="'.($current_page-1).'" title="Previous"><</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">></a></li>'; //next link
$pagination .= '<li class="last"><a href="javscript:;" data-page="'.$total_pages.'" title="Last">»</a></li>'; //last link
}
$pagination .= '</ul>';
}
return $pagination; }}//return pagination links
然后分页设置。