Grouping documents in Sanity.io using parent/child relationship and customization of default list.

9 replies
Last updated: Dec 2, 2022
Is there a way to group documents? I have something for categories where I followed the Parent/Child relationship guide . I guess I just follow that anytime I want something to be grouped?
Nov 29, 2022, 12:21 AM
Are you looking to group documents within a single list? That would be possible, but you'd need to do some customization to the default list to get it to work.
However, if you're ok with having them under separate list items and in separate lists, you'll want to use
this method.
Nov 29, 2022, 12:27 AM
I basically want to see “Schools” in the list like it is and then when I click on it I would see Division A and Division B. Then clicking Division A I would see all the schools that fall under Division A and the same for Division B
Nov 29, 2022, 12:35 AM
But these schools could switch divisions so I don’t want to hard code it
Nov 29, 2022, 12:36 AM
Got it! You'd want to use the method I shared then. How are divisions shown in your schools?
Nov 29, 2022, 12:37 AM
Well those divisions are sacristy the two parent categories you see in the categories screenshot 😅
Nov 29, 2022, 12:38 AM
You'd want to do something like this (if the division is a document that's referenced by a school):
S.listItem()
    .title('Schools')
    .child(
      S.documentTypeList('division')
        .title('Schools by Division')
        .child(divisionId =>
          S.documentList()
            .title('Schools')
            .filter('_type == "school" && $divisionId == division._ref')
            .params({ divisionId })
        )
    ),
Nov 29, 2022, 12:49 AM
Finally able to get to this 😅 A couple of questions...1. How can I remove FCS and FBS from this?
2. The category "Playoffs" I have no idea where that came from... Any ideas how to figure that out
Dec 2, 2022, 11:10 PM
Ooo figured out #1
// Schools
      S.listItem()
        .title("Schools")
        .icon(FaSchool)
        .child(
          S.documentTypeList("category")
            .filter('_type == "category" && title != "FBS" && title != "FCS"')
            .title("Schools by Conference")
            .child((categoryId) =>
              S.documentList()
                .title("Schools")
                .filter('_type == "school" && $categoryId == conference._ref')
                .params({ categoryId })
Now I just have to figure out where "Playoffs" is coming from
Dec 2, 2022, 11:30 PM
Figured it out
Dec 2, 2022, 11:31 PM

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?