Hi, I am having issues with my filter code. I basically wish to filter my repeaters by Type (hotel, house, ALL, etc), Guests, Bedrooms and Bathrooms. The last 3 have to display matches that are equal or more than, and the Type has to match House, Hotel (from database) or ‘ALL’ which lists all. In testing it seems like the Type dropdown is not working correctly.
MY SITE: http://tailoredwebdesign.wixsite.com/book-the-best/queensland1
The code does filter, but has errors:
- If you select guests 3, it does filter out to have properties showing with 3 or more, but if you then pick 2 bedrooms, it filters out all the ones with less than 2 BUT DOESN’T remember the guests selection.
MY FULL CODE:
import wixData from ‘wix-data’;
let originalPropertiesInfo = ;
let filteredResults = ;
$w.onReady(function () {
//Query to get the information from the database
wixData.query(“Properties”)
.find()
.then((results) => {
originalPropertiesInfo = results.items;
$w(#repeater2
).data = originalPropertiesInfo;
})
.catch((err) => {
let errorMsg = err;
});
//Set the information to the repeater
$w(#repeater2
).onItemReady(($w, itemData) => {
//add here all the relevant elements of the repeater
//In this case, I’ve added a text and an image to the repeater
$w(#text52
).text = itemData.title;
$w(‘#image2’).src = itemData.images;
$w(#text53
).text = itemData.guests;
$w(#text50
).text = itemData.bedrooms;
$w(#text51
).text = itemData.bathrooms;
});
});
function filterResults() {
filteredResults = ;
const type = $w(‘#Type’).value;
const guests = $w(‘#Guests’).value;
const rooms = $w(‘#Rooms’).value;
const bathrooms = $w(‘#Bathrooms’).value;
if (type && type !== 'Choose type of accommodation') {
filteredResults = originalPropertiesInfo.filter(item => item.type === type);
}
if (guests && guests !== 'Choose number of guests') {
filteredResults = originalPropertiesInfo.filter(item => item.guests >= guests);
}
//after the first if statement, the results is filtered by state
if (rooms && rooms !== 'Choose number of beds') {
filteredResults = originalPropertiesInfo.filter(item => item.bedrooms >= rooms);
}
if (bathrooms && bathrooms !== 'Choose number of bathrooms') {
filteredResults = originalPropertiesInfo.filter(item => item.bathrooms >= bathrooms);
}
//results is filtered by both guests and rooms
Guests_change();
}
export function Guests_change(event) {
//filtering the results
//setting the data
$w(‘#repeater2’).data = filteredResults;
//setting the repeater elements
$w(‘#repeater2’).forEachItem(($w, itemData, index) => {
$w(`#text52`).text = itemData.title;
$w('#image2').src = itemData.images;
$w(`#text53`).text = itemData.guests;
$w(`#text50`).text = itemData.bedrooms;
$w(`#text51`).text = itemData.bathrooms;
});
}
export function refine_onClick(event, $w) {
filterResults();
$w(“#button6”).show();
}
export function button6_onClick(event, $w) {
$w(“#Type”).selectedIndex = 0;
$w(“#Guests”).selectedIndex = 0;
$w(“#Rooms”).selectedIndex = 0;
$w(“#Bathrooms”).selectedIndex = 0;
$w(“#button6”).hide();
filterResults();
}
Thanks a lot, I really need to get this sorted!