Jump to content

Dynamically update product select drop-downs. Can it be done?

Recommended Posts

Posted

Site URL: https://www.gorgeousgelato.com/next-day-delivery/gelato-pint

I have a test page www.gorgeousgelato.com/automatic-gallery-test where I was able to pull firestore.onSnapshot query results and display them in a real-time gallery of images with this code (which works as intended):

// CREATE GALLERY
<div id="coolGallery" class='flavorsOfTheDayGallery'></div>

// FIND FLAVORS THAT ARE MARKED AVAILABLE
  let flavors = db.collection("flavorsOfTheDay").where("isAvailable", "==", true)
    .limit(16)
  
  // ORDER AVAILABLE FLAVORS ALPHABETICALLY AND BUILD EACH AN IMG TAG WITH SOURCE AND ALT TEXT
portlandsFlavors.orderBy('flavorName')
    .onSnapshot((querySnapshot) => {
    coolGallery.innerHTML = "";
        querySnapshot.forEach((doc) => {
          coolGallery.innerHTML += "<img class='flavorImage' src='"+doc.data().url+"' alt='"+doc.data().flavorName+"'>"
          
            console.log(doc.id, " => ", doc.data());
        });
    })

I have been trying to recreate this success for product select dropdowns '<select><option value="">"text"</option></select>'  where each 'value' and 'text'  would be equal to a flavorName derived from the firestore query (standard dropdown shown below). I can push the query results to the console.log only if I remove reference to 'selectDropdown.innerHTML' from the querySnapshot.

123533734_ScreenShot2021-08-19at11_04_10PM.png.88e552219b02944ff6ab26045addce4b.png

The goal is to update the dropdowns above with live code from a firestore .onsnapshot query

// Target select ID
let selectDropdown = document.getElementById('select-yui_3_17_2_1_1588782094999_71107-field');
  
  // Search for Portland's available flavors & limit to 16
  let dropdownOptions = db.collection("flavorsOfTheDay").where("isAvailablePortland", "==", true)
  .limit(16)
  
  // ALPHABETIZE AVAILABLE FLAVORS
  let alpha = dropdownOptions.orderBy('flavorName')
  .onSnapshot((querySnapshot) => {
    selectDropdown.innerHTML = ""; // .innerHTML cannot read
    querySnapshot.forEach((doc) => {
      selectDropdown.innerHTML += "<option value='"+doc.data().flavorName+"'>'"+doc.data().flavorName+"'</option>"
      
      console.log(doc.id, " => ", doc.data().flavorName);
    });
  })

The last thing to mention is that the lightbox modals (shown below) which hold the select dropdowns are not loaded into the DOM until product "Add to Cart" buttons are clicked. My attempts at using MutationObservers have been unsuccessful and I'm not certain I will be able to make changes even if the mutationObserver catches the addition of the lightbox-modal to DOM.

2100316494_ScreenShot2021-08-19at11_03_48PM.thumb.png.57e2430f497b8a8f6442f17cc6a6f4ca.png

  • 2 months later...
  • Replies 1
  • Views 1.2k
  • Created
  • Last Reply
Posted
On 8/22/2021 at 1:18 AM, cnfall said:

Site URL: https://www.gorgeousgelato.com/next-day-delivery/gelato-pint

I have a test page www.gorgeousgelato.com/automatic-gallery-test where I was able to pull firestore.onSnapshot query results and display them in a real-time gallery of images with this code (which works as intended):

// CREATE GALLERY
<div id="coolGallery" class='flavorsOfTheDayGallery'></div>

// FIND FLAVORS THAT ARE MARKED AVAILABLE
  let flavors = db.collection("flavorsOfTheDay").where("isAvailable", "==", true)
    .limit(16)
  
  // ORDER AVAILABLE FLAVORS ALPHABETICALLY AND BUILD EACH AN IMG TAG WITH SOURCE AND ALT TEXT
portlandsFlavors.orderBy('flavorName')
    .onSnapshot((querySnapshot) => {
    coolGallery.innerHTML = "";
        querySnapshot.forEach((doc) => {
          coolGallery.innerHTML += "<img class='flavorImage' src='"+doc.data().url+"' alt='"+doc.data().flavorName+"'>"
          
            console.log(doc.id, " => ", doc.data());
        });
    })

I have been trying to recreate this success for product select dropdowns '<select><option value="">"text"</option></select>'  where each 'value' and 'text'  would be equal to a flavorName derived from the firestore query (standard dropdown shown below). I can push the query results to the console.log only if I remove reference to 'selectDropdown.innerHTML' from the querySnapshot.

123533734_ScreenShot2021-08-19at11_04_10PM.png.88e552219b02944ff6ab26045addce4b.png

The goal is to update the dropdowns above with live code from a firestore .onsnapshot query

// Target select ID
let selectDropdown = document.getElementById('select-yui_3_17_2_1_1588782094999_71107-field');
  
  // Search for Portland's available flavors & limit to 16
  let dropdownOptions = db.collection("flavorsOfTheDay").where("isAvailablePortland", "==", true)
  .limit(16)
  
  // ALPHABETIZE AVAILABLE FLAVORS
  let alpha = dropdownOptions.orderBy('flavorName')
  .onSnapshot((querySnapshot) => {
    selectDropdown.innerHTML = ""; // .innerHTML cannot read
    querySnapshot.forEach((doc) => {
      selectDropdown.innerHTML += "<option value='"+doc.data().flavorName+"'>'"+doc.data().flavorName+"'</option>"
      
      console.log(doc.id, " => ", doc.data().flavorName);
    });
  })

The last thing to mention is that the lightbox modals (shown below) which hold the select dropdowns are not loaded into the DOM until product "Add to Cart" buttons are clicked. My attempts at using MutationObservers have been unsuccessful and I'm not certain I will be able to make changes even if the mutationObserver catches the addition of the lightbox-modal to DOM.

2100316494_ScreenShot2021-08-19at11_03_48PM.thumb.png.57e2430f497b8a8f6442f17cc6a6f4ca.png

When the lightbox show up, you can notice the body has additional class, you can observer attribute with filter "class" to detect the bahavior

BeyondSpace - Squarespace Website Developer

🖼️ Lightbox Studio (Enable Pinch/Zoom on lightbox - PDF Lightbox popup - ...) </>  🗓️ Delivery Date Picker (Date picker form field)
Gallery block 7.1 workaround </> No-code customisations for Squarespace

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.