# `Backpex.Fields.InlineCRUD`
[🔗](https://github.com/naymspace/backpex/blob/0.18.3/lib/backpex/fields/inline_crud.ex#L2)

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.

* `:type` - Required. The type of the field.

* `:child_fields` (`t:keyword/0`) - Required. A list of child input fields.

  The following fields are **not** supported:
  - `Backpex.Fields.HasManyThrough`
  - `Backpex.Fields.InlineCRUD`
  - `Backpex.Fields.Upload`

  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.

* `:live_resource` (`t:atom/0`) - The live resource of the association. When provided, a link to each item's show page is rendered in the read-only view.

### 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 {: .info}
>
> 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 {: .info}
>
> 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

# `config_schema`

Returns the schema of configurable options for this field.

This can be useful for reuse in other field modules.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
