Filter using Tagify which takes data from an array ["CHI","ARG"]

Hey guys.
I have a field in a table (pct_code_country) that saves in this array format [“CHI”,“ARG”]. These are country codes. This format is saved by a Tagify field when editing the product in the Back-end.
On the front-end I have a page with a filter on a tagify field that takes information from the pct_code_country field to filter but can only filter if the data is saved in this format: CHI. I’ve tried CHI, ARG… ‘CHI, ARG’ but it doesn’t filter. You can only filter if you have a country code in the field (CHI). Then it shows all products that have the CHI code.
Is there any way to take this array [“CHI”,“ARG”], separate it and use it in the filter?
I already did the entire registration system for these codes using the Tagify field and I didn’t want to have to do it again.
I stopped at this problem and I can’t move forward.

Perhaps I did not fully understand you, but I would do two things:

  1. would bring all the values ​​in the database to a single format like array [‘CHI’,‘ARG’] - now it seems that this is clearly not the case with you
  2. it seems easier to me to send the filter value from Tagify to Server Action, where it forms an array, checking compliance with the conditions in a loop and returns a ready-made list to the front
1 Like

Hello.
Thanks for the answer.
The pct_code_country field in the table is in the format [“CHI”, “ARG”].
What I need is to filter by the pct_code_country field and where it has the CHI code, for example, it shows all the products that have this code. But if the same product has the code CHI and ARG [“CHI”, “ARG”] it will show the products that have these codes.
What I’m not able to do is make the filter work with this array [“CHI”, “ARG”]. It only works if the field has a single code, CHI for example.

Do you mean a filter in a database query or something else?

In the video I demonstrate what I need.
When selecting the country in the list, the filter only shows products that have the country code in the pct_code_country field.
If I select another country, it will complement the result.
This works perfectly with just one Country code in the pct_code_country field and in this format CHI or ARG or FRA. If I apply an array [“CHI”, “ARG”] to a product that is in Chile and also in Argentina, the filter does not work. This must be an array problem. I cannot return the result if it is in the array format [“CHI”, “ARG”].

Column in table:

As I understand it, you are trying to do the filtering process at the front? And I suggest doing this on the server side.

1 Like

But on the server side, will it work with the field being an array [“CHI”, “ARG”]?

On the server you do something like the following

  1. create an empty Array List for the result
  2. query for all records of your table
  3. outer loop using the filter passed as an input parameter in the form of an array
  4. a loop through all the records of the table, inside which you check whether the required code is in the value from the table - if so, then add the value of the record to the List
  5. after all the cycles, you get the value from the list into a variable for output - via Get Array List Value
1 Like

A small correction - it’s better to make an outer loop through the table from the database, and an inner loop through the query array, and if there is at least the first match of a country, then you can exit the inner loop - make some kind of boolean flag for this - this way you will avoid duplicating records and it will be faster work

1 Like

I mocked this up for you…

You can see it in action at…

https://dashboard.triptakers.travel/Testing

It’s a pretty convoluted way to do this really. Normalizing your database would really be better, but this works. LMK if you need more info on what’s happening.

Here’s what the table looks like…

Off to take a well deserved hot tub. :smile:

test_for_marco.zip (1.5 KB)

2 Likes

With you 100% on that one.

2 Likes

Hello, @Heather_Mann .
Thank you very much for the tutorial. I’ve already replicated it here.
But this message appears in the browser debug:
“message”: “Array to string conversion”,

This is server connect:

I think this -CLIENT_SIDE: Filter a query’s JSON Array column on ANY match of a tagify content - SOLVED - is a much more elegant solution.

Feel free to get back to me if that doesn’t work for you.

I saw this post but I confess I had doubts.
I usually do everything in PHP code and I’m migrating some projects and creating them from scratch in Wappler. I’m creating this from scratch, so I was very confused on how to apply that function to solve my problem.

Have a look at the additional link I added in that post. It covers how to use custom formatters.

1 Like

Yes.
That’s what I’m doing right now. I’m trying but it’s really confusing for me, but here goes…

Sorry.
It’s very confusing for me…
For example, where do I insert this block of code?
What is The dmx.formatter is?

dmx.Formatter('array', 'existInArrays', function (arr1, arr2) {
 var res = 0;
 if (arr1 && arr1.length && arr2 && arr2.length) {
     const output = arr2.filter(function (arr) {
         res = res + ((arr1.indexOf(arr) !== -1) ? 1 : 0);
     });
 }
 return res;

});

Hey @marcosvinicios,

I can help if you like…
I think it will be a quick think and NOT complicated once you understand it

dmx.Formatter is the way to create custom formatters, like the built-in toString(), toNumber(), etc…

  1. Create a folder “js” right in your root.

  2. Create a .js file in js folder and name it as you like (lets say “customformatters.js” like I did)

  3. Inside that .js file you paste the code I posted

  4. Now you have to include the .js file in your page

When you do these 4 steps tell me to go for the next steps

Hello.
Ok, I’ve already done the 4 steps.

OK,

Let’s go in your server action because it is fast…

  1. A $_GET variable type of Array.
  2. A setValue step with outpur enabled, that outputs the $_GET.countries.split(’,’)
    steps 5 and 6 are ready you got it.
  3. We need our query…
    I have a simple query that exports the id, name and the tags (JSON Array) of my products…

I suppose you can work the same way and build a query that outputs whatever fields you need ( complicated, joins whatever you need) JUST MAKE SURE TO INCLUDE the JSON Array column, I think you call it pcs_code_pais…

NO CONDITIONS needed for our main purpose (filter the countries by the tagify values). You can add any condition is necessary (dates, amounts, names of clients ) but don’t add any condition related to the JSON Array.
(This will be made client-side with the dmx.formatter)

Got it?