Filter data in a range

Hi,
i need help with a code concerning the filter of a repeater.

The website is an escape room website. The repeater mentioned above is supposed to contain
a list of escape rooms (contained in a database called ‘‘stanze’’). Now, so far i managed to create a filter that filters the repeater in 4 ways: an input text and 3 dropdown lists.

Everything works fine, except one dropdown filter. This is because this dropdown filter is supposed to work differently. Infact, i want this dropdown to filter the rooms by number of players. Every room has a min. and a max. number of players, and i already created 2 different colums in my database (with the value as a number). The dropdown filter contains all numbers from 0 to the max number existing in the colum ‘‘max player’’, so that a user can select, for instance, the number 5, and the repeater will show all rooms that allow people to play in 5. Basically, i want to filter all elements that has the selected element between the value in min. colums and max. column.

So, it is days i’m trying to make it to work, but still no results. I’m not a developer, so i’m only referring to articles and posts found here on wix code support and wix code forum.

I’m now pasting the code i’m using now. Hope someone can help me solve the issue.

import wixData from "wix-data";

$w.onReady(function () {
	// Run a query that returns all the items in the collection
	wixData.query("Stanze")
		// Get the max possible results from the query
		.limit(1000)       
  		.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("#iRegione").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.regione);
    		// 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};  
		});
	}
});

$w.onReady(function () {
	// Run a query that returns all the items in the collection
	wixData.query("Stanze")
		// Get the max possible results from the query
		.limit(1000)       
  		.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("#iGenere").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.genere);
    		
    		// 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};  
		});
	}
});

let lastFilterCerca;
let lastFilterGenere;
let lastFilterRegione;
let lastFilterPlayers;
let debounceTimer;

// 
// searchbox
export function iCerca_keyPress(event, $w) {
	if (debounceTimer){
		clearTimeout(debounceTimer);
		debounceTimer=undefined;
		}
		debounceTimer=setTimeout(() => {
	
	filter2($w('#iCerca').value, lastFilterGenere, lastFilterRegione, lastFilterPlayers)
	//filter($w('#iCerca').value, lastFilterGenere, lastFilterRegione);
}, 500);
}

    
// filter for searchbar, genere and regione (giocatori still not working)
function filter2(title, genere, regione, giocatori) {
	if (lastFilterCerca!== title || lastFilterGenere !== genere || lastFilterRegione !== regione || lastFilterPlayers !== giocatori) {
	     let min;
	     let max;
	     let newFilter = wixData.filter();
         if (title)
         newFilter=newFilter.contains('title',title);
	     if (genere)
	     newFilter = newFilter.contains('genere', genere);
	    
	     if (regione)
	     newFilter = newFilter.contains('regione', regione);
	     //if (giocatori) {
	     
	     $w('#Stanze').setFilter(newFilter);
	     
	     lastFilterGenere=genere;
	     lastFilterCerca=title;
	     lastFilterRegione=regione;
	     lastFilterPlayers=giocatori;
	}
	}
function filterplayers (){
	
	if (debounceTimer){
		clearTimeout(debounceTimer);
		debounceTimer=undefined;
		}
		debounceTimer=setTimeout(() => {
	
	let numero = $w('#iGiocatori2').max ;
	let filtragioc=wixData.filter();
	filtragioc= filtragioc.le("maxGiocatori", numero);
$w("#Stanze").setFilter(wixData.filter(filtragioc)
		//.between('maxGiocatori', 1 , numero)
		
		);
	
}, 500);
	
	
}

// genere box
export function iGenere_change(event, $w) {
	filter2(lastFilterCerca, $w('#iGenere').value, lastFilterRegione, lastFilterPlayers);
}

//regione box
export function iRegione_change(event, $w) {
	filter2(lastFilterCerca, lastFilterGenere, $w('#iRegione').value, lastFilterPlayers);
}

//giocatori box (not working)
export function iGiocatori_change(event, $w) {
	filterplayers($w('#iGiocatori2'));
}

export function Filtra_change(event, $w) {
	//Add your code for this event here: 
	filterplayers();
}

I thank you in advance for those who will help me solve this issue. I please ask you to be specific in the explanation of the solution, as i’m pretty new to code. Would be better if you tell me exactly what to add or fix in the code above.

I also have the feeling that the way i worte the code is not the best, the most ‘‘optimized’’. If you think that the code written above is not written in a good way, and maybe you think that there’s a better and easier way to make it to work, don’t hesitate to tell it to me.

Thank you again,

Andrea

Hi Andrea,

The between() filter function is what you’re looking for. An example usage would be something like this:

let min = 1;  // get this value from the min column
let maz = 5; // get this value from the max column
let query = query.between("players", 1, 5); 

I hope this helps,

Yisrael

Hi Ysrael,

thank you so much for your answer. Sure it helps, but i’m afraid is not enough. I already found the between filter function, and i was expecting you to tell me about it.

Unfortunately, as i said above, i’m not very familiar with coding, so i would please ask you to go a little more in deep with it.

Where should i paste in my code the part of code you wrote? How do i connect the values to my min and max colum? In general, can you please explain the whole process?

I would be very grateful if you can integrate your answer with my code.

Thank you again for the help.

Andrea

Andrea,

Please understand that if you are going to work with code extensively in the product and not just the features in the user interface, you will need to familiarize yourself with basic coding concepts to accomplish what you want. We are happy to get you pointed in the right direction and get you started with what the code should look like, but you’ll need to take it from there.

You can start with the article How to Use Code to Let Your Users Search a Collection . The WixDataQuery API contains many example code snippets illustrating the various uses of filters and conditions.

You may also want to check out the WixArena - it’s a hub where you can look for Wix Code (and other) experts for hire.

Good luck,

Yisrael

Hi Ysrael,

thank you for the suggestion. i’ve been working on that, and now seems the filter is working correctly (not sure it is a smart and ‘‘optimized’’ method, but works!)

I’ll contact you in case i’ll occur in another issue.

Thank you so much

Andrea

Hi @yisrael-wix I too am trying to accomplish this task but want to define the range from a drop down.
What I am trying to accomplish is to allow a user to search a price range for rentals.
So what I have is a drop down with the price ranges (0-500, 501-1000, 1000 or higher).
I want to define the min and max by the value range in a dropdown. I am using multiple filters and referred earlier to your example on how to search a database, here is the code I have thus far, but it clearly isn’t communicating the min and max for the between filters:

function filter(title, bedrooms, guests, price) {
 if (lastFilterTitle !== title || lastFilterBedrooms !== bedrooms || lastFilterGuests !== guests || lastFilterPrice !== price){
 let newFilter = wixData.filter();
 if (title)
      newFilter = newFilter.contains('propertyName', title);
 if (bedrooms)
      newFilter = newFilter.ge('ofRooms', bedrooms);
 if (guests)
      newFilter = newFilter.eq('ofGuests', guests);  
 if (price)
      newFilter = newFilter.between('priceLow', price);
    $w('#dataset1').setFilter(newFilter);   
    lastFilterTitle = title; 
    lastFilterBedrooms = bedrooms;
    lastFilterGuests = guests;
    lastFilterPrice = price;
  }
}

This is a live site so I’m working on a page copy for now: https://editor.wix.com/html/editor/web/renderer/edit/e6c5231d-cd19-4fa9-bdda-f021323b43c5?metaSiteId=c94a0b40-101b-4c7a-bb05-dfd6e2eec815&editorSessionId=bc6f7268-6138-4b41-b6bb-1fd5e9da0c48&referralInfo=dashboard

Thank you SO much for any help you can provide.