View Source Backpex.Field behaviour (Backpex v0.8.2)
Behaviour implemented by all fields.
A field defines how a column is rendered on index, show and edit views. In the resource configuration file you can configure a list of fields. You may create your own field by implementing this behaviour. A field has to be a LiveComponent.
Example
def fields do
[
rating: %{
module: Backpex.Fields.Text,
label: "Rating"
}
]
end
Summary
Callbacks
This function will be called in the FormComponent
and may be used to assign uploads.
Determines whether the field is an association or not.
This function is called before the changeset function is called. This allows fields to modify the changeset.
The Backpex.Fields.HasMany
uses this callback to put the linked associations into the changeset.
The field to be displayed on index views. In most cases this is the name / key configured in the corresponding field definition. In fields with associations this value often differs from the name / key. The function will receive the field definition.
Will be used on edit views to render a form for the value of the provided item. This has to be a heex template.
Used to render the readonly version of the field.
Used to render form on index to support index editable.
Will be used on index and show views to render a value from the provided item. This has to be a heex template.
The schema to be used in queries. In most cases this is the schema defined in the resource configuration. In fields with associations this is the schema of the corresponding relation. The function will receive the field definition and the schema defined in the resource configuration.
Defines the search condition. Defaults to an ilike condition with text comparison. The function has to return a query wrapped into a Ecto.Query.dynamic/2
which is then passed into a Ecto.Query.where/3
.
Functions
Defines Backpex.Field
behaviour and provides default implementations.
Gets alignment option for label.
Returns a map of types from a list of fields used for the Ecto changeset.
Defines debounce timeout value.
Handles index editable.
Checks whether index editable is enabled or not.
Defines placeholder value.
Determines whether the field should be rendered as readonly version.
Defines throttle timeout value.
Callbacks
@callback assign_uploads(field :: tuple(), socket :: Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()
This function will be called in the FormComponent
and may be used to assign uploads.
Determines whether the field is an association or not.
before_changeset(changeset, attrs, metadata, repo, field, assigns)
View Source@callback before_changeset( changeset :: Phoenix.LiveView.Socket.t(), attrs :: map(), metadata :: keyword(), repo :: module(), field :: tuple(), assigns :: map() ) :: Ecto.Changeset.t()
This function is called before the changeset function is called. This allows fields to modify the changeset.
The Backpex.Fields.HasMany
uses this callback to put the linked associations into the changeset.
The field to be displayed on index views. In most cases this is the name / key configured in the corresponding field definition. In fields with associations this value often differs from the name / key. The function will receive the field definition.
@callback render_form(assigns :: map()) :: %Phoenix.LiveView.Rendered{ caller: term(), dynamic: term(), fingerprint: term(), root: term(), static: term() }
Will be used on edit views to render a form for the value of the provided item. This has to be a heex template.
@callback render_form_readonly(assigns :: map()) :: %Phoenix.LiveView.Rendered{ caller: term(), dynamic: term(), fingerprint: term(), root: term(), static: term() }
Used to render the readonly version of the field.
@callback render_index_form(assigns :: map()) :: %Phoenix.LiveView.Rendered{ caller: term(), dynamic: term(), fingerprint: term(), root: term(), static: term() }
Used to render form on index to support index editable.
@callback render_value(assigns :: map()) :: %Phoenix.LiveView.Rendered{ caller: term(), dynamic: term(), fingerprint: term(), root: term(), static: term() }
Will be used on index and show views to render a value from the provided item. This has to be a heex template.
The schema to be used in queries. In most cases this is the schema defined in the resource configuration. In fields with associations this is the schema of the corresponding relation. The function will receive the field definition and the schema defined in the resource configuration.
@callback search_condition( schema_name :: binary(), field_name :: binary(), search_string :: binary() ) :: Ecto.Query.dynamic_expr()
Defines the search condition. Defaults to an ilike condition with text comparison. The function has to return a query wrapped into a Ecto.Query.dynamic/2
which is then passed into a Ecto.Query.where/3
.
Example
Imagine the underlying database type of the field is an integer. Before text comparison in an ilike condition you have to cast the integer to text.
The function could return the following query to make the field searchable.
dynamic(
[{^schema_name, schema_name}],
ilike(fragment("CAST(? AS TEXT)", schema_name |> field(^field_name)), ^search_string)
)
Functions
Defines Backpex.Field
behaviour and provides default implementations.
Gets alignment option for label.
Returns a map of types from a list of fields used for the Ecto changeset.
Defines debounce timeout value.
Handles index editable.
Checks whether index editable is enabled or not.
Defines placeholder value.
Determines whether the field should be rendered as readonly version.
Defines throttle timeout value.