Hi, i was wondering whether its possible to code such that one of the tags in my selection tag is already chosen when my page loads. Currently, I have 3 tags where only 1 can be chosen any single time but I don’t want all of my gallery to be shown when a user enters the page.
With…
https://www.wix.com/velo/reference/$w/selectiontags/selectedindices
… you can define selected indices of your SELECTION-TAG-ELEMENT.
Chose the values inside of the element-array, which you want to be selected on start.
—>
let mySelectecIndex = [0];
$w("#mySelectedTags").selectedIndices=mySelectecIndex;
Currently i’m doing that but it does not filter my dataset although the button is selected. Is there any way to remedy this?
import wixData from 'wix-data';
$w.onReady(function () {
$w('#selectionTags1').selectedIndices = [1]
$w('#selectionTags1').onChange(() => {
let selectedTag = $w('#selectionTags1').value;
for (var i = 0; i < selectedTag.length - 1; i++) {
if (selectedTag.length > 1) {
selectedTag.shift();
}
}
setTimeout(() => {
$w('#selectionTags1').value = [];
$w('#selectionTags1').value = selectedTag;
}, 1)
let filter = wixData.filter();
if (selectedTag.length > 0) {
filter = filter.hasSome("arraystring", selectedTag);
}
$w('#dynamicDataset').setFilter(filter);
});
});
Where did you get this code?
Is this code from you?
This code looks some kind of familiar to me.
Question!
How to filter a dynamic dataset?
Always when i see → “dynamic-dataset” the first what i have on my mind is → showing 1-item-only.
So putting a filter onto a dynamic dataset makes not much sense, right?
Maybe you want to use a normal dataset, to do some filterings?
Or maybe i do missunderstand something.
However, i just took a quick look over your code and found some improvements…
import wixData from 'wix-data';
$w.onReady(function() {
let filter = wixData.filter();
$w('#dataset1').onReady(()=>{
$w('#selectionTags1').selectedIndices = [0];
$w('#selectionTags1').onChange(() => {
let selectedTags = $w('#selectionTags1').value;
for (var i = 0; i < selectedTags.length - 1; i++) {
if (selectedTags.length > 1) {
selectedTags.shift();
}
if(i===selectedTags.length - 1) {
$w('#selectionTags1').value = [];
$w('#selectionTags1').value = selectedTags;
console.log(selectedTags[0])
filter = filter.eq("title", selectedTags[0]);
$w('#dataset1').setFilter(filter);
}
}
});
});
});
But this version is not tested and it is generated for a normal dataset.
Try it out, does it work?
EDIT: 10min later found this one…
And here another version…
And here another older post…
Put all informations together and try to optimize the provided code, if it still not working 100%.
Hi thanks for the reply, I got the code from the links you put above.
I think you misunderstood what i wanted to achieve.
I wanted the page to load in a way where not everything in the dataset is shown but only those filtered by the selection tag that was already selected when the page loads. Thats why i tried using the code you provided above however my dataset did not filter according to the chosen selected tag.
let mySelectecIndex =[0];$w("#mySelectedTags").selectedIndices=mySelectecIndex;
import wixData from 'wix-data';
let dbField = "title"; //<---- change here the DATABASE-FIELD you want to use
$w.onReady(function() {
let filter=wixData.filter();
let mySelectecIndex =[0]; //<--- sets the selectionTag'S index to the first value
$w("#mySelectedTags").selectedIndices=mySelectecIndex; //<---
$w('#dataset1').onReady(()=> {
$w('#selectionTags1').onChange(()=>{
let selectedTags = $w('#selectionTags1').value; //<-- sets new value
//function for ONE-SELECTION-ONLY.....
for(var i=0; i<selectedTags.length-1; i++){
if(selectedTags.length>1){
selectedTags.shift();
}
if(i===selectedTags.length-1){
$w('#selectionTags1').value=[];
$w('#selectionTags1').value=selectedTags;
filter=filter.hasSome(dbField, selectedTags[0];
$w('#dataset1').setFilter(filter);
}
});
});
});