Solutons Lounge

How to enable user specified sorting? – Need help


I have a data Type of Posts and a few repeating groups which display them. This one is currently using the Sort by > Modified Date in the Search for Posts Data source as shown in the attached image, but I want to add a dropdown to the page allowing the user to choose different Sort by options. Can anyone point me in the right direction of how to figure this out?
image

I suspect that one way to start is by creating a custom state (does it go on the Repeating Group?) that stores the Sort by option that the user picks from the drop down, but then how the Sort by gets changed depending on that state is confusing me.

Could I do it entirely through Conditionals on the RG element?

Like, “When Dropdown X’s selected item is Y, Sort by Y”? Any reason why this would be suboptimal?

I usually use an Option Set to define sort options for a given datatype.

You just need to define the field name, and whether Descending is yes.

Then just add a dropdown with that Option Set, and set the sort on the RG to Change which field, then set the field and descending options to be that of the selected option from the dropdown.

Could I do it entirely through Conditionals on the RG element?

Yes you could, but it’s not a great way to do it in my opinion – it’s hard to maintain, and just plain unnecessary when you can do it completely dynamically as explained above.



2 Likes

Thank you! I will try to see if I can get it working this way Much appreciate the recommendation.



1 Like

I’m getting a little tripped up trying to implement this because I misspoke, and am not using an actual Bubble Dropdown element, instead I am using a Group Focus, where each element in the group has a workflow triggered on click.

I think that the corrollary here would be to have that click change the Option, is that right? I’m just not sure what goes into the Dynamic field name on the Search, in that case, since the selected option from the dropdown doesn’t exist.

Does that make sense? I will keep hammering at this, but wanted to relate the issue I ran into in case someone reading sees my obvious error.

Updating this to show where I’m at. I couldn’t figure out how to get clicking the child element in the Group Focus to change the option, so I went with having it set a custom state “CS Sort Method” here. So I’m still stuck on what to put as the Dynamic field name, but the answer may be different from my last post.

It doesn’t matter whether you’re using a Bubble Dropdown, or any other type of input, or a custom element (i.e. a groupfocus) – the concept is the same.

Each item in the list of sort options should correspond to an Option from the Sort Option set – so in your GroupFocus, presumably you have a Repeating Group of those Options.

When one is clicked, set a custom state to that value, and use that Option as the basis for your sort.

The dynamic field name is the name of the field you want to sort by (it must be entered exactly as it appears on the data tab).

But, obviously, to make it dynamic, that needs to be stored as an attribute on the Option Set – so the expression you need to enter is the Custom State value’s Field (in whatever terms you have set up your option set attributes).

Then the Descending value can also be dynamic if needed – or just define it manually if not.



1 Like

Okay, thank you again! I did not have a Repeating Group in the Group Focus referring to the Options. I will try that if I decide to go the Option Set route!

For now, I’m tempted to lean on what I know, which is how to write the expression as a Conditional, “When Repeating Group X’s Custom State “Sort Method” is Y, Data source = Search or Posts Sort by Y.”

I should mention that I want to be able to change the sorting method via other conditions too, not just the drop down state, for example I don’t want the Current User to be able to change the sorting method if they are not the Post’s Creator, so this seems like a way to consolidate all the conditions in one place.

I fully admit you may be right about this being less optimal and I may be missing some reason why this won’t work, but the other way seems to add additional steps. Am I wrong?

It will definitely work… and if it makes more sense to you then there’s no reason not to do it that way instead

(although, to me, using multiple conditionals certainly seems like more work, more complexity, less efficiency, and less manageability than using a single dynamic expression)…

But there’s always more than one way to do something in Bubble – so do whatever makes the most sense to you.



1 Like

Thank you so much! That does make sense, that multiple conditionals are less manageable than a single dynamic expression. I’ve run into that before, and I like knowing the reason, whether I go that route or not.

I am still fairly new to Bubble, and have often refactored things I went with early on, once I understood the reason for a different best practice later. I suspect I may come back to this too!



1 Like

Now that I’ve constructed seven separate conditionals I totally understand your point, however, I’m not sure how to make separate options for each sorthing method, with some Descending (yes) and some (no).

If the Sort by is set to “Change which field…” I can only change the field name dynamically, whereas a Conditional allows me to choose the Sort by and whether it is Descending yes or no.

Am I missing something obvious again?

The descending field is also dynamic, so assuming your sort Option set has an attribute for Descending (yes/no) just set the descending value to the selected option’s Descending value.



1 Like

Ahh!!! Of course. I’ve never used the Attributes before. Now I understand what they are for Thanks many times!



1 Like



Source link

Exit mobile version