当前位置: 移动技术网 > IT编程>开发语言>PHP > Laravel框架分页实现方法分析

Laravel框架分页实现方法分析

2018年08月10日  | 移动技术网IT编程  | 我要评论

本文实例讲述了laravel框架分页实现方法。分享给大家供大家参考,具体如下:

laravel使用的过程中,有些功能把前端页面的表达“写死了”,比如分页的翻页按钮!

当然你会说laravel的bootstrap样式也很好看啊,但是实际项目中,翻页按钮常常需要满足的客户的需要,特别在开发一款支持手机适配的web app,更是需要使用自定义的样式。

所以,学习一样东西不能一知半解,而是究其原理。

先来看看laravel是怎么分页的,生成分页按钮的代码究竟写在了哪里?

laravel目录\vendor\laravel\framework\src\illuminate\pagination

先理一下类的继承关系

presentercontract(父类)
bootstrapthreepresenter(子类)<-simplebootstrapthreepresenter
bootstrapfourpresenter(子类)<-simplebootstrapfourpresenter

从作者对类的命名上看,必有区别,我们从代码上研究

bootstrapthreepresenter.php和bootstrapfourpresenter.php主要区别在下列函数

bootstrapthreepresenter.php代码:

/**
* get html wrapper for an available page link.
*
* @param string $url
* @param int $page
* @param string|null $rel
* @return string
*/
protected function getavailablepagewrapper($url, $page, $rel = null)
{
    $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';
    return '<li><a href="'.htmlentities($url).'" rel="external nofollow" rel="external nofollow" '.$rel.'>'.$page.'</a></li>';
}
/**
* get html wrapper for disabled text.
*
* @param string $text
* @return string
*/
protected function getdisabledtextwrapper($text)
{
    return '<li class="disabled"><span>'.$text.'</span></li>';
}
/**
* get html wrapper for active text.
*
* @param string $text
* @return string
*/
protected function getactivepagewrapper($text)
{
    return '<li class="active"><span>'.$text.'</span></li>';
}

bootstrapfourpresenter.php代码:

/**
* get html wrapper for an available page link.
*
* @param string $url
* @param int $page
* @param string|null $rel
* @return string
*/
protected function getavailablepagewrapper($url, $page, $rel = null)
{
    $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';
    return '<li class="page-item"><a class="page-link" href="'.htmlentities($url).'" rel="external nofollow" rel="external nofollow" '.$rel.'>'.$page.'</a></li>';
}
/**
* get html wrapper for disabled text.
*
* @param string $text
* @return string
*/
protected function getdisabledtextwrapper($text)
{
    return '<li class="page-item disabled"><a class="page-link">'.$text.'</a></li>';
}
/**
* get html wrapper for active text.
*
* @param string $text
* @return string
*/
protected function getactivepagewrapper($text)
{
    return '<li class="page-item active"><a class="page-link">'.$text.'</a></li>';
}

我们发现最大的区别在threepresenter几乎是“裸”html标签,而fourpresenter生成的是带class的html标签。

无论是threepresenter还是fourpresenter,他们都有一个相同实现的render()函数

/**
* convert the url window into bootstrap html.
*
* @return \illuminate\support\htmlstring
*/
public function render()
{
    if ($this->haspages()) {
      return new htmlstring(sprintf(
        '<ul class="pagination">%s %s %s</ul>',
        $this->getpreviousbutton(),
        $this->getlinks(),
        $this->getnextbutton()
      ));
    }
    return '';
}

细心的读者已经发觉,还有两个继承类,分别是simplethreepresenter和simplefourpresenter,既然是simple(简单),区别就在他们的render()函数

/**
* convert the url window into bootstrap html.
*
* @return \illuminate\support\htmlstring
*/
public function render()
{
    if ($this->haspages()) {
      return new htmlstring(sprintf(
        '<ul class="pager">%s %s</ul>',
        $this->getpreviousbutton(),
        $this->getnextbutton()
      ));
    }
    return '';
}

也就是说,simplethreepresenter和simplefourpresenter生成的分页按钮是没有“页码”的,只有“上一页”和“下一页”按钮。

更多关于laravel相关内容感兴趣的读者可查看本站专题:《laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于laravel框架的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网