formex
formex copied to clipboard
Expand one empty collection item fields in the form without click on Add button
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?
Are you asking about a JS function, or the way to add item it in backend?
Making them on the backend would be better I think but ideally I'd like to have both options.
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 😄
Thanks, I'll try it out... As for the jQuery... well I used to love it long ago))