Booleans fill cell?

I was wondering, if it was possible to have a bunch of booleans that if true would enter the field name (separated by commas) into a specified cell?

I have a video library and want to be able to add tags automatically to the Tags field of each video if the boolean for the tags are selected as true.

I have a search feature on my video library page that is connected to the Tags field. Adding new to the database will be a lot easier if I could just browse and check off which tags should be added to the field instead having to remember/actively search which tags Iā€™ve used before.

I have no idea where to even start, or if itā€™s even possible, any help would be appreciated. <3

1 Like

I would add the onChange event to your checkboxes and then add a global var tags = ā€œā€; at the top of your code and then concatenate that string for each time you tick a checkbox and when you click update I would SetFieldValue and insert that var into the string field. You can then add commas in that string and then parse it when you want to show videos.

Are you getting the thoughts?

itā€™s possible and we can give an outline of how to do it.
actually wix-data has the hasSome() condition that is very similar to what you need.

but first - is the list of possible tags well known? or is it dynamic?
can a user add a tag that is not from the list?

Hey Andreas, Iā€™m not that good with code to understand much of what you said, thanks for replying though, maybe itā€™ll make more sense as I learn.

Hey Ziv, users cannot add tags or edit the data in any way. I may add more tags in the future as needed but as of right now I have a set of tags (booleans) to choose from and a text field for the tags to be listed in.

If you want help with the code I suggest you send a screenshot on the page where the video and the checkboxes are placed. And one screenshot of the fields in the collection that you want to store and also how you want the data to be stored. Then we might be able to help you faster. I can code but I need to understand how your page looks like and your collection before and how you want it to look after.

Andreas,

on the page where the video and the checkboxes are placed.
There are no checkboxes on the page, just in the collection. I think I miscommunicated, I donā€™t display any video itself, just a category and item page to list them so they can be purchased off site. Iā€™ll get some screenshots and explain what Iā€™m trying to do.

Here are screenshots via link due to my siteā€™s ā€˜NSFW Adult Themeā€™ and as detailed of a description I can give to explain what Iā€™m trying to do.

Database
https://snag.gy/HaVrGE.jpg
There are more fields than what is displayed, this is just a small sample.

There are a bunch of booleans which are my ā€œTagsā€, when I add a new video to the database (manually, not through the site, I may add a page for myself to add videos to the database now that I think of it. Would this be easier or needed to do what I want?), I want to easily click which ā€œTagā€ booleans represent the new entry (video) to true. After all the ā€œtagsā€ are selected, I want to auto generate the actual Tags field using the true booleanā€™s field Name and separate them with a comma and space.

The generated Tags field is used in my site for searching the category page and on the item page, displayed via text to show what tags each video has.

Category Page
https://snag.gy/fFIeTJ.jpg
Shows search bar that is used to search the Tags field on the category page.

Item Page
https://snag.gy/7bvaIB.jpg
Shows where the generated text using the Tags field appears on the page.

I would skip all booleans in data collection and just keep the text string field as tags.

On your add video page I would make checkboxes with the different tags and when checking one of them, lets say striptease I woud add that string to the values in that field. Then when you show them just split the string by commas and you get an array you can use when settings filters and more.

split

string guide

1 Like

Okay so a bit of progress. I managed to figure out how to add a value to a text field.

var tag1 = "Tag 1"
var tag2 = "Tag 2"
var tag3 = "Tag 3"

export function tag1_onChange(event) {
$w("#myCollection").setFieldValues( {
  "myField":  tag1,
} );
}

 export function tag2_onChange(event) { 
 $w("#myCollection").setFieldValues( {
  "myField":  tag2, 
 } );
 } 
 
  export function tag3_onChange(event) { 
  $w("#myCollection").setFieldValues( {
   "myField":  tag3, 
  } ); 
  } 

But Iā€™m not sure exactly how to concat the strings together and split themā€¦

Join the tags:
var allTags = tag1 + ', ā€™ + tag2 + ', ā€™ + tag3;
That would end up in : striptease, wowwow, oopsoops if those would be the tags :slight_smile: Store that using setFieldValue not setFieldValues .

$w(ā€œ#myCollectionā€).setFieldValue(ā€œtagsFieldā€: allTags)

Then you need to save that change using:
$w(ā€œ#myCollectionā€).save();

Split the tags:
Letā€™s say you get all tags from the collection to the variable tagItems.

var tagArray = tagItems.split(", ");

tag1 = tagArray[0];
tag2 = tagArray[1];
tag3 = tagArray[2];

Does that make any sense?

1 Like

I think it does. lol :slight_smile: Do I save the change with itā€™s own function or just put the code in the $w.onReady section of code?

So, would I change all the checkboxsā€™ code var to tagArray[X]?

FROM

 export function tag1_onChange(event) { 
 $w("#myCollection").setFieldValues( {
  "myField":  tag1, 
 } )
 } 

TO

 export function tag1_onChange(event) {
 $w("#myCollection").setFieldValues( {
  "myField":  tagArray[0], 
 } ); 
 } 

Iā€™m sorry, for more questions. Thanks for helping and putting up with me. :wink:

  1. i) when defining tags, do they have to be lower case (StripTease instead of striptease)
    ii) do multiple words need to be strung together? ( Strip\ Tease instead of striptease) or something similar?
    iii) what about symbols I have a couple tags with a forward slash or period/decimal in them could that be achieved with a backslash too (w ** /w or T ** .B ** .A ** .)?

EDIT: Answers

1 i) No they donā€™t have to be lowercase, they can be any case.
ii) & iii) Yes they can have symbols and spaces, they have to be defined and replaced as substrings.

CODE

var str = ( "tag\\1 + (, ) + tag\,2 + (, ) + tag\.3"
           + " + (, ) + tag//4 + (, ) + tag\-5")

var allTags = str.replace("\\", " ")("//","/")("\.",".")("\,",",")("\-","-").trim();

Should create a string that looks like this

tag 1, tag,2, tag.3, tag/4, tag-5

A little tip

When working with string manipulations test it and write your javascript outside WIX and test it first. Will speed up your testing. Tryout code pen.

I see I see, because it didnā€™t work that time. Iā€™ve edited the post with a valid string.

Okay so Iā€™ve figured out how to insert into the field onChange. But I donā€™t understand what you mean by this.

Store that using setFieldValue not setFieldValues .

$w(ā€œ#myCollectionā€).setFieldValue(ā€œtagsFieldā€: allTags)

Then you need to save that change using:

$w(ā€œ#myCollectionā€).save();
This is what I have;

$w.onReady(function () {

$w("#videoLibrary").setFieldValue("tags",  allTags);

$w("#videoLibrary").save();

});

This gives me the following error on load;
DatasetError: There is no current item

If I add it to the submitButton_onClick I get this error on click;
save operation failed: DatasetError: Operation (save) not allowed during save
and it adds the whole string of tags to the field, I assumed it was supposed to do that but I donā€™t understand the purpose.

If I donā€™t add any of that code, it enters the tag into the field, but I get this error clicking the Submit Button;
save operation failed: TypeError: Cannot read property ā€˜replaceā€™ of undefined

And finally, even though I can get it to enter a tag into the field, it will only enter one of the selected tags (seems to be the last selected), how do I get multiple tags into the one field?

Here is my (redacted) code, without the above code;

var str = "tag 1\, + tag\,2\, + tag\.3\,"
        + " + tag\/4\, + tag\-5" 

var allTags = str;

var tagItems = allTags;

var tagArray = tagItems.split('+');

var tag1 = tagArray[0];
var tag2 = tagArray[1];
var tag3 = tagArray[2];
var tag4 = tagArray[3];
var tag5 = tagArray[4];

export function tag1_onChange(event) {
$w("#myCollection").setFieldValues( {
  "myField":  tag1,
} );
} 

export function tag2_onChange(event) {
$w("#myCollection").setFieldValues( {
 "myField":  tag2, 
} ); 
}

export function tag3_onChange(event) { 
$w("#myCollection").setFieldValues( { 
 "myField":  tag3, 
} ); 
}  

export function tag4_onChange(event) {
$w("#myCollection").setFieldValues( {
 "myField":  tag2, 
} ); 
}

export function tag5_onChange(event) { 
$w("#myCollection").setFieldValues( { 
 "myField":  tag3, 
} ); 
}  

Am I doing this right?

Iā€™m still stumpedā€¦ any ideas?

Since you are setting ā€œmyFieldā€ value to only one tag value, you get only the last selected one.
In order for it to work, you need to join the values form all the checkboxes.
This is the simplest code to achieve that:

var allTagsArray = [];
if ($w("#tag1").checked) allTagsArray.push('tag1');
if ($w("#tag2").checked) allTagsArray.push('tag2');
if ($w("#tag3").checked) allTagsArray.push('tag3');
if ($w("#tag4").checked) allTagsArray.push('tag4');
if ($w("#tag5").checked) allTagsArray.push('tag5');
var allTagsString = allTagsArray.join(',');

$w("#myCollection").setFieldValue("myField", allTagsString);

Iā€™m assuming here that the checkbox names are ā€œtag1ā€, ā€œtag2ā€ etc.
Take this example and play with it - using regular JavaScript you can change how the values are stored. Right now they are comma separated, but you can change it to whatever you like.

Great canā€™t wait to try that, so do I replace all my tag# s to the checkbox names?

I actually have 84 different tags xD

Iā€™m sorry, but I posted the wrong code example, fixing itā€¦

1 Like

I fixed the code.
Again, it is not the ā€œcleanestā€ code or the shortest, but I think itā€™s the most understood.
You only need this code, nothing else. Put it in every checkbox onChange event handler (like in your code example above).
If your checkbox names are different, you can change my code accordingly (you can see the names in the properties panel).

2 Likes