Add an item to a repeater

Hi all, I take elements like texts and image from a repeater and I would put them into another repeater but I don’t know how to do it.I want to do something like a shopping cart but is really difficult. At the moment this is my problem.Can somebody help me to find the solution?Thanks

1 Like

Please someone could help me?

Your question is not clear enough.
Can you elaborate + upload screenshots with the desired repeaters ?

  • add details like if you use a database collection to feed the repeater etc…


The #results1 is the first repeater connected to the products database.When the user click on the button “Scegli” the product should go to another repeater situated at the top of the page (Heading 5 ) then here will be displayed the name and the quantity of the object with its price.What I want to know is how I can create and item in the second repeater with the data of the first one? P.S. I already know how to get the data from the first repeater when I click on the button.

OK. I don’t know why you use a 2nd repeater, if you display one item only there (a repeater is a multi-item pattern.
but let’s say you need it there from some reason, then all you need to do is to arrange the data retrieved from repeater 1 in an array of objects and assign it to repeater 2.
You can see here how to do it:
https://www.wix.com/corvid/reference/$w.Repeater.html#data

@jonatandor35 Can you give me an example because I didn’t understand the examples there.

@francescopolacci
so let’s say the user clicked on item#1 (you gets the data using onItemReady),
then you create an array like this:


let repeater2Data = [];
$w("#repeater1").onItemReady( ($item, itemData, index) => {
$item("#Scegli").onClick( (event) => ({
numberOfClickedItems += numberOfClickedItems;
//gether all your data
let repeaterObject = { "Prodetto": xxx, "Qt": yyy, "Prezzo": zzz } //put here the item relevant data
repeater2Data.push(repeaterObject);
repeater2Data.[repeater2Data.length - 1]._id = repeater2Data.length - 1;
$w("#repeater2").data = repeater2Data;
})
})

@jonatandor35 I made this code, everything work but when I click once on the button the repeater delete the last items and put the new one.I want all the two items in the repeater.
#results1 is the first repeater
#repeater1 is the second repeater

$w.onReady( function () {

$w("#results1").onItemReady(($w, itemData, index) => { 
    $w('#repeater1').data = [] 

    $w("#button34").onClick((event) => { 

let id1 = $w(‘#text47’).text;
let id2 = $w(‘#text25’).text;
let id3 = $w(‘#input1’).value;
console.log($w(“#text25”).text)
console.log($w(‘#input1’).value)
const Itemshop = [{
“_id”: id1,
“prodotto”: id2,
“quantità”: id3,
“prezzo”: ‘99’,
}]

        $w("#repeater1").data = Itemshop; 

        $w("#repeater1").onItemReady(($item, itemData1, ) => { 

            $item("#text50").text = itemData1.prodotto; 
            $item("#text51").text = itemData1.quantità; 
            $item("#text52").text = itemData1.prezzo; 
            console.log('va') 

        }) 

    }); 
}) 

})

@francescopolacci try this code:

let secondRepeaterData = [];
let itemShop = {};
$w.onReady(function () { 
$w("#results1").onItemReady(($item, itemData, index) =>{
$item("#button34").onClick((event) => {
let id1 = $item('#text47').text;
let id2 = $item('#text25').text;
let id3 = $item('#input1').value;  
itemShop = {
"_id": id1,
"prodotto": id2,
"quantità": id3,
"prezzo": '99'};
secondRepeaterData.push(Object.assign({},itemShop));
$w("#repeater1").data = secondRepeaterData;
$w("#repeater1").onItemReady(($itemRepeater, itemData1, index) =>{
$itemRepeater ("#text50").text = itemData1.prodotto;
$itemRepeater ("#text51").text = itemData1.quantità;
$itemRepeater ("#text52").text = itemData1.prezzo;
console.log('va')
}) 
}); 
})
})

P.S. delete the duplicates (Wix code box duplicates the #elements);

@jonatandor35 Thank you so much. Other thing do you know how to calculate the total with repeater elements and display it in a field out. The repeater is the repeater1 and the field I have to calculate is the price ‘prezzo’ in Italian. Thanks

@francescopolacci you can d it like this:
Let’s say that the array you used to populate repeater is called secondRepeaterData, the:

let  prezzoArray = [];
    secondRepeaterData.forEach((e) => {
        prezzoArray.push(e.prezzo)
    })
 const reducer = (accumulator, currentValue) => accumulator + currentValue;
 let sum = prezzoArray.reduce(reducer);
console.log(sum);

                  

Alternatively, you can use a simple loop:

let sum = 0;
for(let i = 0; i < secondRepeaterData.length; i++){
sum += secondRepeaterData[i].prezzo;
}
console.log(sum);

@jonatandor35 Thanks.It works with the second one.Other things’ use a vector image like a delete button in the repeaters it possible to eliminate the item that I click?

@francescopolacci yes, But you should add more description regarding what exactly you’re trying to achieve.

@jonatandor35 I only want, when I click a button on the second repeater, to delete the current item from the repeater.

@francescopolacci OK. So you want to remove a certain object from secondRepeaterData and re-populate the repeater;
To do that you can get the Id of the clicked item and then remove its object from the array.
You can use the filter() method for that:

secondRepeaterDate = secondRepeaterDate.filter(object => object._id !== itemData1._id);

Then re-populate the repeater.

I’ve corrected the line (pay attention)

@jonatandor35 It doesn’t work very well, is there another way?

@francescopolacci , have a look at these links:
https://stackoverflow.com/questions/10024866/remove-object-from-array-using-javascript
https://stackoverflow.com/questions/29997710/remove-object-from-array-of-objects