首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用anyOf查看对象名称?

使用anyOf查看对象名称?
EN

Stack Overflow用户
提问于 2019-05-17 06:10:38
回答 1查看 765关注 0票数 0

我在这里有一个模式,我想要在其中有一个下拉列表,以选择一个选项,并从那里-根据选择显示不同的选项;所有选项都嵌套在一个数组中,以具有它们的倍数。

我注意到,当我填充虚拟数据时,输出json没有存储所选选项的名称

所以data.json看起来像这样:

代码语言:javascript
复制
{
  "page1": [
    {
      "imageOptions": {
        "imageHeightType": "vh",
        "imageHeight": 50
      },
      "textboxArea": {
        "headerText": "Header for selection1",
        "headingTag": "h1",
        "textBoxOpacity": 15
      }
    },
    {
      "content": "This is a complety different selection, yet there is no name to tell the difference between these two difference objects"
    }
  ]
}

正如您所看到的,在page1数组中没有对象来包装这两个不同的项-理想情况下应该是这样的:

代码语言:javascript
复制
{
  "page1": [
    {
     // Title of object goes here from schema
      "imageOptions": {
        "imageHeightType": "vh",
        "imageHeight": 50
      },
      "textboxArea": {
        "headerText": "Header for selection1",
        "headingTag": "h1",
        "textBoxOpacity": 15
      }
    },
    {
      // Title of object goes here from schema
      "content": "This is a completely different selection, yet there is no name to tell the difference between these two difference objects"
    }
  ]
}

有没有办法做到这一点?我已经查看了AnyOf的文档,但运气不是很好。React-JsonSchema-Forms非常新。

下面是我当前的架构:

代码语言:javascript
复制
{
  "type": "object",
  "properties": {
    "page1": {
      "type": "array",
      "items": {
        "type": "object",
        "anyOf": [
          {
            "title": "Full Width Image",
            "type": "object",
            "properties": {
              "imageOptions": {
                "type": "object",
                "title": "Image",
                "properties": {
                  "image": {
                    "type": "string",
                    "title": "Image",
                    "format": "data-url"
                  },
                  "imageHeightType": {
                    "enum": [
                      "px",
                      "vh"
                    ]
                  },
                  "imageHeight": {
                    "type": "number",
                    "title": "Image Height"
                  }
                }
              },
              "textboxArea": {
                "type": "object",
                "title": "Textbox Area",
                "properties": {
                  "headerText": {
                    "type": "string",
                    "title": "Heading Text"
                  },
                  "headingTag": {
                    "enum": [
                      "h1",
                      "h2",
                      "h3"
                    ]
                  },
                  "imageText": {
                    "type": "string",
                    "title": "Body Text"
                  },
                  "textboxPosition": {
                    "title": "Textbox Position",
                    "enum": [
                      "Left",
                      "Center",
                      "Right"
                    ]
                  },
                  "textboxColor": {
                    "title": "Color of Textbox Area",
                    "type": "string"
                  },
                  "textBoxOpacity": {
                    "title": "Textbox Opacity %",
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 100,
                    "multipleOf": 5
                  }
                }
              }
            }
          },
          {
            "title": "Custom Block",
            "type": "object",
            "properties": {
              "content": {
                "type": "string"
              }
            }
          }
        ]
      }
    }
  }
}

如果有助于理解我的问题,还可以通过Link连接到在线架构编辑器

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-19 03:59:02

为什么不为每个对象添加一个类似于name的属性呢?然后,您可以根据需要隐藏/禁用它:

模式:

代码语言:javascript
复制
"anyOf": [
    {
        "title": "Full Width Image",
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "default": "fullWidthImage"
            },
            "imageOptions": {
                "type": "object",
                "title": "Image",
                "properties": {...}
                ...
            }
            ...
        }
    },
    {
        "title": "Custom Block",
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "default": "custom"
            },
            "content": {
                "type": "string"
            }
        }
    }
]

uiSchema:

代码语言:javascript
复制
{
    "page1": {
    "items": {
        "name": {
            "ui:widget": "hidden"
        },
        "imageOptions": {...},
        ...
    }
}

然后,formData应该看起来像这样:

代码语言:javascript
复制
{
    "page1": [
        {
            "name": "fullWidthImage",
            "imageOptions": {
                "imageHeightType": "vh",
                "imageHeight": 50
            },
            "textboxArea": {
                "headerText": "Header for selection1",
                "headingTag": "h1",
                "textBoxOpacity": 15
            }
        },
        {
            "name": "custom",
            "content": "This is a complety different selection, yet there is no name to tell the difference between these two difference objects"
        }
    ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56177210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档