Wednesday, December 3, 2008

Re: Pagination & Filtering Conditions

I post here the way I solved in case someone found it useful as well.
I was reading several of the solutions available on the group but none
of them was working 100% for me, so I tried to figure it out the best
I could:

in the controller:

class ExpensesController extends AppController
{
...
var $paginate = array(
'limit' => 15,
'order' => array(
'Expense.stamp' => 'asc'
)
);
...
function index ()
{
//set current month and year by default
$month = date ("m");
$year = date ("Y");

//do we receive something from the form?
if (
!empty ($this->params['data']['filter']['month']['month'])
&&
!empty ($this->params['data']['filter']['year']['year']))
{
// yes, we forget about paging stuff
$month = $this->params['data']['filter']['month']
['month'];
$year = $this->params['data']['filter']['year']
['year'];
}
else
{
//no, then maybe we are paging
if (
!empty ($this->params ['named']['filter.month']) &&
!empty ($this->params ['named']['filter.year']))
{
$month = $this->params ['named']['filter.month'];
$year = $this->params ['named']
['filter.year'];
}
}

$this->params['pass']['filter.month'] = $month;
$this->params['pass']['filter.year'] = $year;

$data = $this->paginate('Expense', "Expense.stamp LIKE '%$year-
$month%'");// AND Expense.stamp LIKE '%-$month-%'");
$this->set(compact('data'));
}
...
}

in the view:
<!-- this are the filtering combos -->
<?= $form->create('Expense', array('type' => 'post', 'action' =>
'index')); ?>
<?= $form->month ("filter.month", $this->params ["pass"]
["filter.month"], array (), false); ?>
<?= $form->year ("filter.year", 2000, 2020, $this->params ["pass"]
["filter.year"], array (), false); ?>
<?= $form->end('View'); ?>
<!-- iterate the data ... -->
...
<!-- ... and show the "go to page 1 | 2 | 3" -->
<?php unset($this->params['pass']['page']); //read it somewhere in
this group, it was useful ?>
Go to Page: <?= $paginator->numbers(array('url' => $this->params
['pass'])); //it's important to pass this params.pass?>

If you see something I'm doing badly wrong or unnecesary please
advise. thanks

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: