3

我创建了一个具有 3 个参考字段的对象 - 类别、子类别和类型。我希望子类别显示与所选类别相关的子类别。

{
      title: "Product Category",
      name: "category",
      type: "object",
      fields: [
        {
          name: "categories",
          type: "reference",
          to: [
            {
              type: "categories",
            },
          ],
        },
        {
          name: "subcategories",
          type: "reference",
          to: [
            {
              type: "subcategories",
            },
          ],
        },
        {
          name: "types",
          type: "reference",
          to: [
            {
              type: "types",
            },
          ],
        },
      ],
    },

我尝试了文档filter中提到的选项,但似乎无法使其正常工作,因此我将其删除。

这就是我定义categoriesand的方式subcategories

// categories.js
export default {
  name: "categories",
  type: "document",
  title: "Categories",
  fields: [
    {
      name: "category",
      type: "string",
      title: "Category name",
    },
    {
      name: "slug",
      title: "Slug",
      type: "slug",
      description: "URL friendly name",
      options: {
        source: "category",
        maxLength: 96,
      },
    },
    {
      title: "Description",
      name: "description",
      type: "text",
    },
  ],
};

// subcategories.js
export default {
  name: "subcategories",
  type: "document",
  title: "Subcategories",
  fields: [
    {
      name: "subcategory",
      type: "string",
      title: "Subcategory name",
    },
    {
      name: "slug",
      title: "Slug",
      type: "slug",
      description: "URL friendly name",
      options: {
        source: "subcategory",
        maxLength: 96,
      },
    },
    {
      name: "categories",
      type: "reference",
      to: [
        {
          type: "categories",
        },
      ],
    },
  ],
};

任何帮助表示赞赏。

4

1 回答 1

4

根据您的架构,我认为这应该可行:

export default {
  title: "Product Category",
  name: "category",
  type: "object",
  fields: [
    {
      name: "categories",
      type: "reference",
      to: [
        {
          type: "categories",
        },
      ],
    },
    {
      name: "subcategories",
      type: "reference",
      to: [
        {
          type: "subcategories",
        },
      ],
      options: {
        filter: 'references($category._id)',
        filterParams: {category: 'categories'}
      }
    },
    {
      name: "types",
      type: "reference",
      to: [
        {
          type: "types",
        },
      ],
    },
  ],
}

这会将filterand添加filterParams到字段上的options对象中subcategories。这应该缩小在参考选择器中可选择的项目。

编辑:这应该可以为您完成工作。categories如果尚未设置,它也不会产生任何结果:

export default {
  title: 'Product Category',
  name: 'category',
  type: 'object',
  fields: [
    {
      name: 'categories',
      type: 'reference',
      to: {type: 'categories'}
    },
    {
      name: 'subcategories',
      type: 'reference',
      to: {type: 'subcategories'},
      options: {
        filter: ({ document }) => {
          console.log(document)
          if (!document.category || !document.category.categories) {
            return;
          }
          return {
            filter: "categories._ref == $category",
            params: {
              category: document.category.categories._ref,
            }
          }
        }
      }
    }
  ]
}
于 2020-06-18T10:00:13.240 回答