Sort by price with multiple filters

I am using filters for my vacation rental site and it works great:

export function location_change() {
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.contains(“subLocation”, $w(‘#location’).value)
.contains(“type”, $w(‘#Type’).value)
.ge(“guests”, Number($w(“#Guests”).value))
.ge(“bedrooms”, Number($w(“#Rooms”).value))
.ge(“bathrooms”, Number($w(“#Bathrooms”).value))
.find()
.then((results) => {
$w(‘#repeater2’).data = results.items;
if (results.items.length === 0) {
$w(‘#box3’).show();
} else $w(‘#box3’).hide();
let value = $w(‘#location’).value;
if (value === ‘All’) {
$w(‘#box3’).hide();
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.find()
.then((results) => {
$w(‘#repeater2’).data = results.items;
});
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
}

export function Type_change() {
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.contains(“subLocation”, $w(‘#location’).value)
.contains(“type”, $w(‘#Type’).value)
.ge(“guests”, Number($w(“#Guests”).value))
.ge(“bedrooms”, Number($w(“#Rooms”).value))
.ge(“bathrooms”, Number($w(“#Bathrooms”).value))
.find()
.then((results) => {
$w(‘#repeater2’).data = results.items;
if (results.items.length === 0) {
$w(‘#box3’).show();
} else $w(‘#box3’).hide();
let type = $w(‘#Type’).value;
if (type === ‘All’) {
$w(‘#box3’).hide();
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.contains(“type”, “”)
.find()
.then((results) => {
$w(‘#repeater2’).data = results.items;
});
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
}

export function Guests_change(event, $w) {
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.contains(“subLocation”, $w(‘#location’).value)
.contains(“type”, $w(‘#Type’).value)
.ge(“guests”, Number($w(“#Guests”).value))
.ge(“bedrooms”, Number($w(“#Rooms”).value))
.ge(“bathrooms”, Number($w(“#Bathrooms”).value))
.find()
.then((results) => {
$w(‘#repeater2’).data = results.items;
if (results.items.length === 0) {
$w(‘#box3’).show();
} else $w(‘#box3’).hide();
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
}

export function Rooms_change(event, $w) {
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.contains(“subLocation”, $w(‘#location’).value)
.contains(“type”, $w(‘#Type’).value)
.ge(“guests”, Number($w(“#Guests”).value))
.ge(“bedrooms”, Number($w(“#Rooms”).value))
.ge(“bathrooms”, Number($w(“#Bathrooms”).value))
.find()
.then((results) => {
$w(‘#repeater2’).data = results.items;
if (results.items.length === 0) {
$w(‘#box3’).show();
} else $w(‘#box3’).hide();

		if (($w("#location").value === 'All')) { 
			$w("#location").selectedIndex === undefined; 
		} 
	}) 
	.catch((error) => { 
		let errorMsg = error.message; 
		let code = error.code; 
	}); 

}

export function Bathrooms_change(event, $w) {
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.contains(“subLocation”, $w(‘#location’).value)
.contains(“type”, $w(‘#Type’).value)
.ge(“guests”, Number($w(“#Guests”).value))
.ge(“bedrooms”, Number($w(“#Rooms”).value))
.ge(“bathrooms”, Number($w(“#Bathrooms”).value))
.find()
.then((results) => {
$w(‘#repeater2’).data = results.items;
if (results.items.length === 0) {
$w(‘#box3’).show();
} else $w(‘#box3’).hide();

		if (($w("#location").value === 'All')) { 
			$w("#location").selectedIndex === undefined; 
		} 
	}) 
	.catch((error) => { 
		let errorMsg = error.message; 
		let code = error.code; 
	}); 

}

What I would like to do is have a sort by price descending & ascending.

This code works perfectly:

export function dropdown1_change(event, $w) {
$w(“#dataset1”).setSort(
wixData.sort()
.descending(“price”));
if ($w(‘#dropdown1’).value === ‘low’)
$w(“#dataset1”).setSort(
wixData.sort()
.ascending(“price”));
if ($w(‘#dropdown1’).value === ‘none’)
$w(“#dataset1”).setSort(
wixData.sort()
);
}

However, it works only on the unfiltered data . I would like users to be able to combine it with my filters as above, so it works before you filter, then you filter and then you can sort the results as well.

Can you please let me know how I would go about that? Thank you!!

Hi TailoredWebDesign,

Consider using setFilter together with setSort (wix-dataset - Velo API Reference - Wix.com).
Extract the relevant filter from the code above, and use it on the dataset. This will provide you with a filtered and sorted dataset.

I hope this helps.
Idan.

Thank you Idan I’m having a weird delay issue when the repeater loads after filtering. Would u mind having a look?

@Idan, I’m not too clear on how you extract the filter. Could you provide more info and an example at all please?

Hi,

The sort by price code works on your dataset while the code on the other dropdown onChange events interacts directly on the collection, disregarding the dataset.

You should either change your sort event to directly access your collection (using wixData.query(“Properties”)… ) or change the other dropdown onChange events to interact with your dataset instead (using dataset.setFilter )