My dropdown lists the sort options in no particular order- is there a way to force the dropdown to list the results alphabetically?
I have zero code experience and I am shocked that I was even able to get this to work- I used other forum posts and Wix support videos to get everything to come together. Hopefully someone has an idea that can help!
Thank you for taking the time to share your knowledge!
See code on the page below, or visit the site at https://caledonia-chamber.com/find-a-business
import wixData from "wix-data";
$w.onReady(() => {
});
let lastFiltertitle;
let lastFiltercategory;
let debounceTimer;
export function ititle_keyPress(event, $w) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w('#ititle').value, lastFiltercategory);
}, 500);
}
export function icategory_change_1(event, $w) {
filter(lastFiltertitle, $w('#icategory').value);
}
function filter(title, category) {
if (lastFiltertitle !== title || lastFiltercategory !== category) {
let newFilter = wixData.filter();
if (title)
newFilter = newFilter.contains('title', title);
if (category)
newFilter = newFilter.contains('category', category);
$w('#dataset1').setFilter(newFilter);
lastFiltertitle = title;
lastFiltercategory = category;
}
}
$w.onReady(function () {
// Run a query that returns all the items in the collection
wixData.query("ChamberMembers")
// Get the max possible results from the query
.limit(100)
.find()
.then(results => {
// Call the function that creates a list of unique titles
const uniqueTitles = getUniqueTitles(results.items);
// Call the function that builds the options list from the unique titles
$w("#icategory").options = buildOptions(uniqueTitles);
});
// Builds an array from the "Title" field only from each item in
// the collection and then removes the duplicates
function getUniqueTitles(items) {
// Use the map method to create the titlesOnly object containing all the titles from the query results
const titlesOnly = items.map(item => item.category);
// Return an array with a list of unique titles
return [...new Set(titlesOnly)];
}
// Creates an array of objects in the form {label: "label", value: "value"} from the array of titles
function buildOptions(uniqueList) {
return uniqueList.map(curr => {
// Use the map method to build the options list in the format {label:uniqueTitle, value:uniqueTitle}
return {label:curr, value:curr};
});
}
});