json-schema-form / angular-schema-form

Generate forms from a JSON schema, with AngularJS!
https://json-schema-form.github.io/angular-schema-form
MIT License
2.47k stars 653 forks source link

How can I get the value of arrayIndex inside onChange method? #778

Closed EtiAggarwal closed 7 years ago

EtiAggarwal commented 7 years ago

Enhancement

As a user/developer, when I call the onChange method I should be able to access the arrayIndex

@json-schema-form/angular-schema-form-lead

joelwkent commented 7 years ago

Hi, currently it is not possible to get the arrayIndex in an onChange function but there are plans to add it. I don't have the related issue/PR IDs to hand but I will try to dig them out and list them here shortly so you can can follow, and if you fancy, get involved in the developments.

EtiAggarwal commented 7 years ago

Thanks

raquintero commented 7 years ago

@joelwkent I would love to get involved with this too, as I need this asap or I will have to completely abandon ASF and move in another direction. I've burned way too many hours fighting ASF.

I mean at this point, if someone who really knows ASF can just explain high level what needs to be done in order for input fields to have access to Array indexes, would be a tremendous help. Heck, is there even a jquery hack to get the index that is created for each tab's class?

joelwkent commented 7 years ago

Hi @raquintero, apologies for not updating you but @Anthropic has made some progress on this, see this commit. I have not had a chance to try it out yet but you could take a look at it, pull it down and see if it works for you.

raquintero commented 7 years ago

@joelwkent @Anthropic Awesome! I will take a look asap!!!

Anthropic commented 7 years ago

@joelwkent @raquintero I have found a bug in it, but it is almost there, I am looking at it now to try to fix it, currently getting the wrong index for the last key. I think I have the controller or directive on the wrong tag. Hopefully find a solution today.

raquintero commented 7 years ago

As an update for this issue, I have added the change identified here: 714 and now in my onChange event, I pass (modelValue, form, scope). I rearranged the scope to the last value, so as not to break my existing onChange functions.

I use it in my onChange function defined in my Controller like this:

var idx = scope.$parent.$parent.$index; // to get the index of the array I am currently in var parentIdx = scope.$parent.$parent.$parent.$parent.$parent.$parent.$index; // to get my parent's index.

I use Chrome Tools to track down how many levels of $parent I need to go.
This is obviously a hack, but it works for now. Just fragile.

EtiAggarwal commented 7 years ago

Is there any update on this ?

Anthropic commented 7 years ago

It is working to some degree in the latest code I have but not ready for a release yet sorry I have some other bugs to overcome.

Anthropic commented 7 years ago

The latest 1.0.0-alpha release should have arrayIndices / $i as an array of the array path so I will close this as that change is tracked in another issue already.