gpt4 book ai didi

ruby-on-rails - 在 ruby​​ on rails 中使用 dataTable 时如何在每行添加复选框?

转载 作者:数据小太阳 更新时间:2023-10-29 08:20:42 26 4
gpt4 key购买 nike

我在我的 ruby​​ on rails 应用程序中使用数据表。它也适用于表格工具。但我想在数据表中的每行也添加复选框以执行批处理操作。我搜索了 the official site 中的所有示例但我找不到复选框。

我想在我的数据表中执行批量删除操作。

谁能建议我在我的数据表中添加复选框?

最佳答案

我不确定dataTables是否提供直接删除机制。但这是我在普通 rails 上所做的。让我们以产品为例。在你的 View 文件中有类似的东西:

<%= form_for('Product', :as => 'products', :url => delete_selected_products_path) do |f| %>
<%= f.button :submit, 'Delete Selected', :class => 'btn-danger product' %>

<div class="clear">&nbsp;</div>

<table>
<thead>
<tr>
<th class="select-row"></th
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th class="actions"></th>
</tr>
</thead>

<tbody>
<% @products.each do |product| %>
<tr>
<td class="first-column"><%= check_box_tag 'ids[]', product.id, false, :class => 'table-row-checkbox' %></td>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<td><%= link_to "View & Edit", product_path(product) %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

并在您的 ProductsController 中将 delete_selected 操作定义为

def delete_selected
params[:ids].each do |id|
product = Product.find(id)
product.destroy
end unless params[:ids].blank?
redirect_to products_path, :notice => 'Selected products are deleted successfully!'
end

并在您的 routes.rb 中添加 delete_selected 作为产品资源的集合:

resources :products do
post :delete_selected, :on => :collection
end

如果需要,您可以使用 AJAX。 :)

关于ruby-on-rails - 在 ruby​​ on rails 中使用 dataTable 时如何在每行添加复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14744882/

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