Advanced filter based on other table

How do I filter a repeating group based on values in different table? Please guide me how to set up my database.

Currently I have a table called product (producttype, price, latitude, longitude, etc.)
Also have a another table called preference (user, producttype, minprice, maxprice, max distance, etc)

Based on above database structure, how do I search a product showing in the repeating group with those filter from preference table?

So a user, has a list of product type in his type field such as microwave, refrigerator. He also has min price = 100 and max price = 500, but no max distance.

However, there will be another user who has different preference: max distance = 500, no min price, but has max price.

How do I create such filter? Can my table structure work? should I modify it?

@NigelG any idea? I saw you had responded to similar question before.

Thanks a lot in advance.

Do Users have a Preference associated to them? I see that ‘Preference’ has a user associated to it, but is the Preference also attached to the user?

Yes the preference table has a link to a user table. So each user will have his own preference.

I understand that part, but does the User have an attribute called Preference?

I’m trying to understand, you are asking if the table user table has a field linking to the preference table? No it doesn’t. Should it be added? So both tables have links to each other? Would it also work if a user has multiple preferences? Meaning, each user may have multipe records in the preference table.

As this is quite complex in terms of data it would be good to post a link to your app.

It would be simpler if the preferences were on the User table, but if you want multiple preferences per user then it is worth have a reference on both the User (type Preference, tick the “Is a list” box) and on Preference (type User).

1 Like

Yes, you can have a ‘Preferences’ field on the User data type that is a list of Preferences.

I like normalised tables so I tend not to do two-way links like this unless there is no other way. As a possible alternative, you should note that you can also search within a search or filter your search. So your RG data source can be a search of Products/ProductTypes and then filtered according to the current user’s preferences.

A quick example: https://bubble.io/page?type=page&name=twotablesearch&id=louisforum&tab=tabs-1

1 Like

Thanks @NigelG, @potentialthings, and @louisadekoya this is helpful.

@louisadekoya I noticed there’s a :filtered option in the repeating group data source. Being new I have never seen this and need to grasp what it does.

It looks like you “search for product” then this one is filtered from the drop down. then you also add :filter option to further filter, based on maxprice. if you want to add more preference, add a new constraint under that dialog box?

how does it work when a user leave the ‘maxprice’ column empty? will it ignore that clause?

also I don’t see table user and preference link to each other in your example. maybe we don’t have to?

Bubble can be a little weird with null values in searches, so I think you should try it out and see.

I have only done a one-way link (the preferences table has a user field). I did this to illustrate that that the two-way link may not be necessary.

By the way, I only used the filtering option because with regular search constraints, the less than or equal to operator (≤) is not available, but it is with filter constraints. If there are more constraints, you can add them to the original search constraints for the RG, so you have a smaller set of records to filter on.

Generally speaking, is filtering done client-side in Bubble - i.e. After the data are fetched from database? (Just trying to get my head around the Bubble-specific terminology.)

ao I created this page… I have no clude how to filter the list.

I was able to create the dropdown on the top of the page, but don’t know how to link them to repeating group. can’t just filter by preference name from the drop down as other use may have the same preference name. it needs to llok for current user’s preference name, find all the fields associated with it…

any help?

I think your app is in private mode, so I can’t access it.

I don’t think you want to filter by preference name - you want to filter by preference. From your description of your data structure, a preference belongs to a user, so each preference in your dropdown should belong to a specific user, therefore if you filter the RG by the selected preference, you should get products matching the owning user’s preference.

@shot62, I’m not certain but I think you’re correct that this is how it works.

sorry, how do I make it non private?

Go to Settings > General tab > Application rights

Done.

If you can see I was able to use dropdown’s value’s minprice, dropdown value’s maxprice etc.

As you may notice I am trying to calc distance of product and filter based on the preference table. That’s where I am stuck… how do I calculate distance (user has lat long, product also has lat long), then how to filter it?

currently I could calculate based on my current geo location, but not user’s location given its lat / long.

perhaps I should’ve stored actual address instead of lat/long?

you can test my app by run as (select user) then on index there’s product list button

Oh, I’m afraid I haven’t done anything with maps/location in Bubble. A forum search for “distance between” yields some interesting looking results that you may want to check out though.

OK. I think I have figured it out the filter. This is what I have found:

If filter is numeric, value can’t be NULL, but text can be NULL
if filter is TEXT, it is case sensitive → if product type is ‘New’, then filter needs to be ‘New’. ‘new’ will not work.

to calculate distance using lat / log, you have to convert lat long to address first by using the ‘calculate coord to address’ option for the origin. then choose distance from, then do 'calculate coord to address again for the destination.

below is my filter screenshot

thanks @louisadekoya for your sample app!

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.