pagination

This commit is contained in:
Etienne Roudeix
2013-06-26 16:09:46 +02:00
parent 80380878f5
commit 3aca4bfb25
3 changed files with 48 additions and 12 deletions

View File

@@ -45,11 +45,16 @@ abstract class BaseLoop
*/ */
public $dispatcher; public $dispatcher;
public $limit;
public $page;
public $offset;
protected function getDefaultArgs() protected function getDefaultArgs()
{ {
return array( return array(
Argument::createIntTypeArgument('offset'), Argument::createIntTypeArgument('offset', 0),
Argument::createIntTypeArgument('limit'), Argument::createIntTypeArgument('page'),
Argument::createIntTypeArgument('limit', 10),
); );
} }
@@ -68,6 +73,30 @@ abstract class BaseLoop
return $this->defineArgs()->addArguments($this->getDefaultArgs()); return $this->defineArgs()->addArguments($this->getDefaultArgs());
} }
public function search(\ModelCriteria $search)
{
if($this->page !== null) {
return $this->searchWithPagination($search);
} else {
return $this->searchWithOffset($search);
}
}
public function searchWithOffset(\ModelCriteria $search)
{
if($this->limit >= 0) {
$search->limit($this->limit);
}
$search->offset($this->offset);
return $search->find();
}
public function searchWithPagination(\ModelCriteria $search)
{
return $search->paginate($this->page, $this->limit);
}
/** /**
* *
* this function have to be implement in your own loop class. * this function have to be implement in your own loop class.

View File

@@ -51,8 +51,6 @@ use Thelia\Type;
* * by default results are sorting by position ascending * * by default results are sorting by position ascending
* - random : if 1, random results. Default value is 0 * - random : if 1, random results. Default value is 0
* - exclude : all category id you want to exclude (as for id, an integer or a "string list" can be used) * - exclude : all category id you want to exclude (as for id, an integer or a "string list" can be used)
* - limit : number of results. Default value is 10
* - offset : at witch id start the search
* *
* example : * example :
* *
@@ -76,8 +74,6 @@ class Category extends BaseLoop {
public $order; public $order;
public $random; public $random;
public $exclude; public $exclude;
public $limit;
public $offset;
protected function defineArgs() protected function defineArgs()
{ {
@@ -130,11 +126,7 @@ class Category extends BaseLoop {
$search->filterByLink($this->link); $search->filterByLink($this->link);
} }
/*if($this->limit > -1) {
$search->limit($this->limit);
}*/
$search->filterByVisible($this->visible); $search->filterByVisible($this->visible);
//$search->offset($this->offset);
switch($this->order) { switch($this->order) {
@@ -164,8 +156,7 @@ class Category extends BaseLoop {
*/ */
$search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN); $search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN);
//$categories = $search->find(); $categories = $this->search($search);
$categories = $search->paginate($page = 2, $maxPerPage = 2);
$loopResult = new LoopResult(); $loopResult = new LoopResult();

View File

@@ -84,4 +84,20 @@ An image from asset directory :
{/loop} {/loop}
</div> </div>
<div>
<p>Some pagination</p>
<p>PAGE 1</p>
<ul>
{loop type="category" name="catloopwithpagination" limit="2" page="1"}
<li>{$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}</li>
{/loop}
</ul>
<p>PAGE 2</p>
<ul>
{loop type="category" name="catloopwithpagination" limit="2" page="2"}
<li>{$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}</li>
{/loop}
</ul>
</div>
{include file="includes/footer.html"} {include file="includes/footer.html"}