cakephp – Paginator

UsersController

W metodzie używającej stronicowania

use Cake\Http\Exception\NotFoundException;
....
public function index()
{

   $this->paginate = [ 
    'order'         => ['deleted' => 'asc', 'active' => 'desc', 'Roles.name', 'Users.firstname'], 
    'contain'       => ['Roles.Departments'], 
    'conditions'    => ['is_admin' => false], 
    'limit'         => 35,   // def. 25 
    'maxLimit'      => 50,   // def. 100 
    'sortWhitelist' => ['active', 'deleted', 'firstname', 'tel', 'email, 'Roles.name', 'Departments.name'] ];

   try {
      $users = $this->paginate();
      $this->set(compact('users'));
   } catch (NotFoundException $e) {
      $this->Flash->error("Błędne odwołanie. Nie ma takiej strony");
      return $this->redirect("/users");
   }
}

W widoku index.ctp w komórkach tabeli nagłówka – odnośniki sortowania

<?= $this->Paginator->sort('id', 'ID') ?>
<?= $this->Paginator->sort('deleted', 'X') ?>
<?= $this->Paginator->sort('active', 'A') ?>
...

Odnośniki paginatora – zmiana stron – pod tabelą

<nav class="paginator d-flex justify-content-center">
  <ul class="pagination">
//  <?= $this->Paginator->first('<< ' . __('FIRST')) ?>
    <?= $this->Paginator->prev( '<< ') ?>
    <?= $this->Paginator->numbers() ?>
    <?= $this->Paginator->next(' >>' ) ?>
 // <?= $this->Paginator->last(__('LAST') . ' >>') ?>
  </ul>
</nav>

Licznik paginatora – podsumowanie – na górze tabeli

<div class="float-right">
<p><?= $this->Paginator->counter(['format' => __('Strona {{page}} z {{pages}}, {{current}} pozycji z {{count}}')]) ?></p>
</div>

Szablon paginatora zgodny z bootstrap – config/paginator-templates.php

return [
  'prevDisabled' => '<li class="page-item"><span class="page-link">{{text}}</span></li>',
  'prevActive' => '<li class="page-item"><a class="page-link" href="{{url}}">{{text}}</a></li>',
  'current' => '<li class="page-item active"><span class="page-link">{{text}}</span></li>',
  'number' => '<li class="page-item"><a class="page-link" href="{{url}}">{{text}}</a></li>',
  'nextActive' => '<li class="page-item"><a class="page-link" href="{{url}}">{{text}}</a></li>',
  'nextDisabled' => '<li class="page-item"><span class="page-link">{{text}}</span></li>',
];

Użycie szablonu własnego – View/AppView.php

// In your AppView.php
public function initialize()
{
    ...
    $this->loadHelper('Paginator', ['templates' => 'paginator-templates']);
}

Przechwytywanie błędów – dostęp do strony, która nie istnieje

use Cake\Http\Exception\NotFoundException;

public function index()
{
    try {
        $this->paginate();
    } catch (NotFoundException $e) {
        // Do something here like redirecting to first or last page.
        // $this->request->getParam('paging') will give you required info.
    }
}

Zmiana ilości wyświetlanych rekordów – limit

<th scope="col" class="actions">
   <?= $this->Paginator->limitControl(
     [15 => 15, 25 => 25, 50 => 50, 100 => 100], 
     50, 
     ['label' => false, 'class' => 'form-control']) ?>
</th>

Opcje: limitControl ( tablica wartości, wartość domyślna, tablica opcji (label, class, …) )

Dodaj komentarz