request->getHostInfo() . Yii::$app->request->url;
// var_dump($url);die;
$url = preg_replace('/&page=[\d+]*$/', '', $url);
$url = preg_replace('/\?page=[\d+]*$/', '', $url);
$page = (int) Yii::$app->request->get('page', 1);
return PageHelper::pageList($url, $dataProvider->totalCount, $page, $dataProvider->pagination->pageSize);
}
/**
* 分页
* @param unknown_type $total
* @param unknown_type $pageid
* @param unknown_type $psize
* @param unknown_type $half
*/
static function pageList($url, $total, $pageid = 1, $psize = 10, $half = 2, $isHtm = false, $anchor = '', $seo = 0) {
//echo $pageid;
$pageData = self::createPageData($url, $total, $pageid, $psize, $half);
$pageLabelHtml = '';
$questionMark = $isHtm == false && strpos($url, '?') ? '&' : '?';
foreach ($pageData as $label) {
if ($label['status'] == 'enable') {
if ('<' == $label['page']) {
$page = $pageid - 1;
$class = 'tonext_on';
} elseif ('>' == $label['page']) {
$page = $pageid + 1;
$class = 'tonext_on';
} else {
$page = $label['page'];
$class = '';
}
if ($isHtm) {
if (($page == 1) && ($seo == 1)) { //seo需求,第一页和默认页url不一样,但是内容一致
$changeurl = str_replace('-', '', $url);
$page = '';
} else {
$changeurl = $url;
}
$pageLabelHtml .= '' . $label['page'] . ' ';
} else {
$pageLabelHtml .= '' . $label['page'] . ' ';
}
} elseif ($label['status'] == 'disabled') {
if ('<' == $label['page']) {
$pageLabelHtml .='< ';
} elseif ('>' == $label['page']) {
$pageLabelHtml .='> ';
} else {
$pageLabelHtml .= '' . $label['page'] . ' ';
}
} elseif ($label['status'] == 'current') {
$pageLabelHtml .= '' . $label['page'] . ' ';
}
}
$pageTotal = ceil($total / $psize); //总共的页数
if($pageLabelHtml){
$pageLabelHtml .= '';
$pageLabelHtml .= self::getbutton($total,$pageTotal);
}else{
$pageLabelHtml .=' 共 '.$pageTotal.' 页,共计 '.$total.' 条';
}
return $pageLabelHtml;
}
/**
* 分页数据构造
* Enter description here ...
* @param unknown_type $url
* @param unknown_type $total
* @param unknown_type $pageid
* @param unknown_type $psize
* @param unknown_type $half
*/
static function createPageData($url, $total, &$pageid = 1, $psize = 10, $half = 2) {
//分页函数,$total=总共的条数;$pageid=当前显示的页号;$psize=每页显示的条数;$half=$pageid前后显示的条数
$totalpage = ceil($total / $psize); //总共的页数
$pageid = $pageid > $totalpage ? $totalpage : $pageid;
$i = 0;
$arr = array();
if ($totalpage < 2) {//小于2页,不显示分页
return $arr;
}
$arr[$i]["page"] = "<";
$arr[$i]["status"] = $pageid == 1 ? "disabled" : 'enable';
if($totalpage<=10){
for ($i=1; $i <=$totalpage ; $i++) {
if($pageid==$i){
$arr[$i]["page"] = $pageid;
$arr[$i]["status"] = "current";
}else{
$arr[$i]["page"] = $i;
$arr[$i]["status"] = "enable";
}
}
}else{
$i++;
if($pageid>=$totalpage-$half-3){ //左边显示省略号 ...
$arr[$i]["page"] = 1;
$arr[$i]["status"] = "enable";
$i++;
$arr[$i]["page"] = "...";
$arr[$i]["status"] = "disabled";
$i++;
$j=$totalpage-2*$half-3;
while ($j<=$totalpage) {
if($pageid==$j){
$arr[$i]["page"] = $j;
$arr[$i]["status"] = "current";
}else{
$arr[$i]["page"] = $j;
$arr[$i]["status"] = "enable";
}
$i++;
$j++;
}
}
if($pageid<=$half+3){ //右边显示省略号 ...
while ($i<=2*$half+3) {
if($pageid==$i){
$arr[$i]["page"] = $pageid;
$arr[$i]["status"] = "current";
}else{
$arr[$i]["page"] = $i;
$arr[$i]["status"] = "enable";
}
$i++;
}
$arr[$i]["page"] = "...";
$arr[$i]["status"] = "disabled";
$i++;
$arr[$i]["page"] = $totalpage-1;
$arr[$i]["status"] = "enable";
$i++;
$arr[$i]["page"] = $totalpage;
$arr[$i]["status"] = "enable";
$i++;
}
if($pageid>$half+3&&$pageid<$totalpage-$half-3){ //俩边显示省略号 ...
$arr[$i]["page"] = 1;
$arr[$i]["status"] = "enable";
$i++;
$arr[$i]["page"] = "...";
$arr[$i]["status"] = "disabled";
$i++;
for ($j = 0; $j < $half; $j++, $i++) {//$pageid前面的[最多$half个]
if ($pageid - $half + $j < 2) {//
$i--; //使$i保持不变
continue;
}/**/
$arr[$i]["page"] = $pageid - $half + $j;
$arr[$i]["status"] = "enable";
} {//中间项
$arr[$i]["page"] = $pageid;
$arr[$i]["status"] = "current";
$i++;
}
for ($j = 0; $j < $half; $j++, $i++) {//$pageid后面的[最多($half-1)个]
if ($pageid + $j + 1 > $totalpage) {//
$i++;
break;
}
$arr[$i]["page"] = $pageid + $j + 1;
$arr[$i]["status"] = "enable";
}
$arr[$i]["page"] = "...";
$arr[$i]["status"] = "disabled";
$i++;
$arr[$i]["page"] = $totalpage-1;
$arr[$i]["status"] = "enable";
$i++;
$arr[$i]["page"] = $totalpage;
$arr[$i]["status"] = "enable";
$i++;
}
}
$arr[$i]["page"] = ">";
$arr[$i]["status"] = $pageid == $totalpage ? 'disabled' : "enable";
return $arr;
}
/**
* 跳转按钮 js
* TODO空格回车直接跳转
* @return string
*/
static function getbutton($total=1,$pageTotal=1) {
$url = Yii::$app->request->getHostInfo() . Yii::$app->request->url;
$url = preg_replace('/page=[\d+]*$/', 'page=', $url);
if (!strpos($url, '?')) {
$url .= '?page=';
}
if (!strpos($url, 'page=')) {
$url .= '&page=';
}
$js = '';
$button = ' 共 '.$pageTotal.' 页,共计 '.$total.' 条';
return $button . $js;
}
}
?>
//首页 实例类
public function actionIndex()
{
$searchModel = new DataSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$pageLabelHtml = PageHelper::getPage($dataProvider);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'pageLabelHtml' =>$pageLabelHtml
]);
}
//视图类
= GridView::widget([
'dataProvider' => $dataProvider,
'pager'=> [
'options'=>['class'=>'hidden']
],
'columns' => [
'id',
'name',
['class' => 'yii\grid\ActionColumn',
'header'=>'操作',
'template' => '{update} {delete}',
],
],
]); ?>
=$pageLabelHtml ?>