Hi,
I have the following function component structure hiercharchy:
page with table
data_table component
some CSS around the following components
table component
pagination component
The table component expects data as slots, e.g. like this: <:col let={col} label="Creation Date" field={:inserted_at}><%= col.inserted_at %></:col>
I have different pages with different kind of data, so the structure varies. How can I pass the data (like columns) to the table component FROM my data_table component?
I was trying to pass it as attribute and then do sth like this:
for col <- my_data do
<:col let={col} label="Creation Date" field={:inserted_at}><%= col.inserted_at %></:col>
end
But that does not work as I get A slot entry must be a direct child of a component error. Any ideas?
Hmm I’m also curious about this, was building something similar and ended up copying the table component markup (it consists of <.table>,<.thead>, <.tr> function components so it was easy to do).
You could pass col as an assign like so:
<.table col={[... list ... ]}>
Since I think a slot gets turned into an assign anyway but with a special data structure. But I am not sure how to create this data structure. Perhaps via inner_block/2? Haven’t tried it yet.
The table component isn’t under my control and just expects data using those col slots. So how can I pass data through my data_table component to that component when I don’t know the fields beforehand?
Haven’t tried that, but the solution is actually way easier than expected.
As named slots are available as an assign, we can just pass it on to the next component.
I wish I had searched the forums sooner! Just spent an afternoon trying to figure out how to pass a slot into a table which itself is in a parent component. Thank you!!!