DefaultSort on a Repeater
I've been searching high and low, but couldn't find an answer to this. Is there a way to decide the sorting order for items in a repeater?
This is my repeater and its components:
I want to sort this by created_at so the newest is at the top; Is there a way I can do this?
10 Replies
Can you share the part of your model that defines the
notes
relationship?It's a simple HasMany
And is the Repeater field orderable by the user? Or should it be fixed?
It should be fixed, but ordered by created_at so newest is at the bottom.
When you add an item in the Repeater, it should show at the bottom by default. Are you creating items also from other places in the app?
(Just trying to understand the context)
I'm not, but my client wants his notes added to be sorted by newest first, and that's been a real headache 😂
Ok, I understand. So the issue is that the new items added in the Repeater don't have a concept of
created_at
, until the record is actually saved...Solution
Something to try - add the orderBy on the relationship definition:
I think the new items will still appear at the bottom, but should be reordered correctly after saving. Not ideal, just something to explore.
That worked! Thank you.