使用ajax观察域过滤数据

发布于 2024-09-30 09:36:19 字数 1051 浏览 3 评论 0原文

我尝试使用带有观察字段和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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小忆控 2024-10-07 09:36:19

也许我不明白这个问题,但在我看来,它可能值得更改:

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()));
}

实际上,您的代码似乎无论如何都在这样做。返回后,检查浏览器窗口顶部的 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:

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'.

寄离 2024-10-07 09:36:19

我明白了,现在它按我希望的方式工作。我自己解释一下情况和解决方案。
当我从组合框中选择类别时,它会呈现页面,代码是:

If ($this->data['Category']['id']) {
  $cat_id=$this->data['Category']['id'];
  $this->Session->write($this->name.'.category_id', $category_id);
  // I will use this session next when I click page number
  $filters=array('Collection.art_type_id' => $category_id); 
  $this->set('collections', $this->paginate('Collection', $filters));

}else{
//if clik page number, next or whatever, $this->data['Category']['id'] will empty so 
// use session to remember the category so the page will display next page or prev 
// page with the proper category, But the problem is, when I set category fillter to
// "All Category" it will dislpay last category taked from the session. To prevent it 
// I used $this->passedArgs['page']. When I clik the page paginator it will return 
// current number page, but it will be empty if we click dropdown. 

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

从这种情况来看,我认为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 :

If ($this->data['Category']['id']) {
  $cat_id=$this->data['Category']['id'];
  $this->Session->write($this->name.'.category_id', $category_id);
  // I will use this session next when I click page number
  $filters=array('Collection.art_type_id' => $category_id); 
  $this->set('collections', $this->paginate('Collection', $filters));

}else{
//if clik page number, next or whatever, $this->data['Category']['id'] will empty so 
// use session to remember the category so the page will display next page or prev 
// page with the proper category, But the problem is, when I set category fillter to
// "All Category" it will dislpay last category taked from the session. To prevent it 
// I used $this->passedArgs['page']. When I clik the page paginator it will return 
// current number page, but it will be empty if we click dropdown. 

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文