formex icon indicating copy to clipboard operation
formex copied to clipboard

Expand one empty collection item fields in the form without click on Add button

Open igormisha opened this issue 5 years ago • 4 comments

What is the way to make one group of fields for adding new collection item to show up without need to click on the Add button?

igormisha avatar Feb 21 '19 17:02 igormisha

Are you asking about a JS function, or the way to add item it in backend?

jakub-zawislak avatar Feb 23 '19 12:02 jakub-zawislak

Making them on the backend would be better I think but ideally I'd like to have both options.

igormisha avatar Feb 23 '19 13:02 igormisha

Backend

You must add a new items to your struct with additional formex_id value. On the JS side I'm generating new values using UUID, but it can be also numbers "1", "2", .... or any other string.

If you are using Ecto, then you have to preload that association before adding new items.

Example with Ecto:

article = Repo.get!(Article, id)
|> Repo.preload(:comments)

article = Map.put(article, :comments, article.comments ++ [
  %App.Comment{
    content: "default content of a new comment", # optional
    formex_id: "1" # required. it may be any unique string
  },
  %App.Comment{
    content: "default content of yet another new comment",
    formex_id: "2"
  }
])

form = create_form(App.ArticleType, article)

JS

import { Collection } from 'formex'

Collection.addCollectionItemForm($('.formex-collection'))

Sorry for the jQuery dependency 😄

jakub-zawislak avatar Feb 23 '19 19:02 jakub-zawislak

Thanks, I'll try it out... As for the jQuery... well I used to love it long ago))

igormisha avatar Feb 23 '19 20:02 igormisha