Populating selection tags with tags present in a dataset

I have created a dynamic list of items. each element has a “tag” field inside which I will insert keywords without a predefined list.
Therefore, I would like to be able to add several tags over time by making sure that the SelectionTag that I added on the page as a filter, sees the values ​​updated according to the tags present in that specific field of the Dataset, without therefore having to enter them by hand each time.

This is the dataset: #dataset1:

This is the field inside the dataset: “categoriaProdotto” containing the tags

This is the selectiontag: #selectionTagsID that works as a filter on the site page, whose values ​​I would like to update automatically in relation to all the tags present in the “categoriaProdotto” field

Thanks in advance for the help :slight_smile:

@lucafr-1411 You will need to get the unique values (tags) residing in the categoriaProdotto field. You could use the distinct function for that and populate the selection tag element options dynamically in the onReady event of the page.

Thanks but I didn’t understand. Would you be kind enough to report the code with the elements I provided?
Thank you!

The wixData API has to be used for something like this. There is nothing in the wix-dataset API that would be able to handle it. Since this is an example, I’m taking the liberty to run a query in the page code. For security’s sake, it’s something that normally should be put in a backend function and called from the page.

It sounds like the field and collection name are the same. If not, make the appropriate change. Have a look at the console.log to see the form that the results take in a distinct query. It’s a one dimensional array of distinct values.

With selection tag elements, you need to assign both the label (what shows on the tag) and the value. Often times, the value is the _id field. In this case it looks like it’s the same as the label. The javascript map function is used to create an array of options for the selection tag element that has a label and value property for each item.

import wixData from 'wix-data';
$w.onReady(function () { 
    wixData.query("categoriaProdotto")
    .distinct("categoriaProdotto")
    .then((results) => {
        console.log(results);
        let options = []
        options = results.items.map(item => {
            return {
                label: item,
                value: item
            }
        })
        $w("#selectionTags").options = options;
    })
});

Great! works :slight_smile: Thanks a lot!