Backpex.Fields.InlineCRUD (Backpex v0.17.0)

View Source

A 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.

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, 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.

Functions

config_schema()

Returns the schema of configurable options for this field.

This can be useful for reuse in other field modules.