Register now - Learn how Tecovas roped in success with Sanity and Shopify 🤠

Filtering options in an array dropdown for selecting modules by matching course in Sanity.io

20 replies
Last updated: Aug 5, 2022
Hi there, how can I filter the options that come when selecting an array dropdown? So I have a bunch of courses, each course an array of modules. I want to select only modules by their matching course. See attached screenshot.
Aug 4, 2022, 6:59 AM
So to clarify if I'm on Course 1 page I only want "Course 1 Module 1" and "Course 1 Module 2" to show up.
Aug 4, 2022, 7:00 AM
Your field is an array of references, is that correct?
Aug 4, 2022, 7:10 AM
user F
thank you for your response, yes it is an array of references
Aug 4, 2022, 7:26 AM
Hi there, can anyone help please? Thank you
Aug 4, 2022, 12:48 PM
Hi Richard. You can filter your references by setting up
option.filter
. There’s an example in the reference docs that could be adapted to do what you’re after.
Aug 4, 2022, 3:37 PM
user A
awesome thank you very much will check it out now!
Aug 4, 2022, 3:57 PM
Hi there, just got around to working on this and I was wondering if there was a better way to do this?
Aug 5, 2022, 6:42 AM
Not sure how to do this, any ideas? The document reference is returned with "brief." prefixed, so I've sliced it off - not sure if this is the right way to do this. I also can't seem to get the GROQ query working properly - could use some input please!
Aug 5, 2022, 7:04 AM
Okay, seem to have got the GROQ query done properly, see screenshots correct courses are showing up in the console logs. But not showing in the array dropdown in the studio?
Aug 5, 2022, 7:33 AM
EISH!
Aug 5, 2022, 7:35 AM
All fine, was dumb, passing the full query to the filter. Changed to course._ref == $courseRef - all good now thank you!
Aug 5, 2022, 7:36 AM
Yay, good work!
Aug 5, 2022, 7:37 AM
Just one question, is it correct practice to slice off the "brief." prefixed to the document id? Or is there a better way?
EDIT: Sorry I mean "draft."
Aug 5, 2022, 7:37 AM
In which context?
Aug 5, 2022, 7:43 AM
On the current document when I'm editing.
Aug 5, 2022, 7:45 AM
So I have a bunch of modules, and each module references only one course. Then in the course you have an array of references to modules. So that's why the document is in draft state.
Aug 5, 2022, 7:45 AM
If the ID will never change then it should be fine.
Aug 5, 2022, 7:45 AM
Right.
drafts.foo
will always be the draft version of document
foo
. If that’s what you were asking.
Aug 5, 2022, 7:46 AM
Thank you!
Aug 5, 2022, 7:46 AM
So by adding or removing the prefix, you can get the ID of the draft or published version of the same document (if it exists).
Aug 5, 2022, 7:46 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?