views:

47

answers:

2

Hi all,

I ve tried to implemented filtering data (list base on selected category) using dropdown with observefield and ajax pagination. I use session to remember selected category in order to keep pagination.

Here is my code (Cakephp 1.2)

In view :

echo $form->select('Category.id', $categories, null, array('id' => 'categories'),'Filter by Categories')
echo $ajax->observeField('categories' ,array('url' =>'update_category','update' => 'collectionDiv'));

In Controller:

if(!empty($this->data['Category']['id'])) 
{ 
    $cat_id=$this->data['Category']['id'];
    $filters=array('Collection.category_id' => $cat_id); 
    $this->set('collections', $this->paginate('Collection', $filters));
    $this->Session->write($this->name.'.$cat_id', $category_id);
}
else
{
    $cat_id=$this->Session->read($this->name.'.cat_id');
    $filters=array('Collection.category_id' => $cat_id);
    $this->set('collections', $this->paginate('Collection'));
}

The filter work as I wanted but the problem is when I select empty value('Filter by Category) it still remember last category session so I can't back to the default list (All record list without filter).

I've tried to make some condition but still not success. Is there another way? Please I appreciate your help. thank

hermawan

A: 

Perhaps I don't understand the question, but it looks to me like it might be worth changing:

else
{
    $cat_id=$this->Session->read($this->name.'.cat_id');
    $filters=array('Collection.category_id' => $cat_id);
    $this->set('collections', $this->paginate('Collection'));
}

to:

else
{
    $this->set('collections', $this->paginate('Collection',array()));
}

In effect your code appears to be doing this anyway. Check what the URL is at the top of the browser window after it has returned. Does it still contain pagination directives from the previous query?

You might want to review http://book.cakephp.org/view/167/AJAX-Pagination and make sure you've 'ticked all the boxes'.

Leo
thank for answer. I ve tried your advise but still don't work. Lets say I pick up 'Painting Category', then the DIV change the content as I want, but if click the page number the DIV will content whole data not only 'Painting Category'.
hermawan
It sounds to me as if the ajax pagination isn't set up correctly, although I'm beginning to think that it may be a limitation of pagination. I recall having a similar problem when I altered the original query like this - it doesn't follow through on subsequent pages. I solved it by writing a custom paginate method on the appropriate model. Pagination is good at taking a query, paginating and column sorting the results. Beyond this it needs help.
Leo