SchemaForm 数组条件

SchemaForm array conditional

背景

我正在使用 http://schemaform.io/

制作表格

设置

我正在尝试制作一组​​对象,用户可以使用表单制作这些对象。因此,用户可以根据需要向数组中添加任意数量的项目。

项目数组包含一个类型,然后是另一个字段,具体取决于该类型。

如果用户单击 REST,我希望它提供一个名为 method 的字段。

如果用户单击 SSH,我希望它提供一个名为 path 的字段。

到目前为止的代码

架构

{
  "type": "object",
  "title": "Command Asset",
  "properties": {
    "name": {
      "title": "Name",
      "type": "string"
    },
    "commands": {
      "type": "array",
      "title": "Actions",
      "items": {
        "type": "object",
        "properties": {
          "commandType": {
            "title": "Command Type",
            "type": "string",
            "enum": [
              "REST",
              "SSH"
            ]
          },
          "path": {
            "title": "Path",
            "type": "string"
          },
          "method": {
            "title": "Method",
            "type": "string"
          }
        }
      }
   }
  }
}

表格

[
  {
    "type": "help",
    "helpvalue": "<h5>Command</h5>"
  },
  "name",
  {
    "title":"Command",
    "key": "commands",
    "items": [
      "commands[].commandType",
      {
        "type": "conditional",
        "condition": "modelData.commands[0].commandType=='SSH'",
        "items": [
          {
            "key": "commands[].path"
          }
        ]
      },
      {
        "type": "conditional",
        "condition": "modelData.commands[0].commandType=='REST'",
        "items": [
          {
            "key": "commands[].method"
          }
        ]
      }
    ]
  }
]

可以在这里测试这段代码:http://schemaform.io/examples/bootstrap-example.html

问题

如你所见,我现在的代码使所有项的次要属性(pathmethod)依赖于数组 commandType 中的第一项(位于[0]), 但我希望它依赖于相应项目的 commandType。因此,如果第一项具有 commandType REST,它会提供一个 method 字段,如果第二项具有 S​​SH 命令类型,它会提供一个 path 字段,依此类推。

我找到了答案。

[0] 替换为 [arrayIndex]

我是从这里找到的:https://github.com/json-schema-form/angular-schema-form/commit/21f6d3ab64435b032456dfe19e03f96b29366320