I have create a database to save templates of form submission. A user has to give the name of the template in case he wishes to save it. Everytime he creates a new template, it gets populated in the database (called template) and its title is populated in DROPDOWN.
What should happen when he selects one of the templates from Dropdown?
On selection of his previously saved dropdown, the other inputboxes should display the corresponding data which relates to the particular entry (as selected in dropdown).
What I have done: Populating the dropdown with new templates
My Issue: I am unable to fetch the data from database corresponding to the selected dropdown value. On top the user should see only his or her created templates.
Hi,
What you need to do is to take the value of the dropdown and query your database with it. Then populate the elements you use for the template with the result values.
Example:
$w("#myElement").onChange( (event) => {
let newValue = event.target.value; // "value of the selected item"
wixData.query("myCollection")
.eq("something", "newValue")
.find()
.then( (results) => {
if(results.items.length > 0) {
let firstItem = results.items[0]; //see item below
} else {
// handle case where no matching items found
}
} )
.catch( (err) => {
let errorMsg = err;
} );
});
Then, you need to pass the result item to the elements you display your templates in.
Dropdown values are created by the people. So I cannot identify a specific dropdown value and query database. User can select anything (whatever he created in past as name of template).
“something” or “newvalue”–> cannot be specified as you have done. These are user generated.
I used the below code, where I selected template. On selection of dropdown (which is basically title of the template database), a value from that index at (whatever position) is required in “input2” (textbox). But nothing happens…
export function dropdown4_click(event) {
let newValue = event.target.value; // “value of the selected item”
wixdata.query(“template”)
.eq(“something”, “newValue”)
.find()
.then( (results) => { if (results.items.length > 0) { let firstItem = results.items[3]; //see item below
$w(“#input2”).value=firstItem;
} else {
// handle case where no matching items found
}
} )
. catch ( (err) => { let errorMsg = err;
} );
}