jdorn / json-editor

JSON Schema Based Editor
MIT License
5.81k stars 1.08k forks source link

How to watch a property of an array item within an array #741

Open eriemens opened 7 years ago

eriemens commented 7 years ago

Assume the following schema:

{
  "id": "test-array.json",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Array of array",
  "type": "object",
  "properties": {
    "myArray": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/myArrayType"
      }
    },
    "myDefault": {
      "type": "string",
      "watch": {
        "my-array": "NESTED_ID.nestedArray"
      },
      "enumSource": [
        {
          "source": "my-array",
          "value": "{{item.p}}"
        }
      ]
    }
  },
  "definitions": {
    "myArrayType": {
      "type": "object",
      "id": "NESTED_ID",
      "properties": {
        "nestedArray": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "p": {"type": "string"}
            }
          }
        }
      }
    }
  }
}

The myDefault properties would need to select from a list of "p" values, where "p" is a string property of a nested array in myArray. Therefore, I added a watch to the nested array using theNESTED_ID id of the items in myArray as source, and using the {{item.p}} template in the value of the enumSource.

Unfortunately, this doesn't work: the json editor outputs and empty array of values in the myDefault property, as shown here. Can you shed some light on how I can watch and use values in a nested array?

Pro100AlexHell commented 7 years ago

+1 after my tests "watch" keyword is very-low-usable, and need some rework with functionality.

jschlumkoski commented 7 years ago

+1 as well, I've been struggling to get this to work for a while now with no luck

gkemp94 commented 6 years ago

+1

dzg commented 5 years ago

+1 Need a way to redraw the form without destroying, which also destroys the watch!

shockthetoast commented 5 years ago

@dzg This repo is no longer active. All work has been moved to this official fork: https://github.com/json-editor/json-editor

ingted commented 3 years ago

Hi guys,

I found this is insteresting:

schema:

{
  "type": "object",
  "properties": {
    "possible_colors": {
      "type": "array",
      "format": "table",
      "items": {
        "type": "object",
        "properties": {
          "text": {
            "type": "array",
            "format": "table",
            "items": {
              "type": "object",
              "properties": {
                "text": {
                  "type": "string",
                  "title": "File"
                }
              }
            }
          },
          "primary_color": {
            "type": "string",
            "watch": {
              "colors": "possible_colors"
            },
            "enumSource": [
              {
                "source": "colors",
                "value": "{{item.text}}"
              }
            ]
          }
        }
      }
    },
    "primary_color": {
      "type": "string",
      "watch": {
        "colors": "possible_colors"
      },
      "enumSource": [
        {
          "source": "colors",
          "value": "{{item.text}}"
        }
      ]
    }
  }
}

image