Hi,
I have a dataset that has prices of products. I use another dataset to be able to save people and products. Now I’m looking for a solution that when a product is entered, the price of this product will be shown in a field, so that this price can be written to the dataset2.
The question is if you want to show the price in the People collection when view via the Data Manager (or via on-page dataset), or maybe you also want to query the People collection based on price (using code)? The solution will be different for these 2 cases.
So it depends. Because the price of a product may change over time, so I can ask if you want to show the current price or the price at the time the product got selected by the member.
Let’s say that you want to show the current price , then in the MemberSelction collection change the type of the product to be a Reference field that points to the Products collection.
When you save the member selections, put the product _id (ID) in the product field (for the product dropdown, use the _id’s as values).
On the Member area connect column 2 to product.price and run calculation for column 3.
If you want to show the price at the time the member selected it, make the product field (for the MemberSelction collection) a Number, and store the price there when the member clicks the OPSLAAN button.
I want to show te price when the member clicks “OPSLAAN”.
So that I can calculate the prices of different products and different times when a product has a price change.
It is true that the price can change over time. Then the prices have to stay that were in the past to keep the count right.
To be honest I just don’t understand what you mean by “If you want to show the price at the time the member selected it, make the product field (for the MemberSelction collection) a Number, and store the price there when the member clicks the OPSLAAN button.”
It’s very easy.
In the collection where you save the member selection, add another column named ‘price’, and once the member clicks OPSLAAN, query the Products collection, find the selected product, and store its price together with member selection.
For example, let’s assume that in dropdown2 you have the product IDs as values, then:
lets products = [];
$w.onReady(() => {
$w('#productsDataset').onReady(() => {
$w('#productsDataset').getItems(0,1000)
.then(r => products = r.items;)
})
$w('#productDropdown').onChange(({target}) => {
const product = products.find(p => p._id === target.value);
const price = product.price;
//then save the prce together with the selection
})
})