No results on filter

Hi guys,
I have setup some filters on my repeater which work with dropdowns onChange.
I have finally worked out how to ‘reset’ each one to All but when I do this, it shows my ‘sorry there are no results’ box briefly before is shows the proper repeater results?? Can someone please help? Thanks

My page : https://pete092.wixsite.com/book-the-best/copy-of-queensland
&
Code below:

import wixData from ‘wix-data’;

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;
let location = $w(‘#location’).value;
if (location === ‘All’) {
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.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 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;
let type = $w(‘#Type’).value;
if (type === ‘All’) {
wixData.query(“Properties”)
.contains(“location”, “Queensland”)
.contains(“subLocation”, $w(‘#location’).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 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();
})
.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();
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
}

Hi,

When changing the location selection to all, the following line executes:

.contains("subLocation", $w('#location').value)  //$w('#location').value = "All"

Since none of the items contain “All” in the “subLocation” field, the query returns with 0 results.
Therefore the following will always execute when selecting “All”, ignoring the else statement:


if (results.items.length === 0) {
    $w('#box3').show();
} 

I believe you probably meant to add this if statement to the .then() after the second query.

Thanks Ido. But if there are no results I want this to show? Just not on the ‘All’ selection. Will this still work if I add to the .then statement?
I think it is also incorrect to query twice, is there another way to have written all this?

Hi,

When selecting “All” you should see a list full of items, therefore the “no items found” box should not pop up.
You can either move the if statement to the second then or use the following method to create different queries based on some condition:

import wixData from 'wix-data';

function createQuery () {
 let query = wixData.query("Properties");
 if (dropDown === all) {
    //no filters needed
    query.find().then((res) => {
      //after query
      })
  }
 else {
    query.contains(propertyName, string)
    query.eq("somethingElse", value)
    //..some more filters...
    
    query.find().then((res) => {
 //after query
    })
  }
} 

Thanks Ido. I tried putting the hide code on the 2nd query as below but it still shows the box? Could you please have a look? My page : https://pete092.wixsite.com/book-the-best/copy-of-queenslan d

  • Update, it does work THANK YOU!!
    There is a second delay though, would there be anyway to speed this up?
  1. Also, I would like to add my price sort code below, but to take into account the filtered results.

My code is:

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() 
	); 

}

Thanks @Ido

Hi,

I’m glad its working :slight_smile:

To speed up the action you can combine the two queries into one.
Use the method I showed in my last reply.