- SOLVED- Loading code form backend to show 'All' on dropdowns.

Hi team, I have some working code which works well below
Backend

export function populateDropdownFilter(DropdownData, filteredField, filteredValue, valueField, ) {
let allItems = [];
return wixData.query(DropdownData)
       .eq(filteredField, filteredValue)
       .ascending('order')
       .find()
       .then((results) => {
if (results.totalCount > 0) {
let items = results.items;
               items.forEach((item) => {   
let oneItem = {
                       label: item.title,
                       value: item[valueField].toString()
                   }
                   allItems.push( oneItem)
               })
return allItems;
           }
return null
       })
}

Front end

import {populateDropdownFilter} from 'backend/utilitiesModule';

$w.onReady(async function () {
    $w('#courseCategoryDropdown').options = await populateDropdownFilter('DropdownData', 'category', 'Category', 'value')
 let courseOptions = $w('#courseCategoryDropdown').options;
    courseOptions.unshift({ "value": '', 'label': 'All Categories' });
    $w('#courseCategoryDropdown').options = courseOptions;
});

This all works, BUT I’m wondering how to tweak the next code so I can show ‘All’ in the dropdowns from the backend. The reason is that I have a variety of dropdowns across the site for searching and I can remove some code if I can just do this in the back end, code I have without any errors but doesn’t work is:

export function populateDropdownFilter(DropdownData, filteredField, filteredValue, valueField, ) {
 let allItems = [];
 return wixData.query(DropdownData)
        .eq(filteredField, filteredValue)
        .ascending('order')
        .find()
        .then((res) => {
 if (res.totalCount > 0) {
 let items = res.items;
                items.forEach((item) => {
 let options = [{
 "value": '',
 'label': 'All' + item.category
                    }];
                    options.push(...res.items.map(title => {
 return {
                            label: item.title,
                            value: item[valueField].toString()
                        };
                    }));
                })
 return null
            }
        })
}

Thank you!

This seems to work :slight_smile:

Backend

export function populateDropdownFilter(DropdownData, filteredField, filteredValue, valueField, ) {
 let allItems = [];
 return wixData.query('DropdownData')
        .eq(filteredField, filteredValue)
        .ascending('order')
        .find()
        .then((res) => {
 let options = [{
                value: '',
                label: 'Any'
            }];
            options.push(...res.items.map(data => {
 return {
                    value: data.title,
                    label: data.title
                }
            }));
 return (options);
        })
}

Frontend

import {populateDropdownFilter} from 'backend/utilitiesModule';

$w.onReady(async function () {
    $w('#courseCategoryDropdown').options = await populateDropdownFilter('DropdownData', 'category', 'Category', 'value')
});