使用ajax观察域过滤数据
我尝试使用带有观察字段和ajax分页的下拉菜单来实现过滤数据(基于所选类别的列表)。我使用会话来记住选定的类别以保持分页。
这是我的代码(Cakephp 1.2)
视图:
echo $form->select('Category.id', $categories, null, array('id' => 'categories'),'Filter by Categories')
echo $ajax->observeField('categories' ,array('url' =>'update_category','update' => 'collectionDiv'));
在控制器中:
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'));
}
过滤器按我想要的方式工作,但问题是当我选择空值(“按类别过滤”)时,它仍然记得最后一个类别会话,所以我无法回到默认值列表(不带过滤器的所有记录列表)。
我试图提出一些条件,但仍然没有成功。还有别的办法吗?请感谢您的帮助。感谢
赫马万
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我不明白这个问题,但在我看来,它可能值得更改:
to:
实际上,您的代码似乎无论如何都在这样做。返回后,检查浏览器窗口顶部的 URL 是什么。它是否仍然包含先前查询中的分页指令?
您可能需要查看 http://book.cakephp.org/view/167/AJAX-分页并确保您已“勾选所有复选框”。
Perhaps I don't understand the question, but it looks to me like it might be worth changing:
to:
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'.
我明白了,现在它按我希望的方式工作。我自己解释一下情况和解决方案。
当我从组合框中选择类别时,它会呈现页面,代码是:
从这种情况来看,我认为observefield不会发送passArg,因为我们从ajax链接或html->链接等url获取。我希望这对任何人都有用
I got it, It work as I hope now. I my self explain the condition and solution.
When I select category from combobox, then it render the page the code is :
From this case, I think that observefield will not send passedArg as we get from url such from ajax link or html->link. I hope this will usefull to anybody