【问题标题】:Rails nested_form adding items to table rowsRails nested_form 将项目添加到表行
【发布时间】:2012-09-19 20:31:08
【问题描述】:

我的目标是使用nested_form gem:https://github.com/ryanb/nested_form 但不是每次添加对象时只创建一组新的标签和字段,我想在现有表中插入一行。

= nested_form_for @transaction do |f|
%h3 Line Items 
%table
  %tr
    %th Branch
    %th Department
    %th Invoice #
    %th Amount
    %th Transaction Type
    %th Deposit (y/n)
    %th

  = f.fields_for :line_items do |line_item|
    %tr
      %td 
        = line_item.text_field :location_id
      %td 
        = line_item.text_field :department_id
      %td 
        = line_item.text_field :invoice_num
      %td 
        = line_item.text_field :amount
      %td 
        = line_item.text_field :transaction_type
      %td 
        = line_item.text_field :deposit
      %td= line_item.link_to_remove "Remove"
    %p= f.link_to_add "Add", :line_items

.link_to_add 按钮只是在第一行,第一个 td 中创建一堆字段。

<h3>Line Items</h3>
<table>
  <tr>
    <th>Branch</th>
    <th>Department</th>
    <th>Invoice #</th>
    <th>Amount</th>
    <th>Transaction Type</th>
    <th>Deposit (y/n)</th>
    <th></th>
  </tr>
  <div class="fields"><tr>
    <td>
      <input id="transaction_line_items_attributes_0_location_id" name="transaction[line_items_attributes][0][location_id]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_department_id" name="transaction[line_items_attributes][0][department_id]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_invoice_num" name="transaction[line_items_attributes][0][invoice_num]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_amount" name="transaction[line_items_attributes][0][amount]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_transaction_type" name="transaction[line_items_attributes][0][transaction_type]" size="30" type="text" />
    </td>
    <td>
      <input id="transaction_line_items_attributes_0_deposit" name="transaction[line_items_attributes][0][deposit]" size="30" type="text" />
    </td>
    <td><input id="transaction_line_items_attributes_0__destroy" name="transaction[line_items_attributes][0][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields" data-association="line_items">Remove</a></td>
  </tr>
  <td><a href="javascript:void(0)" class="add_nested_fields" data-association="line_items">Add</a></td>
  </div>
</table>

我尝试将 .link_to_add 放在几个地方,但它没有将它们放在自己的行中。

有没有一种简单的方法可以每次都添加一行输入框?

【问题讨论】:

  • 你有views/_line_items_fields.html.haml 文件吗?如果有,它是什么样子的?

标签: ruby-on-rails-3 html-table nested-forms


【解决方案1】:

这对我帮助很大:https://github.com/ryanb/nested_form/wiki/How-To:-Render-nested-fields-inside-a-table

默认情况下,fields_for 在 nested_form_for 内会在每个嵌套对象周围添加 &lt;div class="fields"&gt; 包装器。但是,当您需要在表格中呈现嵌套字段时,您可以使用 :wrapper =&gt; false 选项禁用默认包装器并使用自定义包装器:

<table>
  <%= f.fields_for :tasks, :wrapper => false do |task_form| %>
    <tr class="fields">
      <td>
        <%= task_form.hidden_field :id %>
        <%= task_form.text_field :name %>
      </td>
      <td><%= task_form.link_to_remove 'Remove' %></td>
    </tr>
  <% end %>
  <tr>
    <td><%= f.link_to_add 'Add', :tasks %></td>
  </tr>
</table>

注意:您需要指定 id 字段。否则fields_for会在&lt;/tr&gt;之后插入。

您还需要覆盖使用 javascript 将新子表单插入表单的默认行为:

window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
  var $tr = $(link).closest('tr');
  return $(content).insertBefore($tr);
}

类似的技术可用于列表,以与 jQuery UI 可排序列表兼容。

如果您使用的是 simple_form,则将 :wrapper => false 选项添加到周围的 simple_nested_form_for 调用中,否则它会被 :wrapper => nil 默认值覆盖。

【讨论】:

  • :wrapper =&gt; false 就是我们所需要的......!
  • 嗯。不适合我,我必须做很多事情。包括添加的 javascript,但它就像一个魅力。
【解决方案2】:

我最终将我的 link_to_add 作为表格的最后一行,将其添加到我的 application.js(主要来自 wiki 上的示例)

jQuery(function ($) {
  window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
    if($(link).hasClass('insert_in_table')){
      return $(content).insertBefore($(link).parent().parent());
    }
    else{
      return $(content).insertBefore(link);
    }
  };
});

我的表单如下所示:

<table class="tab">
  <tr>
    <th>My Headers</th>
  </tr>
<%= f.fields_for :line_items, :wrapper => false do |form| %>
  <tr class="fields">
    <td>MY FIELDS</td>
  </tr>
<% end %>
  <tr>
     <td><%= f.link_to_add "Add more line items", :line_items, :class => 'insert_in_table' %></td>
  </tr>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2015-04-05
    • 2015-02-11
    • 2020-06-03
    • 2020-06-23
    相关资源
    最近更新 更多