Question:
I have this website im currently working on
https://lavster.wixsite.com/website
Once the website loads, the list of vlog sites randomly sorts to avoid bias to new or alphabetically sorted names. But I want to try and get it to sort the data again after a filter is in place, for example selecting Vegas from the drop down.
import wixData from 'wix-data';
$w.onReady(async function () {
// Initialize the repeater with all items
let res = await wixData.query("VegasVlogs").find();
let allItems = res.items;
// Initialize the repeater with shuffled items
let shuffledArray = shuffleArray(allItems);
$w('#repeater7').data = shuffledArray;
// Add an event handler for the dropdown selection
$w('#dropdown1').onChange((event) => {
console.log('Dropdown selection changed:', event.target.value);
const selectedTag = event.target.value; // Get the selected tag
// Filter the items based on the selected tag
const filteredItems = allItems.filter(item => item.tags.includes(selectedTag));
// Shuffle the filtered items
const shuffledFilteredItems = shuffleArray(filteredItems);
// Update the repeater data with the shuffled filtered items
$w('#repeater7').data = []; // Clear existing data
$w('#repeater7').data = shuffledFilteredItems; // Update with filtered and shuffled items
});
});
function shuffleArray(dataArray) {
for (let i = dataArray.length - 1; i > 0; i--) {
let index = getRandomIndex(0, i);
const temp = dataArray[i];
dataArray[i] = dataArray[index];
dataArray[index] = temp;
}
return dataArray;
}
function getRandomIndex(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Product:
Wix Editor
What are you trying to achieve:
So I have managed to get the code working that randomly sorts the results in a repeater once the website is loaded, however when I select a filter from the tags in the dropdown, the order of the filtered results in the repeater is just how they have been entered into the CMS, I have tried many different combinations on the OnChange code for the dropdown but it doesnt ever sort it.
I have attached the code above that i have tried so far.
What have you already tried:
Different on click commands to try and get the data randomly sorted