How to create a search database with three fields

Hello,
On a test website for the moment I have a page where a table displays the data of a little database with ten companies for example and for the dropdown by list two another “databases”, for the city and the activity…
I was inspired by this tutorial:
https://www.wix.com/code/home/forum/wix-tips-and-updates/how-to-create-a-search-for-your-database
That works well for the list.

a search by keyword, it’s ok.

I have a search field by activity, it works too.

But the third field, search by city, doesn’t work :


I see that the research is done by activity but I do not know why.
I do not know how to code correctly …


  1. import wixData from “wix-data”;

  2. /* - Ok - field search by keyword with timing */

  3. export function formBusiness_keyPress(event, $w) {

  4. if (debounceTimer) {

  5. clearTimeout(debounceTimer); 
    
  6. debounceTimer = undefined; 
    
  7. }

  8. debounceTimer = setTimeout(() => {

  9. filter($w(‘#formBusiness’).value, lastFilterCompagnyName);

  10. }, 500);

  11. }

  12. /* - Ok - */

  13. $w.onReady(() => {

  14. loadListCity();loadListActivity();

  15. });

  16. let lastFilterCompanyName; /* for the search by keywords*/

  17. let lastFilterActivity; /Activity = field in the database for all activities : ListActivity/*/

  18. let lastFilterActivity2; /Activity2 = field in the company database : ListCompany/

  19. let lastFilterCity; /City = field in the database for all the city : ListCity/

  20. let lastFilterCity2; /field in the company database/

  21. let debounceTimer;

  22. /* */

  23. export function formActivity_change(event, $w) {

  24. filter(lastFilterActivity, $w(‘#formActivity’).value);

  25. }

  26. export function formCity_change(event, $w) {

  27. filter(lastFilterCity, $w(‘#formCity’).value);

  28. }

  29. /* -Ok - for the dropdown list with city*/

  30. function loadListCity() {

  31. wixData.query(‘ListCity’)

  32. .find()

  33. .then(res => {

  34.  let options = [{"value": '', "label": 'All city'}]; 
    
  35.  options.push(...res.items.map(city2 => { 
    
  36.  	return {"value": city2.city, "label": city2.city}; 
    
  37.  })); 
    
  38.  $w('#formCity').options = options; 
    
  39. });

  40. }

  41. /* -Ok - for the dropdown list with Activity*/

  42. function loadListActivity() {

  43. wixData.query(‘Activity’)

  44. .find()

  45. .then(res => {

  46.  let options = [{"value": '', "label": 'Tous activitys'}]; 
    
  47.  options.push(...res.items.map(activity2 => { 
    
  48.  	return {"value": activity2.activity, "label": activity2.activity}; 
    
  49.  })); 
    
  50.  $w('#formActivity').options = options; 
    
  51. });

  52. }

  53. /* the filters… */

  54. function filter(companyName, activity, activity2, city, city2) {

  55. if (lastFilterCompagnyName !== compagnyName || lastFilterActivity !== activity || lastFilterActivity2 !== activity2 || lastFilterCity !== city || lastFilterCity2 !== city2)

  56. {

  57. let newFilter = wixData.filter(); 
    
  58. /* - Ok - search by keyword */ 
    
  59.   if (companyName) 
    
  60.   newFilter = newFilter.contains('compagnyName', compagnyName) 
    
  61. /* - Ok - dropdown list search by activity */

  62. if (activity2)

  63.   newFilter = newFilter.contains('activity2', activity2); 
    
  64.  /* - Not work - dropdown list search by city...*/ 
    
  65. if (city2) 
    
  66.   newFilter = newFilter.contains('city2', city2); 
    
  67. /* #ListCompany name of the connexion between the database and the page*/

  68. $w('#ListCompany').setFilter(newFilter);	 
    
  69. lastFilterCompanyName = companyName;	 
    
  70. lastFilterActivity = activity; 
    
  71. lastFilterActivity2 = activity2;  
    
  72. lastFilterCity = city; 
    
  73. lastFilterCity2 = city2; 
    
  74.  }    
    
  75. }


Could you give me some hints please?
thank you
Have a goo day
Florent

Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor.

Hello,

after a few more tries, now the activity field does not work anymore and the search by keyword has problems : the list disappears…
Thank for your help.
Have a good day

Your filter() function has 5 parameters defined:

function filter(companyName, activity, activity2, city, city2) 

However, you are calling filter() with only two parameters:

filter($w('#formBusiness').value, lastFilterCompanyName);
filter(lastFilterActivity, $w('#formActivity').value);
filter(lastFilterCity, $w('#formCity').value);

You need to call the filter function with all 5 parameters. Otherwise, the values of the parameters are not properly defined and the filter of the collection will not work.

You should refer to the Example: Search a Database to see how to properly handle the filter parameters.

Hello,
Thank for the example. Now it’s ok for the search by keywords and by activity. It’s work properly.
But I have always a “bug” with the search by city : when I choose one of the city like “Bret” or another in the dropdown list, I see he try to find by activity as if there was only this filter…

What’s wrong with the filter ?
Thank
Have a good day
https://atelierinformatiqu8.wixsite.com/website-4

You are still not using the correct parameters for the filter() function.

Please understand that if you are going to work with code, you will need to familiarize yourself with basic coding concepts to accomplish what you want. There are a wealth of Javascript coding sites which will help you learn Javascript from basic to advanced - Javascript.info is a good one. You might want to refer to this discussion on functions .

Good luck

Hello,
" You are still not using the correct parameters for the filter() function . "
… I am not a developer but after multiple trials and errors I managed to add the search field by city.
Finally…
Thank


import wixData from “wix-data”;

$w.onReady(() => {
loadListActivities(); loadListCities();
});

/=== Filters===/
let FilterActivities;
let FilterActivity
let FilterCity;
let FilterCities;
let debounceTimer;

/* for search by keyword with timing */
export function formBusiness_keyPress(event, $w) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w(‘#formBusiness’).value, FilterActivity);
}, 500);
}

/===== export function ====/
export function formActivity_change(event, $w) {
filter(FilterActivities, $w(‘#formActivity’).value);
}
export function formCity_change(event, $w) {
filter2(FilterCities, $w(‘#formCity’).value);
}

/===== export function ====/

/* ===the filters… ===/
function filter(activities, activity) {
if (FilterActivities !== activities || FilterActivity !== activity)
{
let newFilter = wixData.filter();
/
- Ok - search by keyword /
if (activities)
newFilter = newFilter.contains(‘companyName’, activities);
/
- Ok - dropdown list search by activity */
if (activity)
newFilter = newFilter.contains(‘activity’, activity);

/* #ListCompany name of the connexion between the database and the page*/
$w(‘#ListAllCompanies’).setFilter(newFilter);
FilterActivity = activity;
FilterActivities = activities;
}
}

function filter2(city, cities) {
if (FilterCity !== city || FilterCities !== cities)
{
let newFilter2 = wixData.filter();
if (cities)
newFilter2 = newFilter2.contains(‘city’, cities);

/* ===#ListCompany name of the connexion between the database and the page===*/
$w(‘#ListAllCompanies’).setFilter(newFilter2);
FilterCity = city;
FilterCities = cities;

}    

}

//=====fonction list ====//

/* -Ok - for the dropdown list with Activity*/
function loadListActivities() {
wixData.query(‘ListActivities’)
.find()
.then(res => {
let options = [{“value”: ‘’, “label”: ‘All activities’}];
options.push(…res.items.map(activity => {
return {“value”: activity.activities, “label”: activity.activities};
}));
$w(‘#formActivity’).options = options;
});
}

/* -Ok - for the dropdown list with city*/
function loadListCities() {
wixData.query(‘ListCities’)
.find()
.then(res => {
let options = [{“value”: ‘’, “label”: ‘All cities’}];
options.push(…res.items.map(city => {
return {“value”: city.cities, “label”: city.cities};
}));
$w(‘#formCity’).options = options;
});
}
//=====fonction list ====//