代码示例

#gem
gem 'select_all-rails'

#application.js
//= require select_all.js

#index.html.erb中的js设置
<%= content_for :javascripts do %>
  <script>
      $("#selectAll").select_all();
  </script>
<% end %>
#application.html.erb中设置
<%= yield :javascripts %>

#routes.rb
resources :words do
  delete :remove_select, on: :collection
end

#controller中设置
def remove_select
  unless params[:word_ids].nil?
    Word.where(id: params[:word_ids]).destroy_all #不能使用delete_all,其不会更新关联项
    redirect_to words_url
    flash[:success] = '选中的文章被全部删除!'
  else
    redirect_to words_url
    flash[:danger] = "文章未被选择!"
  end
end

#view中设置,分别选择选择的id和被选择框的css
<%= form_tag remove_select_words_path, method: :delete do %>
  <!--多选框-->
  <%= check_box_tag "[word_ids][]", word.id, false, class: "selectable" %>

  <!--gem提供的多选框-->
  <input type="checkbox" id="selectAll"><small>全选或取消&nbsp</small></input>
  <%= submit_tag "删除选中项", class: "btn btn-sm btn-danger", id: "remove_select" %> 
<% end -%>

results matching ""

    No results matching ""