Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/everlutionsk/pagination-bundle


https://github.com/everlutionsk/pagination-bundle

bundle pagination symfony3

Last synced: 1 day ago
JSON representation

Awesome Lists containing this project

README

        

# Everlution Pagination bundle

## Features

- simple pagination for tables
- sorting headers for tables
- based on Doctrine Paginator (for now)

## Installation

### Step 1: Download the Bundle

```console
$ composer require everlutionsk/pagination-bundle
```

### Step 2: Enable the Bundle

```php
render(
'AppBundle:User:list.html.twig',
['userPage' => $this->get('uc.client.list')->getPage()]
);
}
```

### Templating in list.html.twig:

```twig



{# first argument is label which is automatically translated; second is query string - please do use underscore notation #}
{{ sortable_header('Name', 'name') }}
{{ sortable_header('Job title', 'job_title') }}
Status
Options



{% for user in userPage.items %}

{{ user }}
{{ user.jobTitle }}
{ status of the user ... }
{ links for edit/delete ... }

{% endfor %}


{{ paginate(userPage) }}
```

### UseCase for listing:

```php
requestStack = $requestStack;
$this->listQuery = $listQuery;
$this->pagination = $pagination;
}

public function getPage(): Page
{
$request = $this->requestStack->getCurrentRequest();

try {
$query = $this->listQuery->getUserViewListQuery();
$this->pagination->setQueryBuilder($query);

// limit and offsete here are provided automagically by RequestTransformer from {page} parameter of URL
return $this->pagination->paginate(
(int) $request->get('limit', Page::DEFAULT_PAGE_SIZE),
(int) $request->get('offset', self::DEFAULT_PAGINATION_OFFSET)
);
} catch (MaxResultsExceeded $exception) {
throw new WrongPaginationArgument($exception->getMessage());
}
}
}
```

Service definition:

```yaml
services:
uc.client.list:
class: Library\UseCase\User\ListUsers
arguments:
- '@doctrine.repository.user'
- '@user.pagination'
- '@request_stack'
```

Repository method for getting data:

```php
public function getUserViewListQuery(): QueryBuilder
{
return $this
->createQueryBuilder('user')
->select('PARTIAL user.{id, firstName, lastName, jobTitle}');
}
```

### Actual sort implementation detail:

Name sorting:

```php
getName() instanceof DoctrineSortColumn) {
return;
}

$builder
->addOrderBy('user.firstName', $query->getName()->getDirection())
->addOrderBy('user.lastName', $query->getName()->getDirection());
}
}
```

Job title sorting:

```php
getJobTitle() instanceof DoctrineSortColumn) {
return;
}

$builder->addOrderBy('user.jobTitle', $query->getJobTitle()->getDirection());
}
}
```

Service definition:

```yaml
services:
# implementation of Doctrine sorting
user.sort.name:
class: Library\UseCase\User\Sort\Name
public: false
user.sort.job_title:
class: Library\UseCase\User\Sort\JobTitle
public: false

# container for Doctrine sorting rules
user.sort.rules_provider:
class: Everlution\PaginationBundle\Pagination\Sort\SortRulesContainer
public: false
calls:
- ['addRule', ['@user.sort.name']]
- ['addRule', ['@user.sort.job_title']]

# concrete implementation of user list sorting
user.sort.doctrine_sort_query:
class: Everlution\PaginationBundle\Pagination\Sort\DoctrineSortQuery
arguments:
- "@user.sort.rules_provider"
- '@pagination.sort.request_sort_query'

# implementation of Doctrine filters
user.filter.name:
class: Library\UseCase\User\Filter\Name
public: false
user.filter.job_title:
class: Library\UseCase\User\Filter\JobTitle
public: false

# definition of user filter container with all filters
user.filter.container:
class: Everlution\PaginationBundle\Pagination\Filter\FilterContainer
arguments:
-
- "@user.filter.name"
- '@user.filter.job_title'

# query pagination with concrete sort for Docrine
user.pagination:
class: Everlution\PaginationBundle\Pagination\QueryPagination
arguments:
- '@user.filter.container'
- '@user.sort.doctrine_sort_query'
- '%everlution.pagination.max_page_size%'
```

## TODO

- automate sort rules creation
- create abstraction to Doctrine so we can use whatever we want (Elasticsearch etc)
- provide usage for Filtering the table
- pagination template for multiple css frameworks (SemanticUI, Bootstrap, Materialize)