-
First of all, thank you for creating ViewComponent, it's a breath of fresh air after regular partials & helpers 🙏🏻 I'm trying to create a generic data table component that will be usable for different types of data. The component currently looks like this: <%= render DataTableComponent.new(
type: "products",
headers: [nil, "name", "price", "purchase_price"],
body: @products.map { |product|
[
image_tag(product.image_url.to_s),
link_to(product.name, product_path(product.product_id)),
number_to_currency(product.price),
number_to_currency(product.purchase_price),
]
},
right_align: %w[price purchase_price],
sortable: %w[name price purchase_price],
pagination: @pagy
) %> This works, but it's not great that headers and data are separate, and that alignment and sortability of individual columns are specified separately. I'm wondering if there is a way to use slots to achieve something like this: <%= render DataTableComponent.new(
type: "products",
items: @products,
pagination: @pagy
) do |c| %>
<% c.column(name: nil) do |product| %>
<%= image_tag product.image_url.to_s, class: "w-10 h-10" %>
<% end %>
<% c.column(name: "name", sortable: true) do |product| %>
<%= link_to product.name, product_path(product.product_id), class: "text-lightblue-700" %>
<% end %>
<% c.column(name: "price", sortable: true, right_align: true) do |product| %>
<%= number_to_currency product.price %>
<% end %>
<% c.column(name: "purchase_price", sortable: true, right_align: true) do |product| %>
<%= number_to_currency product.price %>
<% end %>
<% end %> |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Is there any news here? I am also interested in this use case. |
Beta Was this translation helpful? Give feedback.
-
If someone comes accross that, I made this possible, it's not really workable with slots, but you can work around it, this blog post describes how: |
Beta Was this translation helpful? Give feedback.
Finally got around to this, apologies for the delay! Here's a really basic starting point. Note that lambda slots can use methods from the component itself, so you could use that to inform how the row components are constructed in
renders_many :rows
and manually specify which fields on the model you'd like to use.Hope that's helpful to you! If you find it raises more questions than answers, feel free to respond here and I'll help where I can.