ActiveScaffold 可以配置为在显示列表之前显示搜索表单吗?
当我要求 ActiveScaffold 显示一个很长的列表(例如销售的产品列表)时,它会运行数据库查询以获取第一页数据并显示它。 如果列表有一些关系,则此查询可能需要一些时间才能执行(超过一秒)。 大多数时候,我对这个“未过滤”的列表不感兴趣:我想做的第一件事就是单击“搜索”并过滤这个列表。
有什么方法可以告诉 ActiveScaffold 在调用列表操作时不显示未过滤的列表? 我希望它简单地显示搜索表单,等待输入一些条件,然后才显示过滤后的列表。
When I ask ActiveScaffold to show me a very long list (for example the list of products sold), it runs the database query to get the first page of data and it displays it. If the list has a few relations, this query might take some time to execute (over a second). Most of the time, I'm not interested in this "unfiltered" list: the first thing I want to do is click on "search" and filter down this list.
Is there any way I could tell ActiveScaffold not to display the unfiltered list when the list action is called? I would like it to simply display the search form, wait for some criteria to be entered, and only then display the filtered list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过在控制器中定义 conditions_for_collection 方法找到了解决方案。 这是一种 hack,但很简单(3 行)并且有效:
事情是这样的:当您请求列表时,将调用控制器的 list 方法,由 ActiveScaffold 处理。 ActiveScaffold调用conditions_for_collection,并且由于操作是list(而不是update_table),所以上面的conditions_for_collection方法返回“1=2”,这当然导致一个空列表。
用户可以单击“搜索”按钮,然后启动搜索。 这会调用 update_table 操作,ActiveScaffold 再次调用 conditions_for_collection,这次返回“”(无过滤器),因此会搜索整个列表。
它不是很漂亮,但它可以完成工作。
I found a solution by defining the conditions_for_collection method in the controller. It is kind of a hack, but it's simple (3 lines) and it works:
This is how it goes: when you ask for the list, the controller's list method is called, handled by ActiveScaffold. ActiveScaffold calls conditions_for_collection, and since the action is list (not update_table), the conditions_for_collection method above returns "1=2", which of course leads to an empty list.
The user can click on the "Search" button, and launch a search. This calls the update_table action, again ActiveScaffold calls conditions_for_collection, which this time returns "" (no filter), so the whole list is searched.
It's not really beautiful, but it does the job.
您可能需要覆盖
#index
操作并提供您自己的模板。 似乎没有任何方法可以告诉它不显示该列表。像这样的事情:
然后您必须实现自己的索引方法,该方法有自己的模板等。 将搜索操作交还给 Active Scaffold。 我不确定你会如何做到这一点,但如果你查看返回的原始页面的 HTML 源代码,应该非常清楚它链接到执行搜索的操作。
You probably need to override the
#index
action and provide your own template. It doesn't appear there's any way to tell it to not show that list.Something like this:
You'll have to then implement your own index method that has it's own template and so on. Hand off back to Active Scaffold's search action. I'm not sure how'd you'd do that, but if you look at the HTML source for the original page you get back it should be pretty clear what action it's linking to that does the search.