Populating Selection Tag from Repeater Data

Na Hallöchen Ninja!

Yea your name definitely isn’t unfamiliar to me. I’ve been studying your examples extensively for the past 8 months.

You really helped me out for the first time from this post: https://www.wix.com/velo/forum/coding-with-velo/how-can-i-create-multi-checkbox-filter-similar-to-the-wix-store-prebuild-filter

And that’s of course where you probably recognize your code and syntax from!

So it’s been a busy past couple of weeks, and I’ve managed to compile something that surprisingly works! Here is everything…

(the ZShield is how I inactivate items in the database that are currently out of stock, so they get removed from the repeater)


import wixData from 'wix-data';
import wixWindow from 'wix-window';

//-------- USER-INTERFACE -------------------------------------------------
var DBFIELDS = []
var DATABASE = "Edgebanding"
var DBLIMIT = 1000
var REPEATER = "EdgeRepAll"
//-----------------------------
DBFIELDS[0] = "lengths";       
DBFIELDS[1] = "finishes"; 
DBFIELDS[2] = "zShield"; 
//-------- USER-INTERFACE -------------------------------------------------

$w.onReady(()=>{
   $w('#TheShield').hide()  
   wixData.query(DATABASE)
  .limit(DBLIMIT)
  .find()
  .then(async(results)=>{console.log(results.items);
     populateRepeater($w('#LengthsST').value, $w('#FinishesST').value, $w('#TheShield').value);
      populatesTags("LengthsST", DBFIELDS[0], DBFIELDS[1], $w('#FinishesST').value, $w('#TheShield').value);
      populatesTags("FinishesST", DBFIELDS[1], DBFIELDS[0], $w('#LengthsST').value, $w('#TheShield').value);
  });
  

  $w("#EdgeRepAll").onItemReady (async($item, itemData) => {
      $item("#repTitle").text = itemData.title;
      $item("#repImage").src = itemData.image;

    $item("#repImage").onClick(()=>{
        const repeaterItem = itemData;
        wixWindow.openLightbox("Edgebanding Product",repeaterItem)
      });
      
    $item("#arrow").onClick(()=>{
        const repeaterItem = itemData;
        wixWindow.openLightbox("Edgebanding Product",repeaterItem)
      });
   });

$w('#LengthsST, #FinishesST').onChange(()=>{
      $w("#LengthsST").value =[$w("#LengthsST").value[$w("#LengthsST").value.length -1]];
      $w("#FinishesST").value =[$w("#FinishesST").value[$w("#FinishesST").value.length -1]];
      populateRepeater($w('#LengthsST').value, $w('#FinishesST').value, $w('#TheShield').value);
      populatesTags("LengthsST", DBFIELDS[0], DBFIELDS[1], $w('#FinishesST').value, $w('#TheShield').value);
      populatesTags("FinishesST", DBFIELDS[1], DBFIELDS[0], $w('#LengthsST').value, $w('#TheShield').value);
   });

   $w('#Reset').onClick(()=> {
      $w("#LengthsST, #FinishesST, #sTagTest").value = undefined; 
      populateRepeater($w('#LengthsST').value, $w('#FinishesST').value, $w('#TheShield').value)
      populatesTags("LengthsST", DBFIELDS[0], DBFIELDS[1], $w('#FinishesST').value, $w('#TheShield').value);
      populatesTags("FinishesST", DBFIELDS[1], DBFIELDS[0], $w('#LengthsST').value, $w('#TheShield').value);
   });
});

function populateRepeater(selectedOption1, selectedOption2, selectedOption3) {
 let dataQuery = wixData.query(DATABASE)
    .ascending("title");
 if (selectedOption1.length>0){dataQuery = dataQuery.hasSome(DBFIELDS[0], selectedOption1);}
 if (selectedOption2.length>0){dataQuery = dataQuery.hasSome(DBFIELDS[1], selectedOption2);}
 if (selectedOption3.length>0){dataQuery = dataQuery.ne(DBFIELDS[2], selectedOption3);}
    dataQuery.find()
 .then(results => {
    const filteredItems = results.items;
    $w('#'+REPEATER).data = filteredItems;
 })
}

function populatesTags(sTags, DBFIELD1, DBFIELD2, selectedOption1, selectedOption3) {
 let dataQuery = wixData.query(DATABASE)
    .ascending(DBFIELD1);
 if (selectedOption1.length>0){dataQuery = dataQuery.hasSome(DBFIELD2, selectedOption1);}
 if (selectedOption3.length>0){dataQuery = dataQuery.ne(DBFIELDS[2], selectedOption3);}
    dataQuery.find()
 .then(results => {
    const ITEMS = results.items;
    let uniqueTitles = [...new Set(getxxx(ITEMS).flat())]; 
      $w('#'+sTags).options = buildOptions(uniqueTitles);
      function getxxx(items) {
         const titlesOnly=items.map(item => item[DBFIELD1]); 
         return [...new Set(titlesOnly)];
      }
      function buildOptions(item) {
         return item.map(item => {
            return {label:item, value:item};
         });
      }
   })
}

Now there’s just one more puzzle piece to go… but that’s a separate post.

Huge thanks again to you and all you do for the community.