View Source Backpex.Fields.InlineCRUD (Backpex v0.8.2)
A field to handle inline CRUD operations. It can be used with either an embeds_many
or has_many
(association) type column.
Options
:type
- The type of the field. Either:embed
or:assoc
.:child_fields
- A list of input fields to be used. Currently only supportBackpex.Fields.Text
fields.
You can add additional classes to child field inputs by setting the class option in the list of child_fields
.
The class can be a string or a function that takes the assigns and must return a string.
In addition, you can optionally specify the input type of child field inputs with the input_type
option. We currently
support :text
and :textarea
. The input_type
defaults to :text
.
Important
Everything is currently handled by plain text input.
EmbedsMany
The field in the migration must be of type :map
. You also need to use ecto's cast_embed/2
in the changeset.
Example
def changeset(your_schema, attrs) do
your_schema
...
|> cast_embed(:your_field,
with: &your_field_changeset/2,
sort_param: :your_field_order,
drop_param: :your_field_delete
)
...
end
Important
We use the Ecto
:sort_param
and:drop_param
to keep track of order and dropped items. Therefore, you need to use these options as well in your changeset. The name has to be<field_name>_order
and<field_name>_delete
.
HasMany (Association)
A HasMany relation does not require any special configuration. You can simply define a basic Ecto.Schema.has_many/3
relation to be used with the Backpex.Fields.InlineCRUD field.
Important
You need to set
on_replace: :delete
to be able to delete items, andon_delete: :delete_all
to be able to delete a resource with existing items. It is recommended that you also addon_delete: :delete_all
to your migration.
Example
@impl Backpex.LiveResource
def fields do
[
embeds_many: %{
module: Backpex.Fields.InlineCRUD,
label: "EmbedsMany",
type: :embed,
except: [:index],
child_fields: [
field1: %{
module: Backpex.Fields.Text,
label: "Label1"
},
field2: %{
module: Backpex.Fields.Text,
label: "Label2"
}
]
}
]
end