简体   繁体   English

Rails中带有搜索按钮的下拉列表

[英]Drop Down List With Search Button In Rails

I have a customer model with the following collection: 我有一个具有以下集合的客户模型:

@doctors =  Customer.joins(:user).where(users: {role: User.roles[:doctor]}) 

which joins the user table and customer tables where user role is "doctor". 它将用户角色为“医生”的用户表和客户表连接起来。

In my Customers index view currently I am listing all the customers, now I want to add a search filter using a Drop down list with a search button, where the list contains the first name of the Doctors. 当前,在“客户索引”视图中,我正在列出所有客户,现在,我想使用带有搜索按钮的下拉列表添加搜索过滤器,该列表包含Doctors的名字。 If I press the search button it should show only customers created by that particular doctor selected from the drop down list. 如果我按下搜索按钮,它应该只显示由下拉列表中选择的特定医生创建的客户。

1. How to add a drop down list with search button which displays only first names of doctors ? 1.如何通过搜索按钮添加仅显示医生名字的下拉列表?


2. How to filter my customers listing when I select a particular doctor from the drop down and click on search button ? 2.当我从下拉列表中选择特定医生并单击搜索按钮时,如何过滤我的客户清单?


 <div class="btn-group"> <button type="button" class="btn btn-success btn-sm"><b>Search By Dr.</b></button> <button type="button" class="btn btn-success btn-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <span class="caret"></span> <span class="sr-only">Toggle Dropdown</span> </button> <ul class="dropdown-menu"> <li><a href="#">List All Customers</a></li> <% @practices.each do |practice| %> <li><a href="#">Dr.<%= practice.firstname %></a></li> <% end %> </ul> </div> 

The above is the code that I am currently using, but is it correct way of Rails ? 上面是我当前正在使用的代码,但这是Rails的正确方法吗?

Answer 1: 答案1:

you can do this task using JS I will recommend you to use one of these jquery plugins for search bars Select2 and Chosen . 您可以使用JS来完成此任务。我建议您对搜索栏Select2Chosen使用以下jquery插件之一。

My personal experience with select2 is impressive. 我对select2的个人经历令人印象深刻。

it loads data via ajax and cache as well to avoid repeat calls. 它也通过ajax和缓存加载数据,以避免重复调用。 Its GUI give ua Google like feel. 它的GUI给ua谷歌一样的感觉。 read its documentation and use it. 阅读其文档并使用它。

Answer 2: 答案2:

select2 also give u events like change so u can make another ajax call to load filterd customers. select2还为您提供了诸如change之类的事件,以便您可以再次调用ajax来加载已过滤的客户。

  1. You can simply use the following tag in your rails view: 您可以在rails视图中简单地使用以下标记:

    <%= select("doctor", "firstname", @doctors.all.collect(&:firstname)) %>

  2. Then in your controller you'd have something like this: 然后在您的控制器中,您将得到以下内容:

    def index if params[:doctor] @customers = Customer.joins(:user).where(users: {role: User.roles[:doctor]}, firstname: params[:doctor][:firstname].downcase) else @customers = Customer.all end end

You can do this auto_complete rails. 您可以执行此auto_complete导轨。 Have a look on http://railscasts.com/episodes/102-auto-complete-association 看看http://railscasts.com/episodes/102-auto-complete-association

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM