Unexpected Problems Filtering Collection by Variable

I suddenly can’t figure out how to filter collections by variables. I could have sworn that I was able to set this up properly previously, but now I’m not getting any data when applying a filter based on dropdown selection value.

I recorded a Loom to show what I mean:

I’d really appreciate help with this - it’s something that I’ll need to do in multiple places throughout my app.

Now that I’ve spent more time in my editor, it seems as though multiple filters that were working recently are no longer working. Was there an update to the system that might have caused this?

Did you try converting the variable to a string instead of a number? try using toText()

1 Like

That worked! Thank you so much, just saved me a ton of frustration!

1 Like

Ok, while this works technically, there is a big problem. When I use “toText” to see if a value is contained in an array, it is literally just looking to see if the array contains that character. So, if the selection value is “3”, records with 3, 13, 30 in the array are resulting!

Any ideas on how to avoid that?

Don’t use the contains filter. Use equals to if you want it specifically only looking for a value equal to that.

I can’t do that in this case because I am filtering by an array. So if a record has [3, 14, 45], and the dropdown value is 3, I want to display that record. For that reason, the “is exactly” filter option doesn’t work.

What does your json structure look like?

Ah. I think I see what you are saying.

You have something like this? …

Locations = [
    {
         id: 2,
         location_name: "Toronto",
         employees: [ 1,2,3,4 ]
    },
    {
         id: 1,
         location_name: "Denver",
         employees: [2,4,5]
         }
]

You have a drop down with the value of employee id’s, and you want to select that drop down, and have the collection only show the locations that the employee is attached to?

Yes, that’s part of the json structure. But to make it even simpler, I am just looking to filter by the equivalent of the location ids, and I created another array in each record with just the ids in an array. So, using your above example,

Locations = [
2,
1
]

I don’t think I understand. Can you show an example of your JSON.

json just roles

And here’s how I set up my filters, in case that helps.

Have you tried using filterByKey on the justroles?

If you just need to filter an item in that array.

Screen Shot 2022-04-22 at 6.09.46 PM

I’m not sure how I would do that? I’m trying to filter a collection called users. Each user has a field called “justroles” which lists the ids of the roles the user has. I want to use a dropdown to filter the collection to only show users that have a certain role.

So, if the value of the dropdown is 3, and the collection items include:

[
{
user_id: 1,
justroles: [1,2]
},
{user_id: 2,
justroles: [2,3]
}
]

Only user #2 should be displayed in the collection list.

OK, I realized what you meant and tried it. No luck, unfortunately

For now, I am using a formula to selectively display individual list items that have the selection value in their justroles array. That’s imperfect, possibly for more reasons than I realize currently, but at least it is achieving the main goal for now.

This might be an imperfect solution also…. It looks like you’re using Xano…

An easy solution would be to supply the role name as an addon in the justroles array.

Do you have the role name as the display? then filter by the display field instead of the id, using the contains in the array. Then you wouldn’t have the problem of getting 13 if you only want 3.

I’ll think about another solution, but that could work if you can supply more data through the endpoint.

Problem with this would be if you have something like “admin” and “super admin”

Pulling only admin would also yield super admin. Yeah that won’t work. Hmm

Ah! Okay… I’m pretty sure this can be done with those array filter functions…

but, there’s also this solution… it’s kind of convoluted. I’m not near a computer to do this, but here it is incase you want to try now.

Use a workflow. When dropdown value changes.

Create a variable called users-with-role that is an array.

Use a loop in the dropdown workflow to loop through the users, and then a nested loop to go through the array of roles.

Use an array function to determine if the selected role id is in the array. If it is, place that parent user id into that users-with-role variable.

Then in your api endpoint, I would handle the querying. Supply that array of users to the api endpoint in your collection, and send back the correct users.

Refetch the collection at the end of the workflow.

If no array is supplied to the endpoint, have the array send back all the users that you initially wanted.

Again, I think there is an easier answer, but that would work.