petal_components icon indicating copy to clipboard operation
petal_components copied to clipboard

Data Table - Does sortable support embedded schemas?

Open safari12 opened this issue 1 year ago • 2 comments

I was wondering if the data table component supports sorting embedded schema fields. I have an embedded schema called "metrics" and want make its fields be sortable in a table called "assets". If not, then I think it would cool to have the sortable attribute to have the option to accept an embedded field name for example sortable={:embedded_field_name}

defmodule Metrics do
  use Ecto.Schema

  embedded_schema do
    field :cost_basis, :float, default: 0.0
    field :value, :float, default: 0.0
    field :capital_gain, :float, default: 0.0
  end
end

defmodule Assets do
  schema "assets" do
    embeds_one(:metrics, Metrics)
  
    timestamps()
  end
end

<.data_table :if={@index_params} meta={@meta} items={@assets}>
  <:if_empty>No assets found</:if_empty>
  <:col field={:label} sortable />
  # if sortable does not support embedded schema, then sortable={:embedded_field_name} would be cool to support this syntax
  <:col :let={asset} field={:metrics} sortable={:cost_basis}>
        <%= asset.metrics.cost_basis %>
  </:col>
</.data_table>

safari12 avatar Jun 22 '23 22:06 safari12