Array List, Contains : How to use?

Hey guys,

I need some help here…

I have a table containing tags for products “product_tags”
image

And I have a product table “products” that have a column of tags for each product (JSON field)

What I’m trying to do is to get a unique list of the “used” tag_ids in my products table…
For the above set of data I should get [“2”, “1”, “6”]

My way to go was:
1) add a multiple records query pulling the product Tags “qr_getProd_Tags”,
2) add an ArrayList “tagList_Array” and define its schema as “tag_id”: Number,
3) add a repeat “repeat_prods” looping through the products,
4) add a nested repeat “repeat_tags” looping through each prd_Tags column (JSON field),
5) check if “tagList_Array” contains the current “repeat_tags” $value (tag-id) and
6) if not, add this $value (tag-id) to “tagList_Array”

My problem is that step 5 (ArrayList.contains) always returns false
So, I went with the where formatter and solved it:

BUT the question is:
HOW THE ArrayList.contains() is supposed to be used?

Would it not work better if your prd_Tags value was comma separated rather than saved as an array?

We do this for categories for services, and store in the database as such:

Screenshot from 2023-11-25 18-45-15

This populates a repeat checkbox whereby we then set checked by splitting the array by the comma, using contains:

 dmx-bind:checked="traderProfileData.data.traderProfile.traderSubCategoryArray.split(',').contains(value)"

Giving us:

The checkbox input for the update form (may help to see an example):

<div id="repeatRelatedServices" is="dmx-repeat" dmx-bind:repeat="selectService.data.selectService" key="subCategoryServiceUUID">
                                <div class="checkbox-group" is="dmx-checkbox-group">
                                    <input class="form-check-input" type="checkbox" name="subCategoriesArray[]" dmx-bind:id="'checkbox'+id" dmx-bind:value="subCategoryServiceTitle" id="subCategoriesArray" dmx-bind:checked="traderProfileData.data.traderProfile.traderSubCategoryArray.split(',').contains(value)">
                                    <label class="form-check-label" dmx-bind:for="'checkbox'+id" id="subCategoryArray">{{subCategoryServiceTitle}}</label>
                                </div>
                            </div>
1 Like

Thanks for your reply @Cheese!!

I have to say that I was surprised from your workaround… I really like it and gonna study it for future use.

But now I have my whole project working on this way of handling the prd_Tags… It was from the first things creating and was made using the method of saving tagify values into database.

Sorry but It’s difficult now to change it… Sure on an upcoming project my friend I will remember your method.

But still have the question… How server-side ArrayList.contains works or how to apply?

1 Like

Sorry it didn’t help…

Brain farting here!

Do you not need to add the array values and then split them, similar to (from another Project we do this to calculate extras for reservations) and its on the server side working with an array (maybe a better example):

Not sure if that just confuses the matter but you may have missed a step? Or I’m just off my head as usual! For which in that case ignore me entirely and slap me with a frozen trout!

:slight_smile:

Dear @Cheese,

I really appreciate your effort here but we’re missing the main question…

As you can see in my original post, I got my values into an ArrayList and then I had to check if the ArrayList contains the value of a repeat.
Then I used ArrayList.contains() because that is what it does (I SUPPOSE…)

ArrayList.contains() always returned false no matter what happens, so I canceled it and used the where formatter instead and solved what I wanted to do.

BUT… that amazing tool, ArrayList.contains() isn’t working? Or I’m using it in a wrong way?

Again: Thanks for your time!

1 Like

Hey @famousmag

I don’t know how the .contains() works…

But the way I work with tags like this is to have a separate database table which links the products and the tags…

TABLE product_product_tags

  • product
  • product_tag

Then you just need to query this whole table, return JUST the product_tag field with the Distinct option set…

… and Voila, you have your array in one line of code.

Often, if finding something seems too difficult, it is because your data structure could be better…

I hope that helps!
Antony.

2 Likes

You’re absolutely right @Antony!

That’s the way I would build my database if I was starting now a project…
But this project exists and work well, I have also found the way to handle my JSON field in several cases.

But I mean… We have a great tool in there ArrayList that is supposed to perform a specific procedure, the .contains(), but we don’t know how to use it?

1 Like

Hey! Question!
The output of tagExists returns false?
Or is the condition that doesn’t apply?
What about using !tagExists instead of tagExists==false on the condition?

Hey @franse,

It is the same thing my friend…
I tested it anyway and doesn’t make a difference
Thanks for trying!