Question

I currently have an input form generated using Cocoon and would like to make the input fields created by it sortable (using sortable from jQuery-ui).

The code for the nested form is:

<%= f.simple_fields_for :checkout_procedures do |procedure| %>  
  <li><%= render 'checkout_procedure_fields', :f => procedure %></li> 
<% end %>  
<div class="links">
  <%= link_to_add_association 'add step', f, :checkout_procedures %>
</div>

The partial that is rendered (_checkout_procedure_fields) is:

<div class="checkout_procedure nested-fields">
  <table>
    <tr>
      <td><%= f.input :step %></td>
      <td><%= link_to_remove_association "remove step", f %></td>
    </tr>
  </table>
</div>

I am able to get the existing elements sortable by putting them inside of a <div> and setting it sortable using jQuery:

<div class="sortThese">
   <%= f.simple_fields_for :checkout_procedures do |procedure| %>  
     <li><%= render 'checkout_procedure_fields', :f => procedure %></li> 
   <% end %>  
</div> 
<div class="links">
  <%= link_to_add_association 'add step', f, :checkout_procedures %>
</div>

<script>
 $(document).ready(function() {
   $(".sortThese").sortable();
      });
 </script>

But doing this breaks the functionality of Cocoon's link_to_add_association and link_to_remove_association. Using <ul> with <li> also results in the same Cocoon malfunction. I've been looking around to hack some javascript that moves each input form in and out of the sortable div (doing this seems to make the links work again), but obviously this is inelegant. If anybody has any suggestions, I'd really appreciate them!

Was it helpful?

Solution

It sorts well, if you remove <li> tag. Tried in my project, where i have the same markup, except li wrapper of partial, and it works.

OTHER TIPS

Even with the <li> tag inside your partial, dragging a list item to a new position only moves what is inside the list item's <li> tag. Unfortunately, dragging the item will not move the item's hidden parent <input> tag since cocoon places it outside of your nested-field (i.e. outside of your <li>). Therefore, although your list will appear sorted in the browser, the changes won't be reflected in the params received by your controller method.

If you've found a solution for this, please post it.

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top