Current section
43 Versions
Jump to
Current section
43 Versions
Compare versions
11
files changed
+86
additions
-52
deletions
| @@ -1,5 +1,9 @@ | |
| 1 1 | # CHANGELOG |
| 2 2 | |
| 3 | + ## v0.3.2 (2020-10-18) |
| 4 | + |
| 5 | + * Improve sorting and formatting in Ecto Stats tables |
| 6 | + |
| 3 7 | ## v0.3.1 (2020-10-17) |
| 4 8 | |
| 5 9 | * Ensure the dashboard compiles without optional dependencies |
| @@ -77,5 +77,5 @@ | |
| 77 77 | {<<"name">>,<<"ecto_psql_extras">>}, |
| 78 78 | {<<"optional">>,true}, |
| 79 79 | {<<"repository">>,<<"hexpm">>}, |
| 80 | - {<<"requirement">>,<<"~> 0.2">>}]]}. |
| 81 | - {<<"version">>,<<"0.3.1">>}. |
| 80 | + {<<"requirement">>,<<"~> 0.3">>}]]}. |
| 81 | + {<<"version">>,<<"0.3.2">>}. |
| @@ -24,6 +24,7 @@ defmodule Phoenix.LiveDashboard.TableComponent do | |
| 24 24 | |> Map.put_new(:limit, @limit) |
| 25 25 | |> Map.put_new(:row_attrs, []) |
| 26 26 | |> Map.put_new(:hint, nil) |
| 27 | + |> Map.update(:default_sort_by, nil, &(&1 && to_string(&1))) |
| 27 28 | |> Map.put_new_lazy(:rows_name, fn -> |
| 28 29 | Phoenix.Naming.humanize(params.title) |> String.downcase() |
| 29 30 | end) |
| @@ -57,7 +58,7 @@ defmodule Phoenix.LiveDashboard.TableComponent do | |
| 57 58 | |> Map.new() |
| 58 59 | |> Map.put_new_lazy(:header, fn -> Phoenix.Naming.humanize(field) end) |
| 59 60 | |> Map.put_new(:header_attrs, []) |
| 60 | - |> Map.put_new(:format, & &1[field]) |
| 61 | + |> Map.put_new(:format, & &1) |
| 61 62 | |> Map.put_new(:cell_attrs, []) |
| 62 63 | |> Map.put_new(:sortable, nil) |
| 63 64 | |
| @@ -87,13 +88,12 @@ defmodule Phoenix.LiveDashboard.TableComponent do | |
| 87 88 | end |
| 88 89 | |
| 89 90 | defp normalize_table_params(assigns) do |
| 90 | - %{columns: columns, page: %{params: all_params}} = assigns |
| 91 | - |
| 91 | + %{columns: columns, page: %{params: all_params}, default_sort_by: sort_by} = assigns |
| 92 92 | sortable_columns = sortable_columns(columns) |
| 93 93 | |
| 94 94 | sort_by = |
| 95 95 | all_params |
| 96 | - |> get_in_or_first("sort_by", sortable_columns) |
| 96 | + |> get_in_or_first("sort_by", sort_by, sortable_columns) |
| 97 97 | |> String.to_atom() |
| 98 98 | |
| 99 99 | sort_dir = |
| @@ -118,19 +118,19 @@ defmodule Phoenix.LiveDashboard.TableComponent do | |
| 118 118 | end |
| 119 119 | |
| 120 120 | defp sortable_columns(columns) do |
| 121 | - for column <- columns, column[:sortable], do: to_string(column[:field]) |
| 121 | + for column <- columns, column.sortable, do: to_string(column.field) |
| 122 122 | end |
| 123 123 | |
| 124 124 | defp sortable_dirs(columns, field) do |
| 125 | - case Enum.find(columns, &(&1[:field] == field)) do |
| 125 | + case Enum.find(columns, &(&1.field == field)) do |
| 126 126 | %{sortable: :desc} -> ~w(desc asc) |
| 127 127 | %{sortable: :asc} -> ~w(asc desc) |
| 128 128 | end |
| 129 129 | end |
| 130 130 | |
| 131 | - defp get_in_or_first(params, key, valid) do |
| 131 | + defp get_in_or_first(params, key, default \\ nil, valid) do |
| 132 132 | value = params[key] |
| 133 | - if value in valid, do: value, else: hd(valid) |
| 133 | + if value in valid, do: value, else: default || hd(valid) |
| 134 134 | end |
| 135 135 | |
| 136 136 | @impl true |
| @@ -180,8 +180,8 @@ defmodule Phoenix.LiveDashboard.TableComponent do | |
| 180 180 | <thead> |
| 181 181 | <tr> |
| 182 182 | <%= for column <- @columns do %> |
| 183 | - <%= tag_with_attrs(:th, column[:header_attrs], [column]) %> |
| 184 | - <%= if direction = column[:sortable] do %> |
| 183 | + <%= tag_with_attrs(:th, column.header_attrs, [column]) %> |
| 184 | + <%= if direction = column.sortable do %> |
| 185 185 | <%= sort_link(@socket, @page, @table_params, column, direction) %> |
| 186 186 | <% else %> |
| 187 187 | <%= column.header %> |
| @@ -194,8 +194,8 @@ defmodule Phoenix.LiveDashboard.TableComponent do | |
| 194 194 | <%= for row <- @rows do %> |
| 195 195 | <%= tag_with_attrs(:tr, @row_attrs, [row]) %> |
| 196 196 | <%= for column <- @columns do %> |
| 197 | - <%= tag_with_attrs(:td, column[:cell_attrs], [row]) %> |
| 198 | - <%= column[:format].(row) %> |
| 197 | + <%= tag_with_attrs(:td, column.cell_attrs, [row]) %> |
| 198 | + <%= column.format.(row[column.field]) %> |
| 199 199 | </td> |
| 200 200 | <% end %> |
| 201 201 | </tr> |
| @@ -63,12 +63,12 @@ defmodule Phoenix.LiveDashboard.PageBuilder do | |
| 63 63 | }, |
| 64 64 | %{ |
| 65 65 | field: :memory, |
| 66 | - format: &format_words(&1[:memory]), |
| 66 | + format: &format_words/1, |
| 67 67 | sortable: :desc |
| 68 68 | }, |
| 69 69 | %{ |
| 70 70 | field: :owner, |
| 71 | - format: &encode_pid(&1[:owner]) |
| 71 | + format: &encode_pid/1 |
| 72 72 | } |
| 73 73 | ] |
| 74 74 | end |
| @@ -226,13 +226,10 @@ defmodule Phoenix.LiveDashboard.PageBuilder do | |
| 226 226 | * `:columns` - Required. A `Keyword` list with the following keys: |
| 227 227 | * `:field` - Required. An identifier for the column. |
| 228 228 | * `:header` - Label to show in the current column. Default value is calculated from `:field`. |
| 229 | - * `:header_attrs` - A list with HTML attributes for the column header. |
| 230 | - More info: `Phoenix.HTML.Tag.tag/1`. Default `[]`. |
| 231 | - * `:format` - Function which receives the row data and returns the cell information. |
| 232 | - Default is calculated from `:field`: `row[:field]`. |
| 233 | - * `:cell_attrs` - A list with HTML attributes for the table cell. |
| 234 | - It also can be a function which receives the row data and returns an attribute list. |
| 235 | - More info: `Phoenix.HTML.Tag.tag/1`. Default: `[]`. |
| 229 | + * `:header_attrs` - A list with HTML attributes for the column header. Default: `[]`. |
| 230 | + * `:format` - Function which receives the value and returns the cell information. |
| 231 | + Default is the field value itself. |
| 232 | + * `:cell_attrs` - A list with HTML attributes for the table cell. Default: `[]`. |
| 236 233 | * `:sortable` - Either `:asc` or `:desc` with the default sorting. When set, the column |
| 237 234 | header is clickable and it fetches again rows with the new order. At least one column |
| 238 235 | should be sortable. Default: `nil` |
| @@ -244,6 +241,9 @@ defmodule Phoenix.LiveDashboard.PageBuilder do | |
| 244 241 | * `:rows_name` - A string to name the representation of the rows. |
| 245 242 | Default is calculated from the current page. |
| 246 243 | |
| 244 | + * `:default_sort_by` - The default columnt to sort by to. |
| 245 | + Defaults to the first sortable column. |
| 246 | + |
| 247 247 | * `:title` - The title of the table. |
| 248 248 | Default is calculated with the current page. |
| @@ -46,7 +46,7 @@ defmodule Phoenix.LiveDashboard.ApplicationsPage do | |
| 46 46 | field: :tree?, |
| 47 47 | header: "Sup tree?", |
| 48 48 | cell_attrs: [class: "text-center"], |
| 49 | - format: &if(&1[:tree?], do: "✓") |
| 49 | + format: &if(&1, do: "✓") |
| 50 50 | }, |
| 51 51 | %{ |
| 52 52 | field: :version, |
Loading more files…