网络编程 
首页 > 网络编程 > 浏览文章

yii2分页之实现跳转到具体某页的实例代码

(编辑:jimmy 日期: 2025/4/3 浏览:3 次 )

先上图看效果,大家感觉还错请参考功能怎么实现的!

yii2分页之实现跳转到具体某页的实例代码

从上图中不难看出,我们制定跳转到某页的功能是基于linkpager之上的扩展,这根我们之前实现的分页扩展明显不同,之前的明显就是重写了!当然,这都不重要,我们看看GoLinkPager的具体实现!名字起的有点lower,不重要!

1、在frontend\components目录新建GoLinkPager类文件

2、该类继承yii\widgets\LinkPager;,如下:

namespace frontend\components; 
use yii\widgets\LinkPager; 
use yii\helpers\Html; 
class GoLinkPager extends LinkPager 
{ 
}

3、添加属性public $go = false; //是否包含跳转功能跳转 默认false

4、重写父类linkPager的renderPageButtons方法,具体直接参考下面完整版代码,可主要看go部分的代码实现。

<"form" style="float: left; color: #999; margin-left: 10px; font-size: 12px;">
     <span class="text">共 {$pageCount} 页</span>
     <span class="text">到第</span>
     <input class="input" type="number" value="{$goPage}" min="1" max="{$pageCount}" aria-label="页码输入框" style="text-align: center; height: 25px; line-height: 20px; margin-top: 5px; width: 46px;">
     <span class="text">页</span>
     <span class="btn go-page" role="button" tabindex="0" style="border: solid 1px #ccc; padding: 0px; height: 25px; width: 46px; line-height: 25px;">确定</span>
    </div> 
goHtml;
   $buttons[] = $goHtml;
   $pageLink = $this->pagination->createUrl(false);
   $goJs = <<<goJs
    $(".go-page").on("click", function () {
     var _this = $(this),
      _pageInput = _this.siblings("input"),
      goPage = _pageInput.val(),
      pageLink = "{$pageLink}";
      pageLink = pageLink.replace("page=1", "page="+goPage);
     if (goPage >= 1 && goPage <= {$pageCount}) {
      window.location.href=pageLink;
     } else {
      _pageInput.focus();
     }
    });
goJs;
   $this->view->registerJs($goJs);
  }
  return Html::tag('ul', implode("\n", $buttons), $this->options);
 }
}

下面看具体使用:

<?= GoLinkPager::widget([ 
 'pagination' => $pages, 
 'go' => true, 
]); ?>

可以看出,使用起来也是贼方便贼方便的!加一个属性go为true即可。

需要说明的是,完整版代码中go部分html js可根据自己需要自行修改整理!

以上内容是小编给大家介绍的yii2分页之实现跳转到具体某页的实例代码,希望对大家有所帮助!

上一篇:既简单又安全的PHP验证码 附调用方法
下一篇:php实现子字符串位置相互对调互换的方法 原创
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。