Converting a select form to a dropdown buttons

Terrytreks

UPDATE: I'm almost there! See my partial answer at the end of this question.

I've got a working form with two select dropdowns, the first for countries, the second for cities. When you select a country, an ajax request automagically gets the countries and updates the countries box.

The problem is the select dropdowns are ugly, and I can't really style them. What I want is a dropdown button, which I can use Bootstrap on to make pretty: Bootstrap Button

I don't have to change the jquery or controller logic, I just have to update my view. But I don't even know where to start. This is what I have:

<%= form_for :trip, url: {action: "index"}, html: {method: "get"} do |f| %>
  <%= f.select :country_obj_id, options_for_select(@countries.collect { |country|
    [country.name.titleize, country.id] }, @countries.first.name), {}, { id: 'countries_select' } %>
  <%= f.select :city_obj_id, options_for_select(@cities.collect { |city|
    [city.name.titleize, city.id] }, 0), {}, { id: 'cities_select' } %>
  <%= f.submit "Go!" %>
<% end %>

How can I convert the selects into dropdown buttons?


UPDATE: I can now get a response back. I just don't know how to handle it to update the button.

The view has two dropdown buttons. The first is the countries, the second is the cities for the first country. I want to update the list of cities for the country that is selected:

<div class="dropdown" style="display:inline-block;">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select A Country
  <span class="caret"></span></button>
  <ul class="dropdown-menu">
      <% @countries.each do |country| %>
        <%= content_tag(:li, link_to(country.name.titleize, update_cities_path(country_obj_id: country.id), remote: :true)) %>
      <% end %>
  </ul>
</div>
<div class="dropdown" style="display:inline-block;">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select A City
  <span class="caret"></span></button>
  <ul class="dropdown-menu">
      <% @cities.each do |city| %>
        <%= content_tag(:li, city.name.titleize) %>
      <% end %>
  </ul>
</div>

Selecting a country goes into the update_cities function, which gets the cities for the country_obj_id that is passed.

def update_cities
  @cities = CityObj.where("country_obj_id = ?", 
                           params[:country_obj_id]).order(:name)
  respond_to do |format|
    format.js
  end
end

Then the part that i don't understand. The format.js takes me to update_cities.js.coffee, which I have not changed from my original version that worked on the select dropdown:

$("#cities_select").empty()
  .append("<%= escape_javascript(render(:partial => @cities)) %>")

That ends up updating my old select dropdown, which I left in the view for now. But how can I modify the js to update my new dropdown button?

Terrytreks

Got it. This is the coolest thing ever. It starts out with two buttons. The first says "Select A Country," and the second says "Select A City." The second button is not active to start with, and I'll probably hide it.

The view:

<div class="dropdown" style="display:inline-block;">
  <button id="country-select-btn" class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select A Country
  <span class="caret"></span></button>
  <ul class="dropdown-menu">
      <% @countries.each do |country| %>
        <%= content_tag(:li, link_to(country.name.titleize, update_cities_path(country_obj_id: country.id), remote: :true)) %>
      <% end %>
  </ul>
</div>
<div class="dropdown" style="display:inline-block;">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select A City
  <span class="caret"></span></button>
  <ul id="cities_select" class="dropdown-menu">
  </ul>
</div>

The dropdown items are just links, with a route set so clicking a link goes to the controller update_cities action with a country passed:

def update_cities
  @cities = CityObj.where("country_obj_id = ?", 
                           params[:country_obj_id]).order(:name)
  @country = CountryObj.find_by(id: params[:country_obj_id])

  respond_to do |format|
    format.js
  end
end

The format.js goes into update_cities.js

$("#cities_select").empty()
  .append("<%= escape_javascript(render(:partial => @cities)) %>")

$("#country-select-btn").empty()
  .append("<%= escape_javascript(render(:partial => @country )) %>")

That renders two partials. _city_obj.html.erb renders a link for each of the cities, which will go to the controller's index action when clicked:

<li><%= link_to(city_obj.name.titleize, index_path(:trip => {country_obj_id: city_obj.country_obj.id, city_obj_id: city_obj.id}), id: "cities_select") %></li>

The _country_obj.html partial just updates the "Select A Country" label on the button to the name of the selected country:

<%= country_obj.name.titleize %>

Now, when you click one of the city links, the controller index action can find all the guides for that city/country, then the view will render it.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Django - dropdown form with multiple select

分類Dev

multiple buttons in FORM

分類Dev

converting insert into to select

分類Dev

Style Page Buttons Differently than Select Buttons

分類Dev

Select dropdown color changing

分類Dev

Angular multi select dropdown

分類Dev

Converting curl with --form to python requests

分類Dev

Dynamic amount of submit buttons in a form

分類Dev

Populate Text Input With Form Buttons

分類Dev

Dropdown lists after radio buttons are selected

分類Dev

How to convert a select dropdown into an ul dropdown?

分類Dev

Python mechanize form dropdown error

分類Dev

Responsive Website Dropdown form issue

分類Dev

Blazor onchange event with select dropdown

分類Dev

Disabling option in a select dropdown with jQuery

分類Dev

select randomly from dropdown list?

分類Dev

select randomly from dropdown list?

分類Dev

Adding images to Vue Select dropdown?

分類Dev

select elements in dropdown marked as a list

分類Dev

Converting serialized form's data to json object

分類Dev

Converting an equation to another form using sympy

分類Dev

Converting from different arrays to standard form

分類Dev

How to reset form radio buttons to unchecked in reactjs?

分類Dev

How to keep radio buttons on same line in form

分類Dev

Is there shortcut to affect several buttons appearing on form?

分類Dev

How to create form when second dropdown depends on the first dropdown selection

分類Dev

How can I display select options as buttons?

分類Dev

Filter by pills/buttons instead of using a select - angular

分類Dev

How to submit form on change of dropdown list?

Related 関連記事

ホットタグ

アーカイブ