Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selectable_column implementation #465

Open
brandon-meeks opened this issue Jul 2, 2024 · 2 comments
Open

selectable_column implementation #465

brandon-meeks opened this issue Jul 2, 2024 · 2 comments

Comments

@brandon-meeks
Copy link

I have poured over the source code for the select_column (selectable_column) that can be added to a table, but I have yet to determine what adding that column does or how to provide actions that can be performed once a row is selected. I don't see anywhere to add actions to a table that can be performed on a batch of selected items.

@spohlenz
Copy link
Member

spohlenz commented Jul 3, 2024

I don't have support for batch actions currently in the admin DSL yet (though it is on the roadmap), so right now the selectable_column method is a little incomplete.

Having said that, the Hotwire updates I've released today (in particular full support for Stimulus controllers) will make this much easier to wire up manually. I'll be going through that on another project tomorrow. Once I have the process sorted, I'll post here with an outline.

@spohlenz
Copy link
Member

spohlenz commented Jul 4, 2024

I've just pushed 014dc16 which adds a new batch-action Stimulus controller, as well as an example implementation in the sandbox. As I mentioned, this is by no means a complete and final solution but at least makes batch actions possible right now.

The steps to implement are:

  1. Add your routes and controller actions within your relevant <resource>_admin.rb file:
controller do
  def batch_get
    ids = params[:ids].split(",")
    flash[:message] = { title: "Success!", message: "Performed batch action via GET with #{ids.size} articles." }
    redirect_back fallback_location: admin.path
  end

  def batch_post
    ids = params[:ids].split(",")
    flash[:message] = { title: "Success!", message: "Performed batch action via POST with #{ids.size} articles." }
    redirect_back fallback_location: admin.path
  end
end

routes do
  collection do
    get :batch_get
    post :batch_post
  end
end

The ids param containing the selected IDs is a comma-separated array of strings so you'll need to split it as shown above.

  1. Duplicate the index.html.erb file from here and copy it to app/views/admin/<resources>/index.html. Then edit to add your batch action buttons within a toolbar:
<% toolbar(:secondary) do |t| %>
  <%= t.link "Batch Action (GET)", action: :batch_get, style: :info, data: { controller: "batch-action" } %>
  <%= t.link "Batch Action (POST)", action: :batch_post, style: :warning, data: { controller: "confirm batch-action", turbo_method: :post } %>
<% end %>

The button trigger should be a link with data-controller="batch-action", but POST actions are supported via data-turbo-method="post". You can also add the confirm action for an easy confirmation popover.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants