Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Cocoon (Nested Forms) and jQuery sortable together

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!

like image 577
user1483188 Avatar asked Oct 22 '22 18:10

user1483188


1 Answers

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.

like image 176
Denis Tataurov Avatar answered Nov 10 '22 12:11

Denis Tataurov