Empty Table check

Hi, i filter on a dataset as follows

$w(“#dataset1”).setFilter( wixData.filter() .contains(“field1”, “value1”).ne(“field2”, “value2”))
.then( (results) => {
let items = results.items;
$w(“#table1”).rows = items;
});

how can I check if table1 returns empty?

Thanks and sorry for my bad english
Claudio

Hi Claudio,

The results are returned as an array. Just use the standard Javascript array property length to check the size of the array: items.length; If items.length === 0 , then the results are empty.

I hope this helps,

Yisrael

Hi Yisrael,
I tried to implement your suggestion but it does not work

$w(“#dataset1”).setFilter( wixData.filter() . contains(“field1”, “value1”).ne(“field2”, “value2”))
.then( (results) => {
let items = results.items;
if (items.length === 0)
{
$w(“#table1”).hide();
//console.log(‘stay here’)
}
$w(“#table1”).rows = items;
});

The used filter does not extract anything but does not enter in to IF statement .

If instead the control I put it outside, I do not recognize the variable “items”

Claudio

It may be that the dataset hasn’t fully loaded by the time your code runs.

Try placing your code inside the onReady() callback function. This will ensure that your code doesn’t run until the dataset is completely loaded.

You’ll want to do something like this:

$w("#dataset1").onReady( () => {
	$w("#dataset1").setFilter( wixData.filter() .contains("field1", "value1").ne("field2", "value2"))		
		.then( (results) => {   
			let items = results.items; 
			$w("#table1").rows = items;
		});
});

All right, but the " setFilter " result is always the initial number rows.

I’ll explain, initially the table contains 3 rows, when dropdown1 = ‘VEN’ i expect an empty table, but the length is always the initial number rows that is 3.

Thanks

	if ($w('#dropdown1').value === 'VEN')
	{
		$w("#dataset1").onReady( () => {
			
			$w("#dataset1").setFilter( wixData.filter() . contains("field1", "value1").ne("field2", "value2")) 		
			.then( (results) => {   
   			let items = results.items; 
   			$w("#table1").rows = items;});	
   			if ($w("#table1").rows.length === 0)
  			{
   			  console.log("empty table");   		
   			}

   		});
        .
        .
        .
        .
   

What I don’t understand is that you’re doing a query of the collection (by way of the dataset). If the filter allows 3 records returned, then that’s what you’re asking for. You may need to adjust your filter values.

I’ll explain, i would like to hide or collapse the table1, if the filter used extracts nothing

then

 if ($w('#dropdown1').value === 'VEN') 
 {
 // the dataset1 does not have this condition 
 // the table1 is empty on web page
 // I have to check if it's empty and...
 // ...hide or collapse table1
 }

Thanks

The dropdown list can assume different values, so the situation described above may happen