Paginator example please for nette 2.1.0

Notice: This thread is very old.
kle_py
Member | 4
+
0
-

Hi,

i am trying to add paginator feature to the blog of the quickstart. I have seen the doc page, but i cannot get it work :( .
I also see there is a plugin calles VisualPaginator and tried it too. Is it still compatible and better than the paginator which comes in the package (/utils directory) ?

It would lead me a step further using nette if i could get this working.
Thank You

vyvazil.jakub
Member | 6
+
0
-

Hi kle_py,

VisualPaginator should work with version 2.1 as well. Anyway, VisualPaginator is an extension of Paginator. I believe you would like to you VisualPaginator, so you should download VisualPaginator and copy the folder to your project (ie /app/components/).

Your presenter should look like this:

<?php
class DefaultPresenter extends \BasePresenter {

    /** @var  FrontRepository */
    private $repository;

    public function inject(FrontRepository $repository) {
        $this->repository = $repository;
    }

    public function renderDefault() {
	// set visual paginator
        $vp = new \VisualPaginator($this, 'vp');

        $paginator = $vp->getPaginator();
	//set items per page
        $paginator->itemsPerPage = 15;
	//set count of items from db
        $paginator->itemCount = $this->repository->findAll()->count();

	// fetch data from db to template
        $this->template->items = $this->repository->findAll()->limit($paginator->itemsPerPage, $paginator->offset);
    }
?>

You will need FrontRepository.php, so create something like this:

<?php

namespace Models;

use Nette;

class FrontRepository extends Nette\Object {

    /** @var Nette\Database\Context */
    private $database;

    public function __construct(Nette\Database\Context $connection) {
        $this->database = $connection;
    }

    /** @return Nette\Database\Table\Selection */
    public function findAll() {
        return $this->database->table('items');
    }
}
?>

You have to set up your database connection in config.neon, but I assume you've already done that. Now just simply put {control vp} in your template (in this case default.latte}.

There is a template.phtml file in Visual Paginator – that's the way, how to simply modify the look of Visual Paginator.

Last edited by vyvazil.jakub (2014-02-04 13:01)