--- title: Implement folding slug: latest-folding canonical_url: https://docs.coveo.com/en/atomic/latest/usage/displaying-results/folding/ collection: atomic source_format: adoc --- # Implement folding [Coveo Atomic](https://docs.coveo.com/en/lcdf0264/) lets you display search results that have a parent-child relationship with any level of nesting. This article explains how to build hierarchical result lists using the [`atomic-folded-result-list`](https://docs.coveo.com/en/atomic/latest/reference/components/atomic-folded-result-list/) component. ## Prerequisites Before pushing any data to the Coveo [index](https://docs.coveo.com/en/204/), make sure that the data you're working with has established parent-child relationships. Some data may already have these relationships when you collected it. If not, you'll need to build a relationship graph yourself. > **Note** > > You can't create parent-child relationships via the [Coveo Administration Console](https://docs.coveo.com/en/183/), which means you must implement such data processing in your own infrastructure. Once your data has established parent-child relationships, push the data to your [Coveo organization](https://docs.coveo.com/en/185/) and then create the proper [folding fields](https://docs.coveo.com/en/1884/) for the target items. ## Main features The `atomic-folded-result-list` component is similar to the [`atomic-result-list`](https://docs.coveo.com/en/atomic/latest/reference/components/atomic-result-list/) component, but provides extra functionality. This component requires no extra configuration apart from specifying the three folding fields: * [`collection-field`](https://docs.coveo.com/en/atomic/latest/reference/components/atomic-folded-result-list#properties) * [`parent-field`](https://docs.coveo.com/en/atomic/latest/reference/components/atomic-folded-result-list#properties) * [`child-field`](https://docs.coveo.com/en/atomic/latest/reference/components/atomic-folded-result-list#properties) The `atomic-folded-result-list` requires one or more [`atomic-result-template`](https://docs.coveo.com/en/atomic/latest/reference/components/atomic-result-template/) components to display the results. This result template can include an [`atomic-result-children`](https://docs.coveo.com/en/atomic/latest/reference/components/atomic-result-children/) component with nested [`atomic-result-children-template`](https://docs.coveo.com/en/atomic/latest/reference/components/atomic-result-children-template/) components to render the child results. The `atomic-result-children` component also offers two slots named `before-children` and `after-children`. They're available via the attributes `slot="before-children"` and `slot="after-children"`. These slots let you insert a list of children only when children are available for that specific result. The slots let you insert a component **inside** `atomic-result-children` but **outside** any templates. ## Child elements The following sample shows the basic structure to fold results with one child. ```html ``` > **Important** > > The `atomic-result-children-template` component doesn't let you leave templates empty. > You have to specify elements inside of them. ## Grandchild elements To render grandchildren, add an extra layer, as in the following example: ```html <1> ``` <1> If you want the grandchildren to be the same as the children, you can set `inherit-templates` attribute to `true` on the grandchild level and omit the templates. With that option, you can either leave the `atomic-result-children-template` empty or still specify its own templates which will take priority over children (or inherited) templates. The grandchild rendering depends on templates and the value of the `inherit-templates` option. For example: * The child defines template 1 and template 2; the grandchild defines template 3 and has `inherit-templates="true"`. The grandchild will use the templates in the following order: .. Template 3 (defined at the grandchild level) .. Template 1 and template 2 (both defined at the child level, in this order) * The child defines template 1 and template 2; the grandchild defines template 3 and `inherit-templates` is set to `"false"` or not specified. The grandchild will only use template 3 and ignore the other two. ## Comprehensive example The following example uses all the features mentioned in the previous sections. Note that templates are only available within the scope in which they're defined. For example, an `atomic-result-children-template` child defined inside an `atomic-result-template` will only be available for results that match both templates. An `atomic-result-children-template` that's a child of another `atomic-result-children-template` will similarly only be available for results that match both these templates. To reuse templates, define them outside of the scope of the results and refer to them by ID anywhere you want to place them. ```html ``` <1> YouTube results will ignore any children as no `atomic-result-children` components are present here. <2> Slack results will only show children that are uploaded files and ignore grandchildren. <3> The `p` element will only be rendered when a Slack result has children. ## Folding in rendering functions The `atomic-folded-result-list` component lets you set the templates by passing it to a `renderingFunction` that will return a `FoldedResult`, which you can then use to render its children or grandchildren as well. > **Warning** > > Rendering functions are necessary when using [Atomic React](https://docs.coveo.com/en/atomic/latest/usage/frameworks/atomic-react-wrapper/), which doesn't support `atomic-result-children`, `atomic-result-children-template`, or any other Atomic components in child results. The following is an [example](https://github.com/coveo/ui-kit/blob/master/packages/samples/atomic-react/src/pages/FoldedResultListPage.tsx) that leverages rendering functions to perform folding in Atomic React: ```js import { AtomicResultBadge, AtomicResultFieldsList, AtomicResultLink, AtomicResultMultiValueText, AtomicResultPrintableUri, AtomicResultSectionBadges, AtomicResultSectionBottomMetadata, AtomicResultSectionExcerpt, AtomicResultSectionTitle, AtomicResultSectionTitleMetadata, AtomicResultSectionVisual, AtomicResultText, AtomicText, AtomicResultSectionChildren, AtomicFoldedResultList, AtomicResultImage, } from '@coveo/atomic-react'; import {FoldedResult} from '@coveo/headless'; import {FunctionComponent} from 'react'; import {AtomicPageWrapper} from '../components/AtomicPageWrapper'; export const FoldedResultListPage: FunctionComponent = () => { return ( ); }; function MyTemplate(result: FoldedResult) { return ( <> {result.result.raw.language && ( )} <1> {!!result.children.length && (
This result has children:
)}
<> {result.result.raw.author && (
)} {result.result.raw.source && (
)} {result.result.raw.language && (
)} {result.result.raw.filetype && (
)}
); } ``` <1> This is where you define children. `FoldedResult` offers you the `children` prop where you have access to the child result data.