Learn how to add sort fields inside of new and existing relationships inside of Directus.
Bryant: Hi. Welcome back to the next episode of Short Hops. I'm your host Brian Gillespie and today we are talking about sort fields. This is one that I see come up a lot in our community. Very simple fix, and most people overlook this functionality.
But surprise surprise, you can add sort fields anytime. So I see this coming up often on our many to any relationships, like our page builder scenario or even standard, like, one to many or many to many relationships. Wanna be able to sort those related items within that particular field. So let's just throw together a page builder here, we'll just call this our page blocks. Great.
And then we've got our block FAQs, etcetera. So here on this screen, I don't have a sort option when I'm using the the wizard here, but if I go to continue an advanced field creation mode, I go to my relationship, you'll see there is an option for sort field. So I don't have to manually add a sort field, I can just add sort here and at the bottom, I could see that Directus will create this for me. Likewise, if I wanted to call it something else something else, I wanted to get really fancy with it, I can. So I don't have to name it sort, that's just kind of standard convention, but adding that sort field is as simple as that.
So just go into advanced creation mode. Great. And then when I go to my pages now, I go to build a page, I could go in and create a new block, test test. Great. Let's add a hero, test, test and then I can rearrange those using that particular sort field.
Great. But what about existing relationships? So if I look at projects for example. Right, if I go in and create a test task and a new task, oh, I can't even spell today, test project. Right.
I've already got this relationship set up, but my sort fields, I don't have a way to sort this. How do I fix it? So I just go into my data model, I go to Projects, I look for my field that has a relationship, in this case, it's Tasks, and I go to that relationship tab and I have sort field. So I can pick that item up from one of the existing fields, which there are none, or I can simply just key something in here and Directus is smart enough to know that this field does not exist, so it will actually create it for you. And with that, all I have to do is now go back to our project and I could sort those items however I want.
That's it. Easy peasy. I'll catch you on the next one.