json-editor icon indicating copy to clipboard operation
json-editor copied to clipboard

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

Open eriemens opened this issue 7 years ago • 6 comments

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 the NESTED_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?

eriemens avatar Mar 17 '17 12:03 eriemens

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

Pro100AlexHell avatar May 06 '17 19:05 Pro100AlexHell

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

jschlumkoski avatar May 23 '17 14:05 jschlumkoski

+1

gkemp94 avatar Feb 11 '18 11:02 gkemp94

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

dzg avatar Mar 07 '19 07:03 dzg

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

shockthetoast avatar May 14 '19 10:05 shockthetoast

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

ingted avatar May 24 '21 07:05 ingted