Hello.
I need to submit all forms with a single button outside of the repeater.
I have a repeater with 5 items ($item).
Each item($item) is a form with several fields.
The repeater is connected to a collection “#datasetHere”.
When I use the code bellow the only item saved on the database is the first one, I want to save all 5 forms with a single button outside of the repeater.
export function buttonX_click(event) {
$w(“#datasetHere”).save()
};
Thank you.
1 Like
Does the data from the five items get saved to five separate records or to the same record?
@joaoguidev This code loops through the records sequentially, sets the dataset index, and then saves the data. The purpose of the async approach is to be sure that one record saves before the loop continues.
export function button1_click(event) {
SaveAll();
}
export async function SaveAll(){
for (var i = 0; i < 5; i++) {
await SaveData(i);
}
}
export async function SaveData(i){
await $w('#dataset1').setCurrentItemIndex(i);
await $w('#dataset1').save();
}
@tony-brunsman
Hello Anthonyb.
Thank you again for replying.
It is worth pointing out that each item of the repeater is a different form.
I’ve been trying with this but it also just save the first form:
export function button_click(event) { $w( " #myRepeater " ).forEachItem(($item, itemData, index) => { $w( " # dataset1 " ).save(); }); }
I also try this but with the same result. Just the first form of the repeater was saved:
export function button_click(event) { $w( " #myRepeater " ).forEachItem(($item, itemData, index) => { $w ( ’ #dataset1 ’ ). setCurrentItemIndex ( index );
$w( " # dataset1 " ).save(); }); }
I’ve been hours trying to make it work so thank you so much for helping me
@joaoguidev When you say “each item of the repeater is a different form”, that does mean you are saving data from the different repeater item “forms” to different fields in the same collection, right?
These other looping approaches could also work, but you still have to account for the time it takes for the setCurrentItemIndex and save functions to execute. In particular, the save operation is writing data to the server while the code continues to loop. That’s what the async approach addresses. I’ve tested the above code and it does work.
@tony-brunsman
Hello.
I ended up using wix-data instead of wix-dataset.
Thank you for everything.
Joao
@joaoguidev That would have been my choice too, but not everyone is comfortable with constructing queries.