Paginátor nette 2.4 vs 3.0

Newer
Člen | 47
+
0
-

Ahoj mám problém s paginací článků, která mi normálně běžela na nette 2.4, u 2 projektů....... Nerozumím tomu proč teď nejde, prolejzal jsem API Nette a nejsem schopen nalézt proč mi to nejde, Už se s tím trápím asi 5 hodin a prolezl jsem to celé několikrát tam a zpět a prostě nevím.

Mám to přes componentu, přístupnou přes DI,

config:

`parameters:
pageRadius: 5
productsPerPage: 4

services:

– App\MagazineModule\Model\ArticleManager
- App\MagazineModule\Presenters\ArticlePresenter(%defaultArticleUrl%)

paginationControlFactory:
implement: App\MagazineModule\Components\IPaginationControlFactory
setup:
- setRadius(%pageRadius%)
- setItemsPerPage(%productsPerPage%)`

paginationControl.php vypadá takto:

<?php

namespace App\MagazineModule\Components;

use Nette\Application\UI\Control;
use Nette\Http\Request;
use Nette\Http\Urlscript;
use Nette\Utils\Paginator;

/**
 * Komponenta pro správu a vykreslování stránkování.
 * @package App\MagazineModule\Components
 */
class PaginationControl extends Control {


    const TEMPLATE = __DIR__ . '/../templates/components/pagination.latte';


    private $radius = 5;


    private $paginator;


    private $url;


    public function __construct(Request $request) {
        parent::__construct();
        $this->paginator = new Paginator();
        $this->url = $request->getUrl();
    }


    public function setRadius($radius) {
        if (is_numeric($radius))
            $this->radius = (int) $radius;
    }


    public function getPaginator() {
        return $this->paginator;
    }


    public function setItemsPerPage($itemsPerPage) {
        $this->paginator->setItemsPerPage($itemsPerPage);
    }

    /** Vykresluje komponentu pro správu a vykreslování stránkování. */
    public function render() {
        $this->template->setFile(self::TEMPLATE); // Nastaví šablonu komponenty.
        // Deklarace pomocných proměných.
        $page = $this->paginator->getPage();
        $pages = $this->paginator->getPageCount();

        // Předává parametry do šablony.
        $this->template->page = $page;
        $this->template->pages = $pages;
        $this->template->left = $page - $this->radius >= 1 ? $page - $this->radius : 1;
        $this->template->right = $page + $this->radius <= $pages ? $page + $this->radius : $pages;
        $this->template->url = $this->url;
        $this->template->render(); // Vykreslí komponentu.
    }

}


interface IPaginationControlFactory {


    public function create();
}


?>

latte:
`<nav aria-label=„Page navigation example“>
<ul class=„pagination“>
{* Šipka vlevo. *}
<li n:class=„page-item, $page <= 1 ? disabled“>
<span><span aria-hidden=„true“>
{if $page > 1}
<a class=„page-link“ href=„{$url->setQueryParameter(‚page‘, $page – 1)->getAbsoluteUrl()}“>«</a>
{else}
«
{/if}
</span></span>
 </li>

{* Umístění jedničky. *}
<li class=„page-item“ n:if=„$left > 1“>
<span><a href=„{$url->setQueryParameter(‚page‘, 1)->getAbsoluteUrl()}“>1</a></span>
 </li>

{* Tečky vlevo. *}
<li n:if=„$left > 2“ class=„disabled“>
<span>…</span>
 </li>

{* Stránky v radiusu. }
<li n:for=„$i = $left; $i <= $right; $i++“ n:class=„page-item, $i == $page ? active“>
{if $i == $page} {
Aktivní stránka. *}
<span>{$i}<span class=„sr-only“>(current)</span></span>
{else}
<a class=„page-link“ href=„{$url->setQueryParameter(‚page‘, $i)->getAbsoluteUrl()}“>{$i}</a>
{/if}
 </li>

{* Tečky vpravo. *}
<li n:if=„$right < $pages – 1“ class=„disabled“>
<span>…</span>
 </li>

{* Umístění poslední stránky. *}
<li n:if=„$right < $pages“>
<span><a class=„page-link“ href=„{$url->setQueryParameter(‚page‘, $pages)->getAbsoluteUrl()}“>{$pages}</a></span>
 </li>

{* Šipka vpravo. *}
<li n:class=„page-item, $page >= $pages ? disabled“>
<span><span aria-hidden=„true“>
{if $page < $pages}
<a class=„page-link“ href=„{$url->setQueryParameter(‚page‘, $page + 1)->getAbsoluteUrl()}“>»</a>
{else}
»
{/if}
</span></span>
</li>
</ul>
</nav>`

a presenter úryvky:

<?php
use App\MagazineModule\Components\IPaginationControlFactory;

.
.
.
.
  public function __construct(ArticleManager $articleManager, IPaginationControlFactory $paginationControlFactory) {
        parent::__construct();

        $this->articleManager = $articleManager;
        $this->paginationControlFactory = $paginationControlFactory;
    }

public function renderList($category) {

        $articles = $this->articleManager->getArticles($category);

        // Nastavení stránkování.
        /** @var \Nette\Utils\Paginator $paginator */
        $paginator = $this['pagination']->getPaginator();
        $paginator->setItemCount(count($articles));
        $paginator->setPage($this->getParameter('page', 1));
        $articles->limit($paginator->getLength(), $paginator->getOffset());



        $this->template->articles = $articles;
        // $this->template->articles = $this->articleManager->getArticles($category);
    }
?>

no a nakonec vypisuju paginaci do sablony pres {control pagination}

errory : 1) nelze zavolat constructor – parent::__construct(); v paginationControl.php a kdyz ho z konstrukturu vyhodim…
[2020–01–31 10–41–35] Error: Cannot call constructor in C:\xampp\htdocs\sazky\app\MagazineModule\components\PaginationControl.php:33 @ http://localhost/…eznam-clanku?… @@ exception–2020–01–31–10–41–2ff41f493a.html
[2020–01–31 10–41–37] Error: Cannot call constructor in C:\xampp\htdocs\sazky\app\MagazineModule\components\PaginationControl.php:33 @ http://localhost/…eznam-clanku?… @@ exception–2020–01–31–10–41–2ff41f493a.html

  1. chyba po vyhozeni parent z konstruktoru: – coz mi nedava smysl...... Nette\Http\Url … na 3.0 , má normálně metodu setQeuryParameter()…

2020–01–31 10–38–26] Nette\MemberAccessException: Call to undefined method Nette\Http\UrlScript::setQueryParameter(), did you mean getQueryParameter()? in C:\xampp\htdocs\sazky\vendor\nette\utils\src\Utils\ObjectHelpers.php:65 @ http://localhost/…eznam-clanku?… @@ exception–2020–01–31–10–34–bdc91fa5da.html
[2020–01–31 10–38–36] Nette\MemberAccessException: Call to undefined method Nette\Http\UrlScript::setQueryParameter(), did you mean getQueryParameter()? in C:\xampp\htdocs\sazky\vendor\nette\utils\src\Utils\ObjectHelpers.php:65 @ http://localhost/…eznam-clanku?… @@ exception–2020–01–31–10–34–bdc91fa5da.html

David Matějka
Moderator | 6445
+
+1
-

ahoj, smazani toho parent::__construct() je spravny postup, v nette 3 zmizel konstruktor u rodice

a UrlScript je nove immutable, takze tam neni metoda setQueryParameter, ale withQuery. ale je lepsi, kdyz budes odkaz generovat pres {plink this page => $page + 1}

Newer
Člen | 47
+
0
-

Děkuju ti, moc jsi mi pomohl, upravil jsem to podle doporučení a už to šlape.

Jen nerozumím tomu proč nejde setQueryParameter, podle API 3.0, by ta metoda měla být obsažena v Nette\http\Url;
viz.: https://api.nette.org/…ttp/Url.html

David Matějka
Moderator | 6445
+
0
-

divas se na spatnou tridu, je to UrlScript, ktery dedi od UrlImmutable