简体   繁体   English

将 jQuery UI 中的选定元素传递给 Rails controller

[英]Pass selected elements from jQuery UI Selectable to Rails controller

I am using Rails 3. A Map has and belongs to many Collection s, and vice-versa.我正在使用 Rails 3。 Map拥有并属于许多Collection ,反之亦然。

What Is already there...什么已经存在...

I have a form that allows the user to select several maps that will go in a collection.我有一个表格,允许用户 select 几个地图,这些地图将 go 放在一个集合中。 For that, I use jQuery UI with the Selectable method in a grid.为此,我使用 jQuery UI 和网格中的Selectable方法。

This is how the grid is constructed:这是网格的构建方式:

<ol id="selectable">
<% for map in @user.maps %>
    <li class="ui-state-default">
        <strong><%= map.title %></strong><br>
        <%= image_tag(map.thumbnail_url) %>
    </li>
<% end %>
</ol>

It looks something like this:它看起来像这样:

网格示例

When the user is finished selecting, the selected maps (their <li> tag) will have the class .ui-selected .用户完成选择后,所选地图(它们的<li>标签)将具有 class .ui-selected

What I need...我需要的...

How could I pass the controller the IDs of those selected maps in order to add them to a new collection?我如何将 controller 传递给这些选定地图的 ID,以便将它们添加到新集合中? And what should I do in the controller? controller 应该怎么做? What are the best practices here?这里有哪些最佳实践?

What I thought about doing...我想做什么...

  1. I've thought about writing a custom submit handler for the form, which would go through all of those li.ui-selected and somehow try to extract the IDs, then merge them into a hidden form field and finally submit the form.我考虑过为表单编写一个自定义提交处理程序,它将 go 通过所有这些li.ui-selected并以某种方式尝试提取 ID,然后将它们合并到一个隐藏的表单字段中,最后提交表单。 Then, in the controller, I'd split the hidden form field again, extract the IDs and get the Maps from the database.然后,在 controller 中,我将再次拆分隐藏表单字段,提取 ID 并从数据库中获取地图。

  2. Another way would probably be to have a collection_select , that somehow syncs with the jQuery Selectable.另一种方法可能是有一个collection_select ,它以某种方式与 jQuery Selectable 同步。 The collection_select I have is:我拥有的collection_select是:

     <%= collection_select:collection, :map_ids, @collection.user.maps, :id, :title, {:selected => @collection.map_ids }, {:class => "map_select", :multiple => true, :size => '10', :name => 'collection[map_ids][]' } %>

Both sound a bit hacky though.两者听起来都有些骇人听闻。 Isn't there something easier or more JS/Rails-esque?没有更简单或更多的 JS/Rails 风格吗?

Here's what I ended up with.这就是我最终的结果。 This is my collection_select .这是我的collection_select

<%= collection_select :collection, 
    :map_ids, 
    @collection.user.maps, 
    :id,
    :title, 
    { :selected => nil },
    { :class => "map_select hiddenElement", :multiple => true, :size => '10', :name => 'collection[map_ids][]' }

This is how my individual containers are created:这就是我的个人容器的创建方式:

<ol id="selectable">
  <% for map in current_user.maps %>
    <% if @collection.maps.include? map %>
      <li class="singleMapContainer ui-selected">
    <% else %>
      <li class="singleMapContainer">
    <% end %>
      <div class="singleMapInfo">
        <span class="mapId" style="display:none;"><%= map.id %></span>
        <strong><%= map.title %></strong><br>
        <%= map.created_at %><br>
        <%= image_tag(map.thumbnail_url) %>
      </div>
    </li>
  <% end %>
</ol>

Here's the Javascript that synchronizes them when the form is submitted:这是在提交表单时同步它们的 Javascript:

$("#new_collection, .edit_collection").submit(function(){
  // iterate over each selected map container element
  $("li.ui-selected").each(function(){
    var mapId = $(this).find("span.mapId").html();  // find the map ID
    $("#collection_map_ids option[value='" + mapId + "']:first")[0].selected = true;  // synchronize the multiple select box
  });
  return true;
});

Serializing everything to a hidden INPUT is a good option.将所有内容序列化到隐藏的 INPUT 是一个不错的选择。 If you don't like that one an (arguably) cleaner approach would be to just make an AJAX call to your controller;如果您不喜欢这种(可以说)更清洁的方法,那就是对您的 controller 进行 AJAX 调用; this could even be part of a select event handler on your selectable.这甚至可能是您可选择的 select 事件处理程序的一部分。 This could then serialize the selected items (which I think you even get as an argument to the selection event, but I forget exactly and am too lazy too look it up) in to the POST (or GET or PUT) data of your AJAX call.然后,这可以将所选项目(我认为您甚至可以将其作为选择事件的参数,但我完全忘记并且我太懒了)序列化到您的 AJAX 调用的 POST(或 GET 或 PUT)数据中.

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

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