Rails 级联菜单的问题,二级联动效果

nicetyler · September 08, 2017 · Last by nicetyler replied at October 24, 2017 · 3546 hits

做文档管理部分,我在表单里面做一个二级联动效果,首先判断文档分类,然后根据不同的分类,显示文档对应不同的对象 这是 document 表里面有个 type_id 和 object_id 这是我的表单内的代码,二级联动部分

<div class="form-group">
  <%= f.label :type_id, "文档分类" %>
  <div>
    <%= f.collection_select :type_id, Type.where(class_code: "DOCUMENT"), :id, :description, class:"form-control" %>
  </div>
</div>
<div class="form-group">
  <%= f.label :object_id, "文档对象" %>
  <div class="doc_obj">
    <%= f.collection_select :object_id, Product.all, :id, :name, class:"form-control" %>
    <%= f.collection_select :object_id, Solution.all, :id, :title, class:"form-control" %>
    <%= f.collection_select :object_id, Case.all, :id, :title, class:"form-control" %>
  </div>
</div>

一开始我写的 jquery

$(document).ready(function () {
    $("#document_type_id").change(function () {
        $("#document_type_id option").each(function (i, o) {
            if ($(this).prop("selected")) {
                $(".doc_obj select").hide();
                $(".doc_obj select").eq(i).show();
            }
        });
    });
    $("#document_type_id").change();
});

但是提交的时候会出问题,我选 product 的 id,但是提交的确是 case 的 id,后来我想直接在 jquery 中插入 html 代码,但是 jqeury 里面好像不能直接插 erb 代码

$(document).ready(function () {
    $("#document_type_id option[value='10']").click(function(){
        $(".doc_obj").html("<%= f.collection_select :object_id, Product.all, :id, :name, class:"form-control" %>")
    })
});

不知道怎么写了,就是表单里面的二级联动,有没有大神能指点迷津,万分感谢

$(".doc_obj").html("<%= f.collection_select :object_id, Product.all, :id, :name, class:"form-control" %>")

jquery 可以插入 ruby 代码,只是不能再用f.collection_select了,因为f这个对象此时是不存在的,可以尝试用select_tag 或其他的 help 方法。也可以用 ajax 来做,把type_id 传入后台,生成想要的 html 后直接替换。

Reply to bobo

嗯我先试下其它的帮助方法,如果不行,我就用 ajax 来做😂 ajax 也好久没用都忘了,看下教程怎么弄,谢了啊

Reply to bobo

不好意思,我可以在问一下,如果用 ajax 应该怎么写啊,怎么样能在我选择分类的时候把 type_id 传给后台啊😭

Reply to bobo

我每次要提交了才可以上传,如果直接改变分类的时候就能把 type_id 提交上去那这个问题就解决了

<script>
  $(function({
      $("#type_id").change(function(){
          $.ajax({
              url: xxx_path,
              type: 'get',
              data: { type_id: $('#type_id').val() }
          }).success(data)({
              $('xxx').html(data);
          })
      });
  }))
<script>

大概是这样子吧

Reply to bobo

你这个代码好像写的有问题一直报错,能不能帮忙看下😂 😂 😂 formal parameter name expected 一直报这个错误

Reply to nicetyler

type_id: $ 中间隔个空格试试

Reply to bobo

这个功能还是没写出来😭 就是一个文档管理,然后分成产品文档和方案文档,下拉选产品文档就显示产品的列表,下拉选方案就显示方案,我后来只能分开写 3 个页面,用 js.erb 也不行

$('#document_id').change(function () {
    var checkValue = $(this).val()
    switch (checkValue){
        case("3"):
            $('#document_object').empty();
            $('#document_object').append( <%= select_tag(:product_id, options_for_select([['产品01', 1], ['产品02', 2]])) %>);
            break;
        case("5"):
            alert("输入的是方案文档");
            break;
        default:
            alert("输入错误");
            break;
    }
})

view

select_tag 'document_type_id', options_from_collection_for_select (@document_types, :id, :name), data:{remote: true, url: document_path}
select_tag 'production_id',[]

controller

def document
    if params[:document_type_id]
     @productions = Production.where(document_id: params[:document_type_id])  
  end
end

document.js.erb

$('#production_id').html('<%= j(options_from_collection_for_select @productions, :id, :name)%>')

我在工作中一般是这么写的。你试试,可能要将select_tag 转换为对应的f.select

10 Floor has deleted
Reply to bobo

非常感谢你,这个问题我搞定了😆

You need to Sign in before reply, if you don't have an account, please Sign up first.