Backpex.Fields.InlineCRUD (Backpex v0.16.3)
View SourceA field to handle inline CRUD operations. It can be used with either an embeds_many or has_many (association) type column.
Field-specific options
See Backpex.Field for general field options.
:type- Required. The type of the field.:child_fields(keyword/0) - Required. A list of input fields to be used. Currently only supportBackpex.Fields.Textfields.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 theinput_typeoption. We currently support:textand:textarea. Theinput_typedefaults 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
)
...
endImportant
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, and on_delete: :delete_all to be able to delete a resource with existing items. It is recommended that you also add on_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
Summary
Functions
Returns the schema of configurable options for this field.