anime-db/pagination-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

anime-db/pagination-bundle

Composer 安装命令:

composer require anime-db/pagination-bundle

包简介

Pagination bundle

README 文档

README

Latest Stable Version Latest Unstable Version Total Downloads Build Status Coverage Status Scrutinizer Code Quality SensioLabsInsight StyleCI License

PaginationBundle

This repository is for AnimeDbPaginationBundle. GpsLabPaginationBundle, the new version of PaginationBundle, has been released and is available at https://github.com/gpslab/pagination-bundle.

AnimeDbPaginationBundle is no longer maintained and is now end of life.

Installation

Pretty simple with Composer, run:

composer require anime-db/pagination-bundle

Add PaginatorBundle to your application kernel

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new AnimeDb\Bundle\PaginationBundle\AnimeDbPaginationBundle(),
        // ...
    );
}

Configuration example

You can configure default templates

anime_db_pagination:
    max_navigate: 5 # default page range used in pagination control
    template: 'AnimeDbPaginationBundle::pagination.html.twig' # sliding pagination controls template

Usage

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration;

class ArticleController extends Controller
{
    /**
     * @Configuration\Route("/article/", name="article_index")
     * @Configuration\Method({"GET"})
     *
     * @param Request $request
     *
     * @return Response
     */
    public function indexAction(Request $request)
    {
        $per_page = 100; // articles per page
        $em = $this->get('doctrine.orm.entity_manager');
        $router = $this->get('router');

        // get total articles
        $total = (int)$em
            ->createQueryBuilder()
            ->select('COUNT(*)')
            ->from('AcmeDemoBundle:Article', 'a')
            ->getQuery()
            ->getSingleScalarResult();

        // build pagination
        $pagination = $this
            ->get('pagination')
            ->paginate(
                ceil($total / $per_page), // total pages
                $request->query->get('page') // correct page
            )
            ->setPageLink(function($page) use ($router) { // build page link
                return $router->generate('article_index', ['page' => $page]);
            })
            ->setFirstPageLink($router->generate('article_index')); // build link for first page

        // get articles chunk
        $articles = $em
            ->createQueryBuilder()
            ->select('*')
            ->from('AcmeDemoBundle:Article', 'a')
            ->setFirstResult(($pagination->getCurrentPage() - 1) * $per_page)
            ->setMaxResults($per_page)
            ->getQuery()
            ->getResult();

        // template parameters
        return $this->render('AcmeDemoBundle:Article:index.html.twig', [
            'total' => $total,
            'articles' => $articles,
            'pagination' => $pagination
        ]);
    }
}

From QueryBuilder

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration;
use Acme\DemoBundle\Entity\Article;

class ArticleController extends Controller
{
    /**
     * @var int
     */
    const PER_PAGE = 100;

    /**
     * @Configuration\Route("/article/", name="article_index")
     * @Configuration\Method({"GET"})
     *
     * @param Request $request
     *
     * @return Response
     */
    public function indexAction(Request $request)
    {
        // create get articles query
        // would be better move this query to repository class
        $query = $this
            ->getDoctrine()
            ->getRepository('AcmeDemoBundle:Article')
            ->createQueryBuilder('a')
            ->where('a.status = :status')
            ->setParameter('status', Article::STATUS_ENABLED);

        // build pagination
        $pagination = $this
            ->get('pagination')
            ->paginateQuery(
                $query, // query
                self::PER_PAGE, // articles per page
                $request->query->get('page') // correct page
            )
            ->setPageLink(function($page) { // build page link
                return $this->generateUrl('article_index', ['page' => $page]);
            })
            ->setFirstPageLink($this->generateUrl('article_index')); // build link for first page

        // template parameters
        return $this->render('AcmeDemoBundle:Article:index.html.twig', [
            'total' => $pagination->getTotalPages(), // total pages
            'articles' => $query->getQuery()->getResult(), // get articles chunk
            'pagination' => $pagination
        ]);
    }
}

View

{# total items #}
<div class="total">
    {{ total }}
</div>

{# list articles #}
<table>
    {% for article in articles %}
        <tr{% if loop.index is odd %} class="color"{% endif %}>
            <td>{{ article.id }}</td>
            <td>{{ article.title }}</td>
            <td>{{ article.date|date('Y-m-d, H:i:s') }}</td>
        </tr>
    {% endfor %}
</table>

{# display navigation #}
<div class="navigation">
    {{ pagination_render(pagination) }}
</div>

Custom view

{# display navigation #}
{{ pagination_render(pagination, 'custom_pagination.html.twig', {custom_var: 'foo'}) }}

Example Material Design template for pagination

{# custom_pagination.html.twig #}

{# print 'foo' #}
{{ custom_var }}

{% if pagination.total > 1 %}
{% spaceless %}
    <ul class="pagination">
        {% if pagination.prev %}
            <li>
                <a href="{{ pagination.prev.link }}" title="{{ 'previous.page'|trans }}">
                    <i class="material-icons">chevron_left</i>
                </a>
            </li>
        {% else %}
            <li class="disabled">
                <a href="#">
                    <i class="material-icons">chevron_left</i>
                </a>
            </li>
        {% endif %}
        {% for item in pagination %}
            {% if item.current %}
                <li class="active">
                    <a href="#" title="{{ 'current.page'|trans }}">{{ item.page }}</a>
                </li>
            {% else %}
                <li>
                    <a href="{{ item.link }}" title="{{ 'page.number'|trans({'%page%': item.page}) }}">{{ item.page }}</a>
                </li>
            {% endif %}
        {% endfor %}
        {% if pagination.next %}
            <li>
                <a href="{{ pagination.next.link }}" title="{{ 'next.page'|trans }}">
                    <i class="material-icons">chevron_right</i>
                </a>
            </li>
        {% else %}
            <li class="disabled">
                <a href="#">
                    <i class="material-icons">chevron_right</i>
                </a>
            </li>
        {% endif %}
    </ul>
{% endspaceless %}
{% endif %}

License

This bundle is under the MIT license. See the complete license in the file: LICENSE

统计信息

  • 总下载量: 71
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固