1

I have an navigation structure in Directus.io which consists of two collections.

navigations

  • id
  • title
  • navigation_items (One-to-many)

navigation_items

  • id
  • title
  • navigation (Many-to-one)
  • parent (Many-to-one)
  • children (One-to-many)

Since navigation_items is recursive, how can I get the complete tree in one request?

Example:

$readItem('navigations', id, {
  fields: ['id', 'title', 'navigation_items'],
})

Return:

{
  "id": "main",
  "title": "Main",
  "navigation_items": [
    3
  ]
}

But I have three sub levels of navigation items. How can I get the complete tree?!

1 Answer 1

0

$readItem('navigations', id, {
  fields: [
    'id', 
    'title', 
    'navigation_items.id', 
    'navigation_items.title',
    'navigation_items.children.id',
    'navigation_items.children.title',
    'navigation_items.children.children.id',
    'navigation_items.children.children.title',
    'navigation_items.children.children.children.id',
    'navigation_items.children.children.children.title'
  ],
  deep: {
    navigation_items: {
      _limit: -1, // This will remove any limit on the number of children retrieved
      _sort: 'id',
      children: {
        _limit: -1,
        _sort: 'id',
        children: {
          _limit: -1,
          _sort: 'id',
          children: {
            _limit: -1,
            _sort: 'id'
          }
        }
      }
    }
  }
})
.then(response => {
  console.log(response);
})
.catch(error => {
  console.error(error);
});

In this example, the fields parameter specifies which fields you want to retrieve, including the nested navigation_items and their children up to three levels deep. The deep parameter configures the depth of the recursion and ensures that all children at each level are retrieved.

If you need more than three levels, you can extend the fields and deep parameters accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.