Hi,
I have many selectiontags and each with four tags.
Is there a way to sign each tag an incremental numerical value to database?
E.g.
SelectionTags1
tag1-> Numerical integer 0
tag2->1
tag3->2
tag4->3
If I just assign each their own value 0, 1, 2 and 3 then it still logged as “tags” in the database.
I want it to be pure numerical. Help is much appreciated.
Hi Peter,
There is a way to do it. You could use the selectedIndices property and write to a numeric field, but you obviously couldn’t connect the data directly to the dataset. Did you want to limit it to one choice per selectiontag element? If so, the following approach would work.
export function button3_click(event) {
let tag1 = $w("#selTags1").selectedIndices[0];
let tag2 = $w("#selTags2").selectedIndices[0];
let tag3 = $w("#selTags3").selectedIndices[0];
$w("#dataset1").new();
$w("#dataset1").setFieldValues({
"tags1": tag1,
"tags2": tag2,
"tags3": tag3
})
$w("#dataset1").save();
}
There would be a way to limit the selection to one per selection tag through code, but it would take some tinkering to get it right.
Thanks Anthony
I have used your suggestion in combination with single-selected tags. It workes like a charm.
However, I wonder it could be condensed a bit more. I have quite alot of selectiontags.
import wixData from ‘wix-data’ ;
$w.onReady( function () {
//TODO: write your page related code here…
//Single-selection of tags
let SelectionTags = $w( ‘SelectionTags’ );
SelectionTags.forEach(selectedTags => {
let prevSelectedValue = null ;
if (selectedTags.value.length === 1 ) {
prevSelectedValue = selectedTags.value[ 0 ];
} else if (selectedTags.value.length > 1 ) {
selectedTags.value = ;
// If multiple tags are selected by default, deselect all of them
//(since there’s no good reason to prefer one of them over the others).
}
selectedTags.onChange((event) => {
// Prevent deselecting the only selected tag.
//Radio buttons do not allow it so tags shouldn’t either.
if (!event.target.value || event.target.value.length === 0 ) {
// Re-apply the previously selected tag.
event.target.value = [prevSelectedValue];
} else {
event.target.value = event.target.value.filter(x => x !== prevSelectedValue);
// Replace the previously selected tag with the newly selected one.
prevSelectedValue = event.target.value[ 0 ];
}
//Change slides on selection smokeTags1
const currTagId=event.target.id;
if (currTagId=== “smokeTags1” ) {
if (prevSelectedValue === “No” ) {
$w( ‘#quizSlides’ ).changeSlide( 13 );
} else {
//Change slides on selection on smokeTags1 if “No” is NOT selected
$w( ‘#quizSlides’ ).next();
} //Change slides on selection trainTags6
} else if (currTagId=== “trainTags6” ) {
if (prevSelectedValue === “No” ) {
$w( ‘#quizSlides’ ).changeSlide( 23 );
}
else {
$w( ‘#quizSlides’ ).next();
} //Change slides on selection alcoholTags1
} else if (currTagId=== “alcoholTags1” ) {
if (prevSelectedValue === “No” ) {
$w( ‘#quizSlides’ ).changeSlide( 27 );
} else {
$w( ‘#quizSlides’ ).next();
}
} else {
//Change slides on selection
$w( ‘#quizSlides’ ).next();
}
})
})
});
export function button2_click(event) {
//Add your code for this event here:
//button2 is the submit button
let mindTags1 = $w( “#mindTags1” ).selectedIndices[ 0 ];
let mindTags2 = $w( “#mindTags2” ).selectedIndices[ 0 ];
let mindTags3 = $w( “#mindTags3” ).selectedIndices[ 0 ];
let mindTags4 = $w( “#mindTags4” ).selectedIndices[ 0 ];
let mindTags5 = $w( “#mindTags5” ).selectedIndices[ 0 ];
let mindTags6 = $w( “#mindTags6” ).selectedIndices[ 0 ];
let mindTags7 = $w( “#mindTags7” ).selectedIndices[ 0 ];
let mindTags8 = $w( “#mindTags8” ).selectedIndices[ 0 ];
let mindTags9 = $w( “#mindTags9” ).selectedIndices[ 0 ];
let smokeTags1 = $w( “#smokeTags1” ).selectedIndices[ 0 ];
$w( “#dataset2” ). new ();
$w( “#dataset2” ).setFieldValues({
“mindTags1” : mindTags1,
“mindTags2” : mindTags2,
“mindTags3” : mindTags3,
“mindTags4” : mindTags4,
“mindTags5” : mindTags5,
“mindTags6” : mindTags6,
“mindTags7” : mindTags7,
“mindTags8” : mindTags8,
“mindTags9” : mindTags9,
“smokeTags1” : smokeTags1
})
$w( “#dataset2” ).save();
}
Is there another way to allocate indices to each selectiontags and numerical values to database?
@tranenpeter Definitely, you could make this more concise and readable. Since you are using consistent naming conventions, you could use a for loop to go through all of the selection tag elements, access the value of the selectedIndices, the name of the selection tag, and the field name that would allow you to create the object that setFieldValues needs. The following code creates an array of the selection tags on the page:
let SelTags = $w("SelectionTags");
@tony-brunsman A big thanks to you Anthony. I managed to make it work. In case others wants to do the same.
export function button2_click(event) {
//Add your code for this event here:
//button2 is the submit button
let SelTags = $w( ‘SelectionTags’ );
$w( “#dataset2” ). new ();
SelTags.forEach(selectedTags => {
let selectedTagsIndices=selectedTags.selectedIndices[ 0 ];
const currTagId=selectedTags.id;
$w( “#dataset2” ).setFieldValue(currTagId, selectedTagsIndices);
})
$w( “#dataset2” ).save();
}