gpt4 book ai didi

ruby-on-rails-3 - Rails nested_form将项目添加到表行

转载 作者:行者123 更新时间:2023-12-04 05:25:15 24 4
gpt4 key购买 nike

我的目标是使用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放在几个地方,但没有将它们放在自己的行中。

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

最佳答案

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

By default fields_for inside nested_form_for adds <div class="fields"> wrapper around every nested object. But when you need to render nested fields inside a table you can disable default wrapper using :wrapper => false option and use the custom one:

<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>

Note: You need to specify id field. Otherwise fields_for will insert it after </tr>.

Also you need to override default behavior of inserting new subforms into your form using javascript:

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

A similar technique can be used for lists, for compatibility with a jQuery UI sortable list.

If you are using simple_form then add the :wrapper => false option to the surrounding simple_nested_form_for call, otherwise it gets overwritten by the :wrapper => nil default.

关于ruby-on-rails-3 - Rails nested_form将项目添加到表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12502416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com