Question:
How can I display items from a dynamic dataset in a repeater based on if the item has the attribute
What are you trying to achieve:
I have a dynamic page and CMS collection for workout videos separated by the body area (e.g., ‘Core’, ‘Upper Body’).
I want to show only the videos that have the tag for the current page’s body area.
This is the full code that I used on a static page that worked, which showed the videos based on the attribute I explicitly named. (line 7)
$w.onReady(function () {
// Pull dataset info
const myRepeater = $w('#workoutVideosRepeater');
myRepeater.onItemReady(($item, itemData, index) => {
// Check if the item has the right tag
const isVisible = itemData.workoutType.includes('AUB'); // accessory upper body
// Set visibility for each item in the repeater
if (isVisible) {
$item('#container1').show(); // Show your specific element if it has the tag included above
$item('#container1').expand();
} else {
$item('#container1').hide(); // Hide your specific element if it doesn't have the tag included above
$item('#container1').collapse();
}
});
$w('#workoutVideosDataset1').onReady(() => {
// Get data from the dataset
const datasetData = $w('#workoutVideosDataset1').getCurrentItem();
// Set the data to associate with the repeater
myRepeater.data = datasetData;
});
});
Now, I’m trying to convert it to one Dynamic Page instead of making a separate page for each body area. But I can’t figure out how to pull the current dynamic page’s data and pull it through to the dataset.
What have you already tried:
I have already read the Wix API, looked through some similar topics. Here is my latest attempt:
$w.onReady(function () {
// Assign repeater on page to a constant
const myRepeater = $w('#workoutVideosRepeater');
// Assign CMS data to a dataset that connects to the repeater
$w('#workoutVideosDataset1').onReady(() => {
// Get data from the dataset for item
let datasetData = $w('#workoutVideosDataset1').getCurrentItem();
const workoutTypeTitle = datasetData.workoutType1;
console.log(workoutTypeTitle);
// Set the data to associate with the repeater
myRepeater.data = datasetData;
// Function that goes through each item in dataset
myRepeater.onItemReady(($item, itemData, index) => {
// const workoutTypeTitle = itemData.workoutType1; // this gets each item's workoutType when we want the dynamic page's items workoutType
// Check if the item has the right tag
const isVisible = itemData.workoutType.includes(workoutTypeTitle);
// Set visibility for the item
if (isVisible) {
$item('#container1').show(); // Show your specific element if it has the tag included above
$item('#container1').expand();
} else {
$item('#container1').hide(); // Hide your specific element if it doesn't have the tag included above
$item('#container1').collapse();
}
});
});
});
This code just pulls the first two videos, regardless of the workoutType1 tag.
Additional information:
I know I’m clearly missing something that should be simple! Feel free to point me to the documentation that I need to look at to fix my issue.