1

How can I pass the arrayIndex as a parameter to the function in onClick? Eg:

{
  key:"someKey"
  type:"array",
  items:[
    {
      "key":"someKey[].itemNo"
    },
    {
      "type":"button",
      "onClick":"someFunction(someKey[arrayIndex].itemNo)"
    }
  ]
}

arrayIndex in "condition" works. But in the function, undefined is what I am getting. I am able to access the entire model or the form's data in someFunction (i.e. i can access someKey), but I need access to the particular item in the array(i.e. someKey[index]).

UPDATED as requested: (Taken from schema form example page)

See the performAction button. I need the email in the button's context to be passed into a function.

Form:

[

  {
    "key": "comments",
    "add": "New",
    "style": {
      "add": "btn-success"
    },
    "items": [
      "comments[].name",
      "comments[].email",
      {
        "key": "comments[].spam",
        "type": "checkbox",
        "title": "Yes I want spam.",
        "condition": "model.comments[arrayIndex].email"
      },
      {
        "key": "comments[].comment",
        "type": "textarea"
      },
      {
        "type":"button",
        "onClick":"performAction(comments[arrayIndex].email)",
        "title":"Perform Action"
      }
    ]
  },
  {
    "type": "submit",
    "style": "btn-info",
    "title": "OK"
  }
]

Schema:

{
  "type": "object",
  "title": "Comment",
  "required": [
    "comments"
  ],
  "properties": {
    "comments": {
      "type": "array",
      "maxItems": 2,
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "title": "Name",
            "type": "string"
          },
          "email": {
            "title": "Email",
            "type": "string",
            "pattern": "^\\S+@\\S+$",
            "description": "Email will be used for evil."
          },
          "spam": {
            "title": "Spam",
            "type": "boolean",
            "default": true
          },
          "comment": {
            "title": "Comment",
            "type": "string",
            "maxLength": 20,
            "validationMessage": "Don't be greedy!"
          }
        },
        "required": [
          "name",
          "comment"
        ]
      }
    }
  }
}
4
  • Please clear the question..post proper items array in JSON. so that i can help you. someKey[].itemNo what does it mean. Commented May 22, 2016 at 20:07
  • Check schema forms demo. It is the way to put array elements. I have updated the question with schema forms example Commented May 23, 2016 at 6:33
  • Thanks..can you please share the code that you tried so far. Commented May 23, 2016 at 9:53
  • Now I am using the "event" to capture the click event, navigate to the parent element and from the parent element I find the value required and use it. This is an ugly way. The code I tried to make work is same as what is listed above, except for the actual fields and values. Commented May 23, 2016 at 10:19

1 Answer 1

1

This was added in v1.0.0-alpha.1 with the addition of arrayIndices.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.