pagination con symfony 1.4 e doctrine

pagination con symfony 1.4 e doctrine

This quick tutorial explain how it’s simple to set pagination on module view using symfony

In apps/myapp/modules/mymodule/actions/actions.class.php insert:

<pre>public function executeIndex(sfWebRequest $request)
 {
 $this->pager = new sfDoctrinePager('TableName', '5');

 $this->pager->setQuery(Doctrine::getTable('TableName')->createQuery('a'));
//set first page as default (get parameter page)
 $this->pager->setPage($request->getParameter('page', 1));
 $this->pager->init();
 }
In apps/myapp/modules/mymodule/templates/indexSuccess.php
<?php foreach ($pager->getResults() as $customer): ?>
<tr>
 <td><?php echo $customer->getId() ?></td>
 <td><?php echo $customer->getName() ?></td>
 <td><?php echo $customer->getSurname() ?></td>
</tr>
<?php endforeach; ?>

<?php // check if results > 5 ?>
<?php if ($pager->haveToPaginate()): ?>
<ul id="pagination">
<?php $links = $pager->getLinks(); foreach ($links as $page): ?>
<li>
<?php echo ($page == $pager->getPage()) ? $page : link_to($page, 'mymodule/index?page='.$page) ?>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>

You can also use this two strings to get first and last elements of your pagination:

<?php //for the first page ?>
<?php echo link_to('first', 'module/index?page='.$pager->getFirstPage()) ?>

<?php //for the last page ?>
<?php echo link_to('last', 'module/index?page='.$pager->getLastPage()) ?>

This quick tutorial explain how it’s simple to set pagination on module view using symfony

In apps/myapp/modules/mymodule/actions/actions.class.php insert:

<pre>public function executeIndex(sfWebRequest $request)
 {
 $this->pager = new sfDoctrinePager('TableName', '5');

 $this->pager->setQuery(Doctrine::getTable('TableName')->createQuery('a'));
//set first page as default (get parameter page)
 $this->pager->setPage($request->getParameter('page', 1));
 $this->pager->init();
 }
In apps/myapp/modules/mymodule/templates/indexSuccess.php
<?php foreach ($pager->getResults() as $customer): ?>
<tr>
 <td><?php echo $customer->getId() ?></td>
 <td><?php echo $customer->getName() ?></td>
 <td><?php echo $customer->getSurname() ?></td>
</tr>
<?php endforeach; ?>

<?php // check if results > 5 ?>
<?php if ($pager->haveToPaginate()): ?>
<ul id="pagination">
<?php $links = $pager->getLinks(); foreach ($links as $page): ?>
<li>
<?php echo ($page == $pager->getPage()) ? $page : link_to($page, 'mymodule/index?page='.$page) ?>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>

You can also use this two strings to get first and last elements of your pagination:

<?php //for the first page ?>
<?php echo link_to('first', 'module/index?page='.$pager->getFirstPage()) ?>

<?php //for the last page ?>
<?php echo link_to('last', 'module/index?page='.$pager->getLastPage()) ?>

Post Correlati

  • Nessun post correlato trovato.

Commenti

Lascia un Commento

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *

*

È possibile utilizzare questi tag ed attributi XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>