简体   繁体   English

4下拉菜单,根据选择更新每个菜单,然后再搜索

[英]4 Drop down menus, update each one based on the selection before to SEARCH

I am creating a cinema application in Ruby on Rails and am currently working on the bookings. 我正在使用Ruby on Rails创建一个影院应用程序,目前正在进行预订。

What I am trying to do is implement a search so that a user can search for a free seat, select that seat, and then book that seat for a film's showing. 我想要做的是实施搜索,以便用户可以搜索免费座位,选择该座位,然后预订该座位以进行电影放映。

To do this, I want to implement 4 drop down menus: films, showings, screens and seats. 为此,我想实现4个下拉菜单:电影,放映,屏幕和座位。 But the difficulty is that what I want is the drop down menus to be dynamic and update themselves based on the previous selection, so when a user selects a film, the showing drop down only shows showings for that film, when the showing is selected the screen drop down only shows the screen that showing is in, and then the screen drop down menu is used so that the seats drop down only shows seats that are in that screen. 但困难在于我想要的是下拉菜单是动态的并且根据之前的选择更新自己,所以当用户选择一部电影时,显示下拉列表仅显示该电影的放映,当选择显示时屏幕下拉菜单仅显示显示的屏幕,然后使用屏幕下拉菜单,以便座位下拉菜单仅显示该屏幕中的座位。

I have so far enabled the user to search for a screen and then when they click search the seats available are shown on the seats/index.html.erb. 到目前为止,我已启用用户搜索屏幕,然后当他们点击搜索时,座位/ index.html.erb上会显示可用的座位。

This was done using the partial _booking_lookup.html.erb: 这是使用partial _booking_lookup.html.erb完成的:

<%= form_tag my_path, :method=>'post', :multipart => true do %>

<%= select_tag ('screen_id'), 
    options_from_collection_for_select(@screens, :id, :screens_info, 0 ),
    :prompt => "Screen" %> 


<%= submit_tag 'Search' %>
<% end %>

Which is displayed on the layouts/application.html.erb: 哪个显示在layouts / application.html.erb上:

<%= render(:partial => '/booking_lookup', :locals=> {:film => @films = Film.all, :showings => @showings = Showing.all, :screens => @screens = Screen.all, :seats => @seats = Seat.all, :my_path => '/seats/display_seats_by_screen' }) %>

And links to the display_seats_by_screen in the seats_controller: 并链接到seat_controller中的display_seats_by_screen

    def display_seats_by_screen
    @seats = Seat.screen_search(params[:screen_id])
    if @seats.empty?
        # assign a warning message to the flash hash to be displayed in
        # the div "feedback-top"
        flash.now[:alert] = "There are no films of that genre."
        # return all products, in alphabetical order
        @seats = Seat.all
    end
    render :action => "index"
end

Which uses the screen_search in the seat.rb model: 它采用screen_search在seat.rb模型:

def self.screen_search(search_string)
    self.where("screen_id = ?", search_string)
end

So essentially for the above before the user selects the screen they should have selected a showing and before that a film so that only screens for that showing are shown so they can select an appropriate seat. 因此,在用户选择屏幕之前,基本上对于上述内容,他们应该选择一个显示,然后选择一个电影,以便仅显示该显示的屏幕,以便他们可以选择合适的座位。

The models associations: film: 模特协会:电影:

has_many :showings
belongs_to :certificate
belongs_to :category

showing: 显示:

belongs_to :film
has_many :bookings
belongs_to :screen

screen: 屏幕:

has_many :seats

seat: 座位:

belongs_to :screen
has_many :bookings

Can anyone help? 有人可以帮忙吗?

Here's an example jsfiddle : 这是jsfiddle的一个例子:

It works on the first one, It's not dynamic though. 它适用于第一个,但它不是动态的。 html: HTML:

<p>films</p>
<div data-film1='["showing1_value","showing2_value"]' data-film2='["showing3_value","showing4_value"]'> 
    <select name="user[films]" id="films" class="select"><option value=""></option>
        <option value="film1">transformers</option>
        <option value="film2">the hangover</option>
    </select>
</div>   
<p>showing</p>    
<div data-showing1='["screen1_value","screen2_value"]' data-showing2='["screen3_value","screen4_value"]'> 
    <select name="user[showing]" id="showing" class="select">
        <option value=""></option>
        <option value="showing1_value">showing1</option>
        <option value="showing2_value">showing2</option> 
        <option value="showing3_value">showing3</option> 
        <option value="showing4_value">showing4</option> 
</select>
</div>

js: JS:

 $('#films').change(function(){

     if ($("#films").val() == "film1"){
         //you can get this films shows:
         alert (   $("#films").parent().data("film1")   );
         //now you need to ajust the showing dropdown according: something like this:
         $("#showing").html('<option value=""></option>');

         $.each( $("#films").parent().data("film1"), function( index, show ){
            $("#showing").append('<option value="' + show + '">' + show + '</option>');
         });
     }    
});

Since the js only apply to this current page, I wouldn't put it in application.js (where it would be includes on every page), I would put it on the relevant page- since it only apply that page. 由于js仅适用于当前页面,我不会将它放在application.js(它将包含在每个页面上),我会将它放在相关页面上 - 因为它只应用该页面。

On the server side you would need to set the data attributes of course. 在服务器端,您需要设置数据属性。

What I did was put each film's showings, as data attributes , and then when the user, selected that film, I used that film's attributes- which contains that film's relevant showings, to create the next dropdown, as seen here 我所做的就是把每部电影的放映,作为数据属性 ,当用户选择的那部电影,我用那部电影的属性-它包含了影片的相关看房,以创建下一个下拉列表,然后,所看到这里

Now for the rails part: 现在为rails部分:

In this page controller action, You will have something like this: 在此页面控制器操作中,您将具有以下内容:

#now it doesn't matter how you do it, you can choose your own way
#but eventually you need like seen here, only that it includes all the films,
#and for each film all his showings.

@films = Film.all

@film_data_attributes_string = ""

@films.each do |film|
  film_str = "data-#{film.title}="                   #this should give: data-tranformers=
  part_two_1 = "'[\""                                  #this should give '[\"


  film_showings_array = film.showings                          #this will give the film's showins. since film has_many :showings, 
  showings_array_names = film_showings_array.map(&:name)       #this will give an array of the showing's titles
  part_two_2 = showings_array_names.join('","')                #this should give a string:    showing3_value","showing4_value




  part_two_3 = "\"]'"                                  #this should give "]'

  @film_data_attributes_string << film_str + part_two_1 + part_two_2 + part_two_3 + " "

  #all together:          data-tranformers='["showing1_value","showing2_value"]' 
  #notice there is a space in the end
end

this will give you (more or less) the data attributes of the films . 这将给你(或多或少) 电影数据属性 It's not tested, but that's the main idea. 它没有经过测试,但这是主要的想法。 assuming both your films and showings have an attributes called "name". 假设你的电影和放映都有一个名为“名字”的属性。 (of course it would be better to move all that functionality to the film model and not use that in controller) (当然最好将所有功能都移到电影模型而不是在控制器中使用它)

Then in the view, you will use @film_data_attributes_string of course, maybe you will need to use html escape or something, not sure. 然后在视图中,你将使用@film_data_attributes_string,当然,也许你需要使用html转义或其他东西,不确定。

Or you can use this answer 或者你可以使用这个答案

and use this instead: 并使用此代替:

@data_hash = {}
@films.each do |film|
  @data_hash["data-#{film.title}"] = film.showings.map(&:name).to_json
end

You will need to do something similar for the java script. 您需要为java脚本执行类似的操作。

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

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