Hi guys I’m stucked with a piece of code, we have a client who requires to show dropdowns on a custom dynamic product page, we made it using static code time ago, but the new requirements is that we need to generate dropdowns dinamically, because the client states that they have about 40 different options, so hardcoding 40 differents options is not feasible for us, could anyone give us a light on this?
Kind Regards,
wixData . query ( “Stores/Products” )
. eq ( ‘_id’ , session . getItem ( ‘productId’ ))
. find ()
. then (( r ) => {
**if** ( r.items.length > 0 ) {
$w ( '#text49' ). show ()
**if** ( 'OPTION1' **in** r.items [ 0 ]. productOptions ) {
$w ( '#dropdown1' ). show ()
**let** queryItems1 = r.items [ 0 ]. productOptions.FABRIC.choices
$w ( '#dropdown1' ). placeholder = 'SELECT OPTION1'
$w ( "#dropdown1" ). options = prepareDataForDropdown ( queryItems1 , "description" )
} **else** {
$w ( '#dropdown1' ). hide ()
}
**if** ( 'OPTION2' **in** r.items [ 0 ]. productOptions ){
$w ( '#dropdown2' ). show ()
**let** queryItems2 = r.items [ 0 ]. productOptions.LABEL.choices
$w ( '#dropdown2' ). placeholder = 'SELECT OPTION2'
$w ( "#dropdown2" ). options = prepareDataForDropdown ( queryItems2 , "description" )
} **else** {
$w ( '#dropdown2' ). hide ()
}
**if** ( 'OPTION0' **in** r.items [ 0 ]. productOptions ) {
$w ( '#dropdown' ). show ()
**let** queryItems = r.items [ 0 ]. productOptions.COLLAR.choices
$w ( '#dropdown' ). placeholder = 'SELECT OPTION0'
$w ( "#dropdown" ). options = prepareDataForDropdown ( queryItems , "description" )
} **else** {
$w ( '#dropdown' ). hide ()
}
**if** ( 'OPTION3' **in** r.items [ 0 ]. productOptions ){
$w ( '#dropdown3' ). show ()
**let** queryItems3 = r.items [ 0 ]. productOptions.SLEEVE.choices
$w ( '#dropdown3' ). placeholder = 'SELECT OPTION3'
$w ( "#dropdown3" ). options = prepareDataForDropdown ( queryItems3 , "description" )
} **else** {
$w ( '#dropdown3' ). hide ()
}
} **else** {
console . log ( 'Nothing to Show' )
}
**function** prepareDataForDropdown ( data , column ) {
**return** data . map ( item => {
**return** {
label : item[column ],
value : item[column ],
}
})
}
})
. catch (( err ) => {
console . log ( err );
});