Hi all
I have read a few posts about enabling conditional or dependant dropdowns but I have failed to get it to work on my site.
I need:
- 2 drop downs, one is vehicle Make , the other is vehicle Models. The dropdown options for Models must be filtered based on Make selected. These are kept in a collection called ’ MakeModel’.
- Both dropdowns’ placeholder (onReady) values need to be pulled from a collection named ‘Users’ which holds current user info.
- When a new Make ( dropdownMake ) is selected the options ( newModels ) for the second drop down ( dropdownModel ) need to update based on the new selection (variable newMake ).
- The placeholder of the second dropdown also needs to change to the first available model option in the new filtered list ( newModels )
- Upon update and submission of these new selections values for Make and Model dropdowns need to be submitted to the ‘Users’ collection to update the user’s choice (this is currently managed via the UI not in code).
Please see my attempt at the above in code:
import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(function () {
let currentUserMake;
let currentUserModel;
let currentUserModelOptions;
let newMake;
let newModels;
//get current user make
wixData.query("Users")
.eq("_id", '${wixUsers.currentUser.id}')
.find()
.then((results) => {
currentUserMake = results.items.vehicleMake;
});
//apply current user make to dropdownMake value
$w('#dropdownMake').value = currentUserMake;
//get current user model
wixData.query("Users")
.eq("_id", '${wixUsers.currentUser.id}')
.find()
.then((results) => {
currentUserModel = results.items.vehicleModel;
});
//apply current user model to dropdownModel value
$w('#dropdownModel').value = currentUserModel;
//find current relevant models
wixData.query("MakeModel")
.eq("Make", currentUserMake)
.find()
.then((results) => {
currentUserModelOptions = results.items.model;
});
//apply current relevant models to dropdownModels options
$w('#dropdownModel').options = currentUserModelOptions;
//on change of make, update dropdownModel.options with result of wixData query for chosen make
$w('#dropdownMake').onChange(() => {
newMake = $w('#dropdownMake').value;
wixData.query("MakeModel")
.eq("Make", newMake)
.find()
.then((results) => {
newModels = results.items.model;
});
$w('#dropdownModel').options = newModels;
$w('#dropdownModel').placeholder = '- Please select -'; //Placeholder is first entry from updated model options list
$w('#dropdownModel').enable();
});
});
Note that I am also connecting the dropdowns to collections via the Wix UI so as to avoid having to do this in code (as it works well). Not sure if I need to disable this and then replicate in code to make this work.
Im fairly new to code so I suspect there are syntax issues as well. When run in preview mode the above code gives the error ‘Wix code SDK Warning: The options parameter that is passed to the options method cannot be set to null or undefined.’
It also doesnt work at all. The fields dont populate with current user selection of ‘Users’ dataset and when selecting a different Make (options for which have been added via the UI manually) the Models dropdown doesnt show anything its just blank with a red box around it.
Please help…
thanks