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

Mar*_*042 2 ruby ruby-on-rails ruby-on-rails-3 jquery-datatables

我在我的ruby on rails应用程序中使用DataTables.它也适用于桌面工具.但我想在datatable中每行添加复选框以执行批处理操作.我搜索了官方网站上的所有示例,但我找不到复选框.

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

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

Man*_*nga 7

我不确定dataTables它是否提供直接删除机制.但这就是我在普通轨道上所做的.让我们举一个产品的例子.在您的视图文件中有类似于:

<%= 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 %>
Run Code Online (Sandbox Code Playgroud)

并在您的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
Run Code Online (Sandbox Code Playgroud)

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

resources :products do
  post :delete_selected, :on => :collection
end
Run Code Online (Sandbox Code Playgroud)

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