feat: adds dynamic cols to table core component
This commit is contained in:
parent
11179e51f0
commit
100ed96493
1 changed files with 33 additions and 0 deletions
|
|
@ -318,6 +318,13 @@ defmodule MvWeb.CoreComponents do
|
||||||
default: &Function.identity/1,
|
default: &Function.identity/1,
|
||||||
doc: "the function for mapping each row before calling the :col and :action slots"
|
doc: "the function for mapping each row before calling the :col and :action slots"
|
||||||
|
|
||||||
|
attr :dynamic_cols, :list,
|
||||||
|
default: [],
|
||||||
|
doc: "list of dynamic column definitions with :custom_field and :render functions"
|
||||||
|
|
||||||
|
attr :sort_field, :any, default: nil, doc: "current sort field"
|
||||||
|
attr :sort_order, :atom, default: nil, doc: "current sort order"
|
||||||
|
|
||||||
slot :col, required: true do
|
slot :col, required: true do
|
||||||
attr :label, :string
|
attr :label, :string
|
||||||
end
|
end
|
||||||
|
|
@ -335,6 +342,16 @@ defmodule MvWeb.CoreComponents do
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th :for={col <- @col}>{col[:label]}</th>
|
<th :for={col <- @col}>{col[:label]}</th>
|
||||||
|
<th :for={dyn_col <- @dynamic_cols}>
|
||||||
|
<.live_component
|
||||||
|
module={MvWeb.Components.SortHeaderComponent}
|
||||||
|
id={:"sort_custom_field_#{dyn_col[:custom_field].id}"}
|
||||||
|
field={"custom_field_#{dyn_col[:custom_field].id}"}
|
||||||
|
label={dyn_col[:custom_field].name}
|
||||||
|
sort_field={@sort_field}
|
||||||
|
sort_order={@sort_order}
|
||||||
|
/>
|
||||||
|
</th>
|
||||||
<th :if={@action != []}>
|
<th :if={@action != []}>
|
||||||
<span class="sr-only">{gettext("Actions")}</span>
|
<span class="sr-only">{gettext("Actions")}</span>
|
||||||
</th>
|
</th>
|
||||||
|
|
@ -349,6 +366,22 @@ defmodule MvWeb.CoreComponents do
|
||||||
>
|
>
|
||||||
{render_slot(col, @row_item.(row))}
|
{render_slot(col, @row_item.(row))}
|
||||||
</td>
|
</td>
|
||||||
|
<td
|
||||||
|
:for={dyn_col <- @dynamic_cols}
|
||||||
|
phx-click={@row_click && @row_click.(row)}
|
||||||
|
class={@row_click && "hover:cursor-pointer"}
|
||||||
|
>
|
||||||
|
{if dyn_col[:render] do
|
||||||
|
rendered = dyn_col[:render].(@row_item.(row))
|
||||||
|
if rendered == "" do
|
||||||
|
""
|
||||||
|
else
|
||||||
|
rendered
|
||||||
|
end
|
||||||
|
else
|
||||||
|
""
|
||||||
|
end}
|
||||||
|
</td>
|
||||||
<td :if={@action != []} class="w-0 font-semibold">
|
<td :if={@action != []} class="w-0 font-semibold">
|
||||||
<div class="flex gap-4">
|
<div class="flex gap-4">
|
||||||
<%= for action <- @action do %>
|
<%= for action <- @action do %>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue