Submitting multiple items to a database

Ok here is a good one, I have done this before in a Filemaker Database but not sure how to do it on wix:

I have a submit form where I input data for every sale in my office, that data is to be stored in a “sales” collection, that input form lets me select the customer through a dropdown referenced to my “costumers” collection, then I need to select the product from the “products” collection, then submit… So far so good… but the question here is: How can I do this but now being able to store more than one product for each sale?

I thought of creating another collection called “items” which serves as a multiple item referenced for the sale and at the same time referenced to the “products” collection. I think i could accomplish this with a button after selecting a product in the dropdown and then adding it to the items collection and showing it on a list.

Any help on how to code this or solve this would be much appreciated.

Thanks in advance

Hey
I would add a Dropdown with the available products you want the user to select from. Then add a Add Product button and in the click event for that button add that selected product to the sales data collection using insertReference function. But you will need to use the .New() function on the dataset when the form is loaded and get the promise back so you know the _id of the created sales item before allowing people to add products.

Like this maybe. when clicking the button


  wixData.insertReference("sales", "items", newlyCreatedId, $w("#dropdown").value) // Make sure product id is the value in the dropdown
  .then( () => {
    console.log("Reference inserted");
  } )
  .catch( (error) => {
    console.log(error);
  } );

When then click ADD PRODUCT it will insert the product selected as a reference in the multiple items field with field key items.

Sounds promising! Thanks Andreas

I am now suck here:

Any ideas?