Hi - I need to have a search function so users can search a dataset with their surname, and as a result the table number is displayed in the table below?
$w.onReady( function() { } ); import wixData from 'wix-data' export function input1_keyPress(event) { // Runs a query on the "TablePlan" collection wixData.get("TablePlan") // Query the collection for any items whose "Surname" field contains // the value the user entered in the input element .contains("Surname", $w("#input1").value) .find() // Run the query .then(res => { // Set the table data to be the results of the query $w("#table1").rows = res.items; });
Any help would be greatly appreciated!
Thanks!
Stevie,
With a couple adjustments, you should be close to having what you want. Use "get" only when you are searching in a collection for a particular ID. Instead, use "query", and it will allow you to refine the search using one of the functions. For this purpose, the startsWith function would be better.
export function input1_keyPress(event) { // Runs a query on the "TablePlan" collection wixData.query("TablePlan") .startsWith("Surname", $w("#input1").value) .find() // Run the query .then(res => { // Set the table data to be the results of the query $w("#table1").rows = res.items; }); }