cakephp-3.0
cakephp 3.2의 Ajax 커스텀 페이지 매김
수색…
cakephp 3.2의 Ajax 커스텀 페이지 매김
여기 cakephp 3.2에서 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) {
});
});
});
아약스 게시물에 현재 요청한 페이지 번호 (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'));}
pagination 부분을 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
그러면 페이지 매김이 설정됩니다.
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow