With a little ‘family help’, this query has now been resolved. The generic code below will search a specified database field using wildcard ? to represent any single letter, and * to represent multiple letters.
import wixData from ‘wix-data’;
$w.onReady(function () {
// Wait for data to upload;
});
export function button1_click(ev) {
// Get the query from the html form:
let query = $w(“#input1”).value;
// Sanitise the query to replace RegEx control characters. Not really
// necessary at the moment, but if you added any other search
// characters (such as +), then the query would need to be cleaned prior to
// creating the regex…
let sanitizedQuery = query.replace(/?/g, “@1”) // ? matches single character
.replace(/*/g, “@2”); // * matches any number of characters
// Replace sanitized query with RegEx equivalents. \S matches any
// non-space character.
let regex = new RegExp(sanitizedQuery.replace(/@1/g, “\S{1}”, “gi”)
.replace(/@2/g, “\S*”, “gi”));
// Fetch everything, then filter in JS (terribly inefficient, but if
// Wix db doesn’t support wildcards, then not much choice)
wixData.query(“your-database-name”)
.find() // get all records
.then( (result) => {
var filteredItems = ;
// Loop through every record, and match it against the regex pattern.
// If it matches, add it into the filteredItems array
for (var i = 0; i < result.length; i++) {
let item = result.items[i];
// Here, "title" is the field key used in your database
if (regex.test(item.title)) {
filteredItems.push(item);
}
}
// filteredItems now contains only matching items, so return them to
// wix / table on page.
$w("#table1").rows = filteredItems;
})
.catch( (error) => {
let msg = error.message;
let code = error.code;
console.error("Error running query: " + msg + "(" + code + ")");
});
}
Thanks to https://www.chrisblunt.com for the help.