weixin_33708432 2015-07-28 14:15 采纳率: 0%
浏览 7

动态菜单,Ajax,Ruby

Hey folks a nooby noob here,

My application has Dynamic Drop down menus for Category and Subcategory. When a category is selected the corresponding subcategory will change and display the category's subcategories - I have that set. But what I would like to accomplish is to hide the subcategory menu until a category is selected.

My Code i'm using for menus:

StaticPage Controller

          def update_subcategories
            # updates categories and subcategories based on product type selected
            category = Category.find(params[:category_id])
            # map to name and id for use in our options_for_select
            @subcategories = category.subcategories.map{|a| [a.name, a.id]}.insert(0, "Select a Product")
          end

View staticpage>home

    <div class="form-group">
      <%= f.label 'Choose a Product:' %>
      <%= f.collection_select :category_id,  @categories,  :id, :name, {:prompt   => "Select a Category"}, {:id => 'categories_select'} %>
    </div>
    <div class="form-group">
      <%= f.label 'Choose a brand:' %>
      <%= f.collection_select :subcategory_id, @subcategories, :id, :name, {:prompt   => "Select an Product"}, {:id => 'subcategories_select'} %>
    </div>


<script>
  $(document).ready(function() {
    var categories = '#categories_select';
    var subcategories = '#subcategories';


    $('#categories_select').change(function() {
      $.ajax({
        url: "<%= update_subcategories_path %>",
        data: {
          category_id : $('#categories_select').val()
        },
        dataType: "script"
      });
    });
  });
</script>

View staticpage>update_subcategories.js.erb

$('#subcategories_select').html("<%= escape_javascript(options_for_select(@subcategories)) %>");

Thanks in advance!


Update


USING @PAVAN SUGGESTION. HOWEVER DROPDOWN SUBCATEGORY MENU SEEMS TO BREAK: image of subcat. menu

Subcategory breaking

  • 写回答

1条回答 默认 最新

  • weixin_33716154 2015-07-28 14:45
    关注

    Try the following

    <script>
      $(document).ready(function() {
      $('#subcategories_select').hide();
    
        $('#categories_select').change(function() {
          $('#subcategories_select').show();
          $.ajax({
            url: "<%= update_subcategories_path %>",
            data: {
              category_id : $('#categories_select').val()
            },
            dataType: "script"
          });
        });
      });
    </script>
    

    You don't need these lines var categories = '#categories_select'; var subcategories = '#subcategories';

    And also provide id subcategories_select to the sub_categories div instead to the sub_categories collection_select otherwise you will end showing the label Choose a brand: before changing the products collection_select.

    <div class="form-group", id="subcategories_select">
      <%= f.label 'Choose a brand:' %>
      <%= f.collection_select :subcategory_id, @subcategories, :id, :name, {:prompt => "Select an Product"} %>
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测