How to filter a dropdown list in Sanity Studio based on selected options

8 replies
Last updated: Jul 5, 2022
Hi! Happy Friday!Is there something I can use , so the array of reference does not show me the Services that are already selected?
I would like to select only the one that are not on the list. For example if Orthopedics is already selected, it should be hidden on the Dropdown. Is it posible?
Thanks!
Jun 24, 2022, 1:13 PM
you would probably have to create a custom input component to handle that logic, but i don't see why it wouldn't be possible
Jun 24, 2022, 1:19 PM
Mmmh, no I think you might be able to do it with just a filter.
Jun 24, 2022, 1:25 PM
Not sure. 😅
Jun 24, 2022, 1:26 PM
I’ve definitely done something like this before, and yes it made use of the filter property Kitty pointed to - let me see if I can dig it up
Jun 24, 2022, 2:09 PM
Aha, here we are - haven’t got the Studio for this particular project up and running atm so can’t confirm but something like this should be similar
export default {
  name: 'term',
  title: 'Term',
  type: 'document',
  fields: [
    {
      name: 'type',
      title: 'Name',
      type: 'string',
    },
    {
      name: 'childTerms',
      title: 'Child term(s)',
      type: 'array',
      of: [{
        title: 'Child term',
        type: 'reference',
        to: [{type: 'term'}],
        options: {
          filter: ({document}) => {
            // We need to provide both the draft and the live id (prefixed by 'drafts.' and without this prefix)
            // in order to exclude the current term from the list of possible child terms.
            const idWithoutDraft = document._id.replace('drafts.', '')
            return {
              filter: '!(_id in $selectedIds)',
              params: {
                selectedIds: [idWithoutDraft, document._id, ...document.childTerms.map(term => term._ref)]
              }
            }
          },
        },
      }]
    }
  ]
}

Jun 24, 2022, 2:13 PM
Hey
user S
, it was really helpful and it works fine when I use the standar schema type. My problem is that when I use the Sanity Schema Builder it comes with an error. Do you know if I can fix it?Works:

{
                name: 'childTerms',
                title: 'Child term(s)',
                type: 'array',
                of: [{
                  title: 'Child term',
                  type: 'reference',
                  to: [{type: 'pages'}],
                  options: {
                    filter: ({document}) => {
                        console.log(document);
                      // We need to provide both the draft and the live id (prefixed by 'drafts.' and without this prefix)
                      // in order to exclude the current term from the list of possible child terms.
                      const idWithoutDraft = document._id.replace('drafts.', '')
                      return {
                        filter: '!(_id in $selectedIds)',
                        params: {
                          selectedIds: [idWithoutDraft, document._id, ...document.childTerms.map(term => term._ref)]
                        }
                      }
                    },
                  },
                }]
            }  ,
Not Working:
S.array('testt', 'TEst').of([ 
                S.reference('pages', 'Select a Page')
                    .to([{type : 'pages'}])
                    .options({
                        filter: ({document}) => {
                            console.log(document);
                            // We need to provide both the draft and the live id (prefixed by 'drafts.' and without this prefix)
                            // in order to exclude the current term from the list of possible child terms.
                            const idWithoutDraft = document._id.replace('drafts.', '')
                            return {
                                filter: '!(_id in $selectedIds)',
                                params: {
                                    selectedIds: [idWithoutDraft, document._id, ...document.childTerms.map(term => term._ref)]
                                }
                            }
                        },
                    })
            ]),
Jun 27, 2022, 12:24 PM
Sorry, I’ve not used the Sanity Schema Builder before so I’m not sure about its usage/limitations
Jul 5, 2022, 9:07 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?