Nette Framework – VisualPaginator

Notice: This thread is very old.
Takeshi
Member | 596
+
0
-

Hi all, I have a little/big problem. I use a VisualPaginator so I have created a pagination (for example: Five movies per page) BUT!…I REALLY!!! dont know how to draw a steps/pages ..(prev1 2 3 4 5 6..11next) … so for example when I click on NEXT it will go on page+1. Can anybody help me please … thanks a lot

Last edited by Takeshi (2012-01-18 03:08)

Takeshi
Member | 596
+
0
-
<?php

   class MoviePresenter extends BasePresenter
   {
      public function actionIndex()
      {
         $this->template->movies = 'Here I will put some movies';
      }
      public function renderIndex()
      {
          $vp = new VisualPaginator($this, 'vp');
          $paginator = $vp->getPaginator();
          $paginator->itemsPerPage = 3;
          $paginator->itemCount = count(10);

          $this->template->rows = dibi::query('SELECT * FROM [movies] %ofs %lmt', $paginator->offset, $paginator->itemsPerPage);
      }
   }

?>

-------Template-------

{block content}
<div n:foreach="$rows as $movie">
   <table border="0">
      <tr><td width="30">
          {$movie->id}
      </td><td width="200">
          {$movie->name}
      </td></tr>
   </table>
<hr>
</div>
{/block}

Sorry. I hope now it is wrote correct :-)

Patrik Votoček
Member | 2221
+
0
-

Patrik Votoček wrote:

You must setup VisualPaginator with component factory and render in your template with {control coponentName}.

Takeshi
Member | 596
+
0
-

At first: sorry I am beginner :-)
So I tried to write … a component :…

<?php

   class MoviePresenter extends BasePresenter
   {
      public function actionIndex()
      {
         $this->template->movies = 'Here I will put some movies';
      }
      public function renderIndex()
      {
          $pagin = $this['pagin'];
      }
      protected function createComponentPagin()
      {
         $pagin = new VisualPaginator($this, 'pagin');
         $paginator = $pagin->getPaginator();
         $paginator->itemsPerPage = 3;
         $paginator->itemCount = count(10);
         $this->template->rows = dibi::query('SELECT * FROM [movies] %ofs %lmt', $paginator->offset, $paginator->itemsPerPage);
         return $pagin;
      }
   }

?>

… and in my template …

{block content}
<div n:foreach="$rows as $movie">
   <table border="0">
      <tr><td width="30">
          {$movie->id}
      </td><td width="200">
          {$movie->name}
      </td></tr>
   </table>
<hr>
</div>

{control pagin}
{/block}

… and it showed me just a database … no steps/pages <prev 1 2 3 4 .. 11 next>

… and I added a template.phtml (I downloaded it with VisualPaginator from Nette page)to NettePHP\libs\Nette\Utils/template.phtml

Last edited by Takeshi (2012-01-18 02:58)

duke
Member | 650
+
0
-
  1. Setting of template variables belongs into render method, not action method.
  2. The $pagin = $this['pagin'] in your render method is unnecessary.
  3. Your component factory method shouldn't mess with the template either. Leave that to render method.
  4. Expression count(10) returns 1, which is probably not what you've expected.

I suggest this:
In your component factory, simply create an instance of VisualPaginator and possibly set it the default itemsPerPage, but nothing more.
In your action/render methods access the database, retrieve the rows and item count, and set the itemCount of the paginator component like this: $this['pagin']->paginator->itemCount = $count. In the render method, set the $this->template->rows variable.

EDIT: Also, inside the factory method, you can instantiate the VisualPaginator simply like this:
$pagin = new VisualPaginator;
There is no need for the arguments.

Last edited by duke (2012-01-18 03:38)

Patrik Votoček
Member | 2221
+
0
-

OK so lets go step by step… :-)

first create VisualPaginator component

protected function createComponentPaginator()
{
	$vp = new VisualPaginator;
	$vp->getPaginator()->itemsPerPage = 3;
	return $vp;
}

Now set data to paginator…

public function actionIndex()
{
	$itemsCount = dibi::query('SELECT COUNT([id]) FROM [movies]')->fetchSingle();
	$this['paginator']->getPaginator()->itemCount = $itemsCount;
}

Now load paginated data…

public function renderIndex()
{
	$paginator = $this['paginator']->getPaginator();
	$movies = dibi::query('SELECT * FROM [movies] %ofs %lmt', $paginator->offset, $paginator->itemsPerPage);
	$this->template->movies = $movies;
}

And template

{block content}
<div n:foreach="$movies as $movie">
   <table border="0">
      <tr><td width="30">
          {$movie->id}
      </td><td width="200">
          {$movie->name}
      </td></tr>
   </table>
<hr>
</div>

{control paginator}

I don't test it! Just think! :-) If doesn't work tell me.

Takeshi
Member | 596
+
0
-

Thanks a lot. So … by following steps in your first message I created that steps (prev1 2 3 4next) but it was almost done … but i needed to change 2–3 rows (from your second message) and ...... now it is perfect … tanks :-)