View Source ExTeal.Field behaviour (ExTeal v0.28.0)

The core struct that represents a field on a resource served by ExTeal.

Summary

Functions

Override the default filter for the field.

Use a getter function to display a computed field on the resource.

Define the options for the select field. The function accepts a list of options that are either strings or maps with of value and label keys. If the list members are strings, the value will be used for both the value and label of the <option> element it represents.

Mark a field as virtual, which will update how teal queries the database for the field.

Types

@type t() :: %ExTeal.Field{
  as_html: term(),
  attribute: term(),
  can_see: term(),
  component: term(),
  embed_field: term(),
  field: term(),
  filterable: term(),
  getter: term(),
  name: term(),
  options: term(),
  panel: term(),
  pivot_field: term(),
  prefix_component: term(),
  private_options: term(),
  relationship: term(),
  sanitize: term(),
  show_on_detail: term(),
  show_on_edit: term(),
  show_on_index: term(),
  show_on_new: term(),
  sortable: term(),
  stacked: term(),
  text_align: term(),
  type: term(),
  value: term(),
  virtual: term()
}

Callbacks

Link to this callback

apply_options_for(t, struct, struct, atom)

View Source
@callback apply_options_for(t(), struct(), struct(), atom()) :: t()
@callback default_sortable() :: boolean()
@callback filterable_as() :: ExTeal.FieldFilter.valid_type()
@callback make(atom(), String.t() | nil) :: t()
@callback sanitize_as() :: atom() | false
Link to this callback

value_for(t, struct, atom)

View Source
@callback value_for(t(), struct(), atom()) :: any()

Functions

Link to this function

filter_as(field, filter_module)

View Source
@spec filter_as(t(), module()) :: t()

Override the default filter for the field.

Use a getter function to display a computed field on the resource.

The getter function is given a schema and expects a string result

Link to this function

nested_value_for(field, model)

View Source
Link to this function

struct_from_field(implementation, name, label)

View Source
Link to this function

transform_options(options)

View Source

Define the options for the select field. The function accepts a list of options that are either strings or maps with of value and label keys. If the list members are strings, the value will be used for both the value and label of the <option> element it represents.

options are expected to be an enumerable which will be used to generate each respective option. The enumerable may have:

  • keyword lists - each keyword list is expected to have the keys :key and :value. Additional keys such as :disabled may be given to customize the option

  • two-item tuples - where the first element is an atom, string or integer to be used as the option label and the second element is an atom, string or integer to be used as the option value

  • atom, string or integer - which will be used as both label and value for the generated select

Optgroups

If options is a map or keyword list where the firs element is a string, atom, or integer and the second element is a list or a map, it is assumed the key will be wrapped in an <optgroup> and teh value will be used to generate <options> nested under the group. This functionality is only handled in the UI for select fields, boolean groups will not respond.

This functionality is equivalent to Phoenix.HTML.Form.select/3

Link to this function

value_for(field, model, type)

View Source
@spec virtual(t()) :: t()

Mark a field as virtual, which will update how teal queries the database for the field.

This is relevant for fields that are based on schemaless queries in resources that represent complex queries in read-only situations. For example, if a resource has a records definition like:

from(p in "posts", select: %{user_id: p.user_id, count: count(p.id)}, group_by: p.user_id)

Sorting by that resource would fail without marking the Number.make(:count) field as virtual. Virtual fields allow Teal to sort by computed fields that are not backed by a direct database reference, assuming the field is named with Ecto.Query.API.selected_as/2:

from(
  p in "posts",
  select: %{user_id: p.user_id, count: p.id |> count() |> selected_as(:count)},
  group_by: p.user_id
)

A resource can then use Number.make(:count) |> Field.virtual() to mark the field as virtual.