Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoon/jQuery: append items to relevant table

I have a Rails view that allows many PanelItems to be added to many Panels on a Page.

Models

class Page < ActiveRecord::Base
  has_many :panels
  accepts_nested_attributes_for :panels, allow_destroy: true

class Panel < ActiveRecord::Base
  has_many :panel_items
  accepts_nested_attributes_for :panel_items, allow_destroy: true, reject_if: :all_blank

class PanelItem < ActiveRecord::Base

Views

app/views/pages/_form.html.haml

= simple_form_for @page, html: { class: 'form-inline' } do |form|
  = form.input :name, required: :required, autofocus: :autofocus

  %table.table.table-hover
    %tbody
      = form.simple_fields_for :panels do |panel_form|
        %tr
          %td
            .table-responsive
              %table.table.table-hover
                %tbody.panel-items
                  = panel_form.simple_fields_for :panel_items do |panel_item_fields|
                    = render 'panel_item_fields', f: panel_item_fields
            = link_to_add_association panel_form, :panel_items,
                'data-association-insertion-method' => 'append',
                class: 'btn btn-success pull-right' do
              %span{class: 'glyphicon glyphicon-plus-sign'}
              New item
  = form.button :submit, "Update Page", class: 'btn-success'

app/views/pages/_panel_item_fields.html.haml

%tr.nested-fields
  %td
    = f.input :content_item_id,
        collection: current_site.content_items,
        selected: f.object.content_item_id,
        prompt: 'Select a content item'
  %td
    = link_to_remove_association f, title: 'remove this item',
        data: { toggle: 'tooltip', association: 'panel-item' } do
      %span.sr-only remove this item
      %span{class: 'glyphicon glyphicon-minus-sign'}

I want the New item button to append the panel_item_fields partial at the end of the associated table. I've tried many combinations of data-association-insertion-node and data-association-insertion-traversal attributes with no joy.

Cocoon uses the data- attributes to set the variables insertionTraversal and insertionNode, used in the following jQuery code:

$this[insertionTraversal](insertionNode)

but I don't really know how to read that.

EDIT: If I use:

'data-association-insertion-node' => '.panel-items',
'data-association-insertion-method' => 'append',

The new row gets appended to every table on the page, instead of just the closest table.

like image 363
Bryan Ash Avatar asked Jan 02 '15 20:01

Bryan Ash


People also ask

How to append content in HTML table using jQuery?

An append example in HTML table. You may append or add content in different elements of DOM by using jQuery append method. Not only you can add paragraphs, div element and others but also HTML table content as well. In this example, a table is already created as the page loads with heading (th) and dummy table data.

What is jQuery append and how to use it?

The jQuery append is quite useful as in different scenarios you need to add content on the fly like on the basis of certain user’s action. Examples can be adding new table rows in an HTML table or adding div elements.

How to append a div element using jQuery?

This is a very simple example of using append jQuery method where I will simply append div element in the body section of the document. As you click on the button, the append method will add a div element. Keep on clicking and new elements with same CSS and text will be created.

How to append rows to a table in Salesforce?

In this table we have to append rows by clicking on ticket button. addRow function appends the row in the table containing textbox ,label,button for save with seprate id of each row and each control in a row. SaveTicket function save the current row in which the click event is fired and adds the edit and delete button in the row inplace of save.


2 Answers

It seems that append'ing to a table appends to the tbody, so by making the New item button a sibling of the table:

.table-responsive
  %table.table.table-hover
    %tbody.panel-items
      = panel_form.simple_fields_for :panel_items do |panel_item_fields|
        = render 'panel_item_fields', f: panel_item_fields
  = link_to_add_association panel_form, :panel_items,
      'data-association-insertion-method' => 'append',
      'data-association-insertion-traversal' => 'prev',
      'data-association-insertion-node' => 'table',
      class: 'btn btn-success pull-right' do
    %span{class: 'glyphicon glyphicon-plus-sign'}
    New item

cocoon can now append to the previous table.

like image 135
Bryan Ash Avatar answered Sep 23 '22 14:09

Bryan Ash


Did you try with:

 'data-association-insertion-method'    => 'append' 
 'data-association-insertion-node'      => '.panel-items'
 'data-association-insertion-traversal' => 'closest'

[UPDATE: use a function to determine the insertion-node]

A while ago cocoon was updated so the data-association-insertion-node can contain a function to determine the insertion node, and then you can do more elaborate traversals, like first going up to do then go down to determine the correct place.

E.g. something like (copied from the cocoon documentation)

$(".add_sub_task a").
    data("association-insertion-method", 'append').
    data("association-insertion-node", function(link) {
      return link.closest('.row').next('.row').find('.sub_tasks_form')
    });
like image 30
nathanvda Avatar answered Sep 23 '22 14:09

nathanvda