In Wix Studio, I have a dynamic item page linked to a CMS. How could I concatenate data from multiple columns of this same CMS into a single text field?
For example, on the dynamic item page of the CMS “projects”, I would like to retrieve the data from the “type”, “technic”, “duration” and “year” columns to display them on a single line separated by commas.
The problem is more how to access the dynamic Item Page data, rather than how to concatenate it.
More concretely: I have a CMS called “projects” which has the “type”, “technic”, “duration” and “year” columns. I would like this data to be displayed on the dynamic item page in a single line separated by commas.
This code works, but unfortunately it lists all the CMS entries rather than just filtering the entry of the current item linked to the current dynamic item page.
import wixData from 'wix-data';
$w.onReady(function () {
// Get the ID of the current item from the page context
let itemId = $w("#dynamicDataset").getCurrentItem()._id; // Make sure to replace "dynamicDataset" with the actual name of your dataset
// Retrieve data for the current item
wixData.get("projects", itemId)
.then((result) => {
// Concatenate values from CMS columns you want to display
let concatenatedValue = `${result.type}, ${result.technic}`;
// Add the value of "duration" only if it is not empty
if (result.duration) {
concatenatedValue += `, ${result.duration}min`;
}
concatenatedValue += `, ${result.year}`;
console.log("concatenatedValue:", concatenatedValue);
// Display the concatenated value in the "technicalSheet" text field
$w("#technicalSheet").text = concatenatedValue;
})
.catch((error) => {
console.error(error);
});
});
Got it. Glad you figured it out. You should also be able to use just $w("#dynamicDataset").getCurrentItem(); to get all the current data without needing to do an additional wixData.get()
So something like:
$w.onReady(function () {
// Get the ID of the current item from the page context
let item = $w("#dynamicDataset").getCurrentItem(); // Make sure to replace "dynamicDataset" with the actual name of your dataset
// Concatenate values from CMS columns you want to display
let concatenatedValue = `${item.type}, ${item.technic}`;
// Add the value of "duration" only if it is not empty
if (item.duration) {
concatenatedValue += `, ${item.duration}min`;
}
concatenatedValue += `, ${item.year}`;
console.log("concatenatedValue:", concatenatedValue);
// Display the concatenated value in the "technicalSheet" text field
$w("#technicalSheet").text = concatenatedValue;
});
Now I’m struggling to do the same thing in a repeater’s text field. The data is correctly loaded and concatenated, but only certain repeater items are correctly updated, the others keep the default text of the design.