gpt4 book ai didi

javascript - 如何根据另一个引用文献的变化过滤引用文献?

转载 作者:行者123 更新时间:2023-12-05 02:52:56 25 4
gpt4 key购买 nike

我创建了一个包含 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",
},
],
},
],
},

我尝试了 docs 中提到的 filter 选项但似乎无法正常工作,所以我将其删除。

这就是我定义类别子类别的方式。

// 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",
},
],
},
],
};

感谢任何帮助。

最佳答案

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

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",
},
],
},
],
}

这会将 filterfilterParams 添加到 subcategories 字段上的 options 对象。这应该会缩小引用选择器中可选择的项目的范围。

编辑:这应该可以为您完成工作。如果尚未设置 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,
}
}
}
}
}
]
}

关于javascript - 如何根据另一个引用文献的变化过滤引用文献?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62436032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com