Rails Ajax Select 二级联动下拉菜单

Route

match "dungeons/load_dungeons/:id" => "dungeons#load_dungeons", via: [:get]
match "dungeons/load_dungeons" => "dungeons#load_dungeons", via: [:get]
match "dungeons/roll_test/:id" => "dungeons#roll_test", via: [:get, :post]
match "dungeons/roll_test" => "dungeons#roll_test", via: [:get, :post]

Controller

def index
  @dungeon_categories = DungeonCategory.all
end

def load_dungeons
  dungeon_category_id = params[:dungeon_category_id].to_i
  @dungeons = Dungeon.where(dungeon_category_id: dungeon_category_id)
  if request.xhr?
    render :json => {
      :file_content => @dungeons
    }
  end
end

def roll_tesst
  dungeon_id = params[:dungeon_id].to_i
  # ...
end

View

index.html.erb

<%= form_tag( dungeons_roll_test_path ) do %>
  <% arr_ca = @dungeon_categories.collect { |n| ["#{n.id}: #{n.name}", n.id] } %>
  <%= select_tag 'dungeon_category_id', options_for_select(arr_ca, params[:dungeon_category_id]), {:onchange => 'load_dungeons()'} %>
  <select id="dungeon_id" name="dungeon_id"></select>
  <%= submit_tag 'Run', confirm: 'Are you sure?' %>
<% end %>


  $(document).ready(function(){
    load_dungeons();
  });

  function load_dungeons() {
    $.ajax({
      type: "GET",
      url: "",
      data: {dungeon_category_id: $("#dungeon_category_id").val()},
      success: function(data, textStatus, xhr) {
        $("#dungeon_id").empty();
        for (var dungeon in data.file_content) {
          $("#dungeon_id").append(''+data.file_content[dungeon].id+':'+data.file_content[dungeon].dungeon_name+'');
          
            if(data.file_content[dungeon].id == ){
              $("#dungeon_id option[value='"+data.file_content[dungeon].id+"']").attr("selected", "selected");
            }
          
        }
      }
    });
  }

Advertisements

Rails Group Select Option

Model

# app/models/category.rb
class Category < ApplicationModel                                                                                                                                                                              
  has_many :category_details
end

# app/models/category_detail.rb
class CategoryDetail < ApplicationModel                                                                                                                                                                        
  belongs_to :category
end

View

<%= grouped_collection_select(:category_detail_id, :name, Category.all(:conditions => {:language_id => @language_id}), :category_details, :name, :id, :name, :include_blank =>'Please select' ) %>
or
<%= select_tag :category_detail_id, option_groups_from_collection_for_select(Category.all(:conditions => {:language_id => @language_id}),:category_details,:name,:id,:name,category) %>

Controller

@categories = Category.all(:conditions => {:language_id => @language_id}) 
@category_details = Category.includes(:category_details).where('language_id' => @language_id)

结果集上下位置交换

Controller

  def up
    News.shard(:shard_master) do
      news = News.find_by_disp_priority(params[:disp_priority])
      up_news = News.find(:all, order: 'disp_priority DESC')
      up_news.each_with_index do |up, index|
        if index == params[:index].to_i - 1
          news.disp_priority = up.disp_priority
          up.update_attributes(:disp_priority => params[:disp_priority])
        end
      end
      news.save!

      @news = News.find(:all, order: 'disp_priority DESC')
      @count = @news.count
    end

    redirect_to :action => 'index'
  end

  def down
    News.shard(:shard_master) do
      news = News.find_by_disp_priority(params[:disp_priority])
      down_news = News.find(:all, order: 'disp_priority DESC')
      down_news.each_with_index do |down, index|
        if index == params[:index].to_i + 1
          news.disp_priority = down.disp_priority
          down.update_attributes(:disp_priority => params[:disp_priority])
        end
      end
      news.save!

      @news = News.find(:all, order: 'disp_priority DESC')
      @count = @news.count
    end

    redirect_to :action => 'index'
  end