Database search options

I’ve got the database search working for 1 field with it populating a table from my database (Members). I’m wondering if it is possible to use the same search box to search multiple fields. Example: If someone enters Betty in the search box it returns all files with the name Betty. Or if someone enters July in the search box it returns all files with a birthday in July. (I have field keys of name, address, birthday, phone)

An alternative would be to have a dropdown box with the different fields to search. Example: Someone would enter the name Betty in the text box and then select ‘name’ in the dropdown and then click search button and it returns all files with the first name Betty. Or someone would enter July in the text box and then select ‘birthday’ in the dropdown and click search button and it returns all files with a birthday in July.

Which sounds easier and how should I do this?
This is my current code.
import wixData from ‘wix-data’;

$w.onReady(function () {

});

export function searchButton_onClick(event) {
wixData.query(‘Members’)
.contains(‘birthday’, $w(‘#textInput1’).value)
.find()
.then(res => {
$w(‘#table2’).rows = res.items;
});
}

Thanks!

1 Like

Hi,

you can search in multiple fields by combining queryies with WixDataQuery.or(…) .

For example:

wixData.query('Members')
  .contains('birthday', value)
  .or(wixData.query().contains('name', value))
  .or(wixData.query().contains('address', value))
  .find()

Please, let me know if you succeed or need any further assistance. And have fun Wix Coding!

Works great. Thank you for the answer. I had a feeling it could be done with the .or command but I was leaving out the wixData.query.

Thanks again!

I’m not sure if I need to post a new question or continue with this topic (it is related).

I’ve got a table populated from a search. Now I would like for the user to be able to click the row and be directed to a dynamic item page based on the results of the search. Is this possible?

My current code.

import wixData from ‘wix-data’;

$w.onReady(function () {

});

export function searchButton_onClick(event) {
wixData.query(‘Members’)
.contains(‘birthday’, $w(‘#textInput1’).value)
.or(wixData.query(‘Members’).contains(‘name’, $w(‘#textInput1’).value))
.or(wixData.query(‘Members’).contains(‘spouse’, $w(‘#textInput1’).value))
.find()
.then(res => {
$w(‘#table2’).rows = res.items;
});
}

I have a table I am searching on. Here is the code I am using. Can you tell me how and where if i want to add a second table element to search on using the same search box? Currently my search box is searching on the title of a document. My documents have numbers and I would like to search on that as well using the same search box.

import wixData from ‘wix-data’;

export function input1_keyPress_1() {
wixData.query(‘Letters’)
.contains(‘title’, $w(‘#input1’).value)
.find()
.then(res => {
$w(‘#table1’).rows = res.items;

}); 

}

Here is the code I was using to create a multiple search from a single search box on a data table: It is not working, however.

import wixData from ‘wix-data’;

$w.onReady(function () {

});

export function input1_keypress() {
wixData.query(‘Meditations’)
.contains(‘title’, $w(‘#input1’).value)
.or(wixData.query(‘Meditations’).contains(‘code’, $w(‘#input1’).value))
.or(wixData.query(‘Meditations’).contains(‘author’, $w(‘#input1’).value))
.find()
.then(res => {
$w(‘#table1’).rows = res.items;
});
}

Hi Jonathan,

While the syntax for your code appears correct, at the moment it is not possible to execute multiple ORs within a query using this method.
Thank you for bringing it into our attention.

To make this work change your code as per the example below:

import wixData from 'wix-data';
$w.onReady(function () { 
});
export function input1_keypress() {   
 wixData.query('Meditations')
  .or(wixData.query('Meditations').contains('title', $w('#input1').value))
  .or(wixData.query('Meditations').contains('code', $w('#input1').value))
  .or(wixData.query('Meditations').contains('author', $w('#input1').value))
  .find()
  .then(res => {   
     $w('#table1').rows = res.items;
   });
}

Please direct me for the code, (I want the table to disappear when there is no content in the input box, also if instead of dedicated search button, the ENTER key on the keyboard would work as the search button).

Thanks in advance!

Hi Imran,

  1. code for hiding table is pretty simple, just check for the number of rows in the table:
if ($w('#table1').rows.length === 0) {
    $w('#table1').hide();
}
  1. use onKeyPress for that.

Liran.

I am using the same code with 1 parameter search but it is not working…No result is displayed in the table…

import wixData from ‘wix-data’;

$w.onReady(function () {

});

export function searchButton_onClick() {
wixData.query(‘Member’)
.contains(‘name’, $w(‘#input1’).value)
.find()
.then(res => {
$w(‘#table1’).rows = res.items;
});
}

Hi,

Any errors or warnings in the console?

Hi Liran,

how about I make the result is going into a dynamic page. On this code

import wixData from 'wix-data';
$w.onReady(function () { 
});
export function searchButton_onClick() {   
  wixData.query('Member')
   .contains('name', $w('#input1').value)
   .find()
   .then(res => {   
      $w('#table1').rows = res.items;
    });
} 
 .then(res => { $w('#table1').rows = res.items; 

how can I make the result go into a dynamic page? thank you for the response :slight_smile:

could this code success multiple drop box too?

Hi Geo,

Not sure I understand what you’re trying to achieve. can you please elaborate?

yuanxintee,
Yes, you can use multiple search terms in database based on several drop downs.
See here .

Liran.

Hi Liram,

I want to put the result into a dynamic page, instead into the table. So after the customer put the item/id code the result will go into a dynamic page and show all the information of that particular item/id code.

If you have. let’s say, a text box called ‘text1’, you can use $w.onReady function inside your dynamic page:

$w.onReady(function(){
      wixData.query('Member').contains('name', $w('#input1').value)
      .find() 
      .then(res => { 
            if (res.items.length > 0) {
                  const firstItem = res.items[0];
                  $w('#text1').text = firstItem.code;
            }
      });
});

Please note that this is a general skeleton code, you need to adjust it to your page.

Liran.

Hi all,

Very interesting all this, but my database has MANY fields, is it possible to just say search everywhere instead of telling it EACH AND EVERY field to look into?

import wixData from ‘wix-data’;
export function searchinput1_onkeyPress() {

$w.onReady(function() {

wixData.query(“Inventaire”);
.contains( $w(‘’).value) //Obviously, this seems to be where the game will be played. It’s my ONLY red dot because I left the field blank
.find()
.then(res => {
$w(‘#table1’).rows = res.items;

Hi,

currently there is no way to search in every field, without listing the fields.

Thanks Giedrius ,

Hmmmm. :frowning: OK, so apparently I’ll have to list them all :-S

So just to make sure I got this right, for EACH field to search, if I have 30 fields (I have more than that, but just as an example), I’ll put 30 lines containing :

.contains(‘FieldName’, $w(’ #input1 ').value)

Right?

Can I put

.contains(‘FieldName’, $w(‘#input1’, ‘#input2’, ‘#input3’ (up to) ‘#input30’, ).value)

Hi I want to get result from database :

  1. The script searches a database of companies, with names, addresses, city, states. 3. The script displays all the companies that match the search results in the following order. 1. Company Name Address City State