Send checked/selected data from repeater to a 2nd repeater on the same page

Hi everyone!

I am creating an Order Request form (no payment transaction taking place), and I have a repeater with checkboxes. When the user selects an item from the repeater to add to their order, a Quantity field appears so they can select the number of items they want included in their order. They can select/check many different items, and then pick the quantity of each different item to add to their order.

I would then like to pass this info into another repeater, like a “cart”, where they can review their order selections before confirming.

I want to send the info from each checked row of the 1st repeater (named: “repeaterCurrentAvailability”) into a 2nd repeater (named: “repeaterOrderReview”) for the user’s review.

Currently, my code is retrieving each of the selected items, and I can see each different item correctly in the console log, but when I retrieve it back to pass along to the 2nd repeater (“repeaterOrderReview”) for user’s order review, only the last/most recently selected item appears in the 2nd repeater.

I know I’m likely missing something rather simple, but have been trying everything I can find/think of, and my coding experience is self-taught so I know it’s not always perfect. PLEASE HELP!

I am currently using session storage to accomplish this. And it half-works, but like I said, my 2nd repeater where the user would review their order is only showing the last/most recently selected item from the whole order.

See code & console snippets below:

THIS IS THE CODE I AM USING TO “SAVE” THE CHECKED/SELECTED ORDER ITEMS TO SESSION STORAGE:

export function buttonPlaceOrder_click(event) {
    $w('#repeaterCurrentAvailability').forEachItem( ($item, itemData, index) => {

    let isCheckedReview = $item('#checkboxAddToOrder').checked;
    if(isCheckedReview===true){
      let orderFlower = $item('#textTypeFilter').text; 
      let orderFlowerPrice = Number($item("#price").value);
      let orderFlowerColor = $item('#textColor2').text;
      let orderQtyAvailable = Number($item('#qtyAvailable').text); 
      let orderQtyRequested = Number($item("#qtyRequested").value); 
      let orderItemSubtotal = Number($item("#orderSubtotal").value); 
      let orderQtyRemaining = Number($item("#quantityRemaining").text);

      session.setItem("orderFlower", orderFlower);
      session.setItem("orderFlowerPrice", orderFlowerPrice);
      session.setItem("orderFlowerColor", orderFlowerColor);
      session.setItem("orderQtyRequested", orderQtyRequested);
      session.setItem("orderItemSubtotal", orderItemSubtotal);
      session.setItem("orderQtyRemaining", orderQtyRemaining);

      console.log("orderFlower: " + orderFlower);
      console.log("orderFlowerPrice: " + orderFlowerPrice);
      console.log("flowerColor: " + orderFlowerColor);
      console.log("quantityRequested: " + orderQtyRequested);
      console.log("quantityRemaining: " + orderQtyRemaining);
      console.log("itemSubtotal: " + "$" + orderItemSubtotal);
     }
     else{
       console.log("N/A")
     }
    })
    setOrderReviewRepeater();
    }

And this is what I see in the console.log during the “setItem” part of the code, where it looks like it is saving the info from each checked/selected row of the 1st repeater for the session … it seems as if this is working properly?

A) FIRST SELECTED ITEM:

B) SECOND SELECTED ITEM:

C) THIRD SELECTED ITEM:

Then this is the code I am using to TRY to populate the 2nd repeater (named: “repeaterOrderReview”) FROM THE SESSION STORAGE

export function setOrderReviewRepeater() {
  $w('#repeaterOrderReview').forEachItem( ($item, itemData, index) => {
      let orderFlower = session.getItem("orderFlower");
      let orderFlowerPrice = session.getItem("orderFlowerPrice");
      let orderFlowerColor = session.getItem("orderFlowerColor");
      let orderQtyRequested = session.getItem("orderQtyRequested");
      let orderItemSubtotal = session.getItem("orderItemSubtotal");
      let orderQtyRemaining = session.getItem("orderQtyRemaining");
    
      console.log("orderFlowerReview: " + orderFlower);
      console.log("orderFlowerPriceReview: " + orderFlowerPrice);
      console.log("orderFlowerColorReview: " + orderFlowerColor);
      console.log("orderQuantityRequestedReview: " + orderQtyRequested);
      console.log("orderItemSubtotalReview: " + orderItemSubtotal);
      console.log("orderQuantityRemainingReview: " + orderQtyRemaining);

      $w("#textFlowerName").text = orderFlower;
      $w("#inputItemPrice").value = orderFlowerPrice;
      $w("#inputItemQty").value = orderQtyRequested;
      $w("#inputItemSubtotal").value = orderItemSubtotal;
})
}

But then as the code continues … And as you can see from this console screenshot, when I use “getItem” to add the checked/selected repeater row items to the 2nd repeater (“repeaterOrderReview”) only the last/most recent selection is shown, both in the console.log and within the 2nd repeater itself:

This is what I see on the website page preview, as you can see, the code populates the 2nd repeater (“repeaterOrderReview”) with only the most recent checked selection from the order, not each item checked, and then fills each row of the 2nd repeater with the same info over & over, rather than 3 different order items:

I hope this makes sense, and appreciate any help – I’ve been stuck on this last step of a very long bit of code for A LONG time – please help if you can, I really need to finish this project & send off to the client (and move on with my life)! Thanks in advance!!!

Update to add: I have also tried passing the info to a repeater within a lightbox, instead of a 2nd repeater on the same page … and I am having the same issue, that only 1 item is appearing in the repeater, not each of the checked/selected items

Any suggestions?

Annie, I don’t have a lot of time for follow-up with this, but I would like to point you in a direction that will solve your dilemma. Using multiple session storage items is not flexible enough to handle multiple records well.

The idea in this first snippet on the original page is to create a record object to update a record in a collection that I’m calling RepeaterData whose sole purpose is to hold the values of that repeater temporarily in an array field called repeaterData. You will need to obtain the _id value from that collection record. You could just continually override the value of that record.

import wixData from 'wix-data';

let Options = {
    "suppressAuth": true,
    "suppressHooks": true
};

$w.onReady(function () {
  $w("#btnSaveRepeaterData").onClick((event) => {
    let id = "9e4cb586-327e-4fa4-95fd-92d6a7629fa3";
        let RepeaterData = {"_id": id ,
        "repeaterData": $w("#repeater1").data};
        wixData.save("RepeaterData",RepeaterData,Options)
        .then(() => {

        })      
   })
});

Then on your cart page, you could do something like this to get it populated. You would be getting the data from that same record with your unique ID that you will have for that collection. It would be the same _id value for both of these. You will have to wrestle with the details, but, in principle, this should be a workable approach:

import wixData from 'wix-data';
$w.onReady(function () {
   let id = "9e4cb586-327e-4fa4-95fd-92d6a7629fa3";
   wixData.get("RepeaterData",id)
   .then((savedData) => {
    $w("#repeater1").data = savedData.repeaterData;
    $w("#repeater1").onItemReady(($item, itemData,index)={
      // your repeater populating code goes here.
    });         
  })
});

Bear in mind that having data saving and getting code on your front end page code is not ideal from a security standpoint, but that can be dealt with later when you have it working.

Anthony,

Thanks for your reply! What you’re saying about the session storage makes sense. I am still struggling to make this work with adjustments to the code you suggested however

  1. The repeaterData array is saving every item from 1st repeater into the array, not just the checked items … and i’m also having trouble populating the array data into a 2nd repeater still too

  2. While I understand the idea of overwriting the same temporary data in the repeater using the unique ID, even if I get this working, I will likely run into issues if multiple people are trying to use this functionality at the same time, right? Like they could theoretically overwrite each others order data?

Still seriously stuck, so if you have the time to help me dig into this a bit more i would be SO SERIOUSLY GRATEFUL :slight_smile:

Thanks again!

Annie, the hesitancy for folks to help with a post like this is that they immediately see that wading through the weeds and hashing out the details is going to take some time. Your points are well thought out and let me address one of them. You could solve the first problem by adding a blank array called checkedItems and looping through the saved repeaterData array to cull out the ones that are checked. The field name in the collection that I created is actually called “checked”; hence the “item.checked” condition.

    $w("#btnSaveRepeaterData").onClick((event) => {
    let id ="9e4cb586-327e-4fa4-95fd-92d6a7629fa3";
        let repeaterData = $w("#repeater1").data;
        let checkedItems = [];
        repeaterData.forEach((item) => {
            if (item.checked){
                checkedItems.push(item);
            }
        })
        let RepeaterData = {"_id":id"repeaterData":checkedItems};
        wixData.save("RepeaterData",RepeaterData,WriteOptions)
        .then(() => {

        })      
    }) 

Regarding the second point, I don’t have an answer for it at the moment. I wasn’t thinking worst case/multi-user situation when proposing the above. I will post again this weekend if I think of a solution.

It dawned on me that the solution to the second point would be a good task for, let’s have a drum roll … session storage. That’s how you would keep track of which RepeaterData record is being used in a given browser session. So, you would use wixData.insert instead of save since you want it to create a new record.

 $w("#btnSaveRepeaterData").onClick((event) => {
        let repeaterData = $w("#repeater1").data;
        let checkedItems = [];
        repeaterData.forEach((item) => {
            if (item.checked){
                checkedItems.push(item);
            }
        })
        let RepeaterData = {"repeaterData": checkedItems};
               wixData.insert("RepeaterData",RepeaterData,WriteOptions)
        .then((newRec) => {
           session.setItem("RepeaterDataId",newRec._id);
        })      
    })

Then on the cart page:

$w.onReady(function () {
  let id = session.getItem("RepeaterDataId");
  wixData.get("RepeaterData",id)
  .then((savedData) => {
    $w("#repeater1").data = savedData.repeaterData;
    $w("#repeater1").onItemReady(($item,itemData,index) =>{
      // your repeater populating code goes here.
     });         
   })
});