Last active
December 23, 2015 21:19
-
-
Save kilfu0701/6695048 to your computer and use it in GitHub Desktop.
CakePHP 1.x : pagination elements. Put this file in app/view/elements/ directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Need '$pagitor2' variable. | |
* Example: | |
* $pagitor2 = array( | |
* 'total' => 100, | |
* 'limit' => 15, | |
* 'page' => 2, // current page | |
* //'range' => 3, // optional. | |
* //'method' => 'get' // opt | |
* //'info' => true, // opt | |
* //'remove_null' => true, // opt, 移除null | |
* ); | |
*/ | |
$pagitor2['range'] = isset($pagitor2['range']) ? $pagitor2['range'] : 3; | |
$pagitor2['max_page'] = ceil( $pagitor2['total']/$pagitor2['limit'] ); | |
if($pagitor2['max_page']==0) { | |
$pagitor2['max_page'] = 1; | |
} | |
if($pagitor2['page']>1) { | |
$pagitor2['prev'] = $pagitor2['page'] - 1; | |
} | |
if($pagitor2['page']<$pagitor2['max_page']) { | |
$pagitor2['next'] = $pagitor2['page'] + 1; | |
} | |
if($pagitor2['page']-$pagitor2['range']<1) { | |
$pagitor2['start'] = 1; | |
$inc = $pagitor2['range'] - $pagitor2['page']; | |
} else { | |
$pagitor2['start'] = $pagitor2['page'] - $pagitor2['range']; | |
$inc = 0; | |
} | |
if($pagitor2['page']+$pagitor2['range']+$inc>$pagitor2['max_page']) { | |
$pagitor2['end'] = $pagitor2['max_page']; | |
} else { | |
$pagitor2['end'] = $pagitor2['page']+$pagitor2['range']+$inc; | |
} | |
$dec = $pagitor2['page'] + $pagitor2['range'] - $pagitor2['max_page']; | |
if($dec > 0) { | |
$pagitor2['start'] = ($pagitor2['start'] - $dec > 0) ? $pagitor2['start'] - $dec : 1; | |
} | |
$pagitor2['page_range'] = range($pagitor2['start'], $pagitor2['end']); | |
?> | |
<?php | |
if(isset($pagitor2['method'])&&$pagitor2['method']=="get"): | |
$_params = $this->params; // make a copy. | |
$argv = $this->webroot.$_params['url']['url']; | |
unset($_params['url']['url']); | |
if(isset($_params['url']['p'])) { | |
unset($_params['url']['p']); | |
} | |
if(isset($pagitor2['remove_null']) && $pagitor2['remove_null']===true) { | |
foreach($_params['url'] as $k => $v) { | |
if($v=='') { | |
unset($_params['url'][$k]); | |
} | |
} | |
} | |
$_params['url']['dummy'] = ''; | |
$argv .= '?'; | |
$argv .= http_build_query($_params['url']); | |
$argv = substr($argv, 0, -1); | |
?> | |
<style> | |
/* page */ | |
.page{ overflow:hidden; text-align:center; padding:25px 0;} | |
.page span{padding:0 4px;} | |
.page a, .page span a{ border:1px solid #ccc; font:12px/1.2 Arial; padding:0 4px; color:#999;} | |
.page a.back,.page a.next{ color:#656565; border:0;} | |
.page a.select, .page span.current{ color:#3ea6d7; border:0;} | |
.page a.more{ border:0;} | |
</style> | |
<div class="page"> | |
<?php if(isset($pagitor2['prev'])): ?> | |
<?php if($pagitor2['page']!=1): ?> | |
<a class="next" href="<?php echo $argv.'&p=1'; ?>">第一頁</a> | |
<span>|</span> | |
<?php endif; ?> | |
<a class="back" href="<?php echo $argv.'&p='.$pagitor2['prev']; ?>">上一頁</a> | |
<a class="back" href="<?php echo $argv.'&p='.$pagitor2['prev']; ?>">«</a> | |
<?php endif; ?> | |
<?php foreach($pagitor2['page_range'] as $k => $v): ?> | |
<?php if($v==$pagitor2['page']): ?> | |
<a class="select"><?php echo $v; ?></a> | |
<?php else: ?> | |
<a href="<?php echo $argv.'&p='.$v; ?>"><?php echo $v; ?></a> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
<?php if(isset($pagitor2['next'])): ?> | |
<a class="next" href="<?php echo $argv.'&p='.$pagitor2['next']; ?>">»</a> | |
<a class="next" href="<?php echo $argv.'&p='.$pagitor2['next']; ?>">下一頁</a> | |
<span>│</span> | |
<?php endif; ?> | |
<?php if($pagitor2['max_page']!=$pagitor2['page']): ?> | |
<a class="next" href="<?php echo $argv.'&p='.$pagitor2['max_page']; ?>">最後一頁</a> | |
<?php endif; ?> | |
</div> | |
<?php else: ?> | |
<div class="page go_page"> | |
<?php if(isset($pagitor2['prev'])): ?> | |
<?php if($pagitor2['page']!=1): ?> | |
<a class="next gopage_1" href="#">第一頁</a> | |
<span>|</span> | |
<?php endif; ?> | |
<a class="back gopage_<?php echo $pagitor2['prev']; ?>" href="#">上一頁</a> | |
<a class="back gopage_<?php echo $pagitor2['prev']; ?>" href="#">«</a> | |
<?php endif; ?> | |
<?php foreach($pagitor2['page_range'] as $k => $v): ?> | |
<?php if($v==$pagitor2['page']): ?> | |
<a class="select"><?php echo $v; ?></a> | |
<?php else: ?> | |
<a href="#" class="gopage_<?php echo $v; ?>"><?php echo $v; ?></a> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
<?php if(isset($pagitor2['next'])): ?> | |
<a class="next gopage_<?php echo $pagitor2['next']; ?>" href="#">»</a> | |
<a class="next gopage_<?php echo $pagitor2['next']; ?>" href="#">下一頁</a> | |
<span>│</span> | |
<?php endif; ?> | |
<?php if($pagitor2['max_page']!=$pagitor2['page']): ?> | |
<a class="next gopage_<?php echo $pagitor2['max_page']; ?>" href="#">最後一頁</a> | |
<?php endif; ?> | |
</div> | |
<?php endif; ?> | |
<?php if(isset($pagitor2['info']) && $pagitor2['info']===true): | |
$_start = ($pagitor2['page']-1)*$pagitor2['limit'] + 1; | |
$_end = $pagitor2['page'] * $pagitor2['limit']; | |
if($_end > $pagitor2['total']) { | |
$_end = $pagitor2['total']; | |
} | |
?> | |
<div style="text-align:center;">共 <?php echo $pagitor2['total']; ?> 筆,顯示第 <?php echo $_start; ?> ~ <?php echo $_end; ?> 筆</div> | |
<?php endif; ?> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
// change page onclick. [POST] | |
// if pagination use 'POST' method, modify this part to satisfy your own form farmat. | |
$('.go_page a').not('.select').click(function(e) { | |
var _form = document.MY_FORM, // XXX: change this to your form | |
_page = $(this).attr('class').split('_')[1]; | |
// need a hidden input for 'page'. | |
$(_form).find('input#page').val(_page); | |
_form.submit(); | |
return false; | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment