Skip to content

Commit ac6cee4

Browse files
committed
Array index and array indices for onClick
1 parent c13017b commit ac6cee4

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/directives/field.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,23 @@ angular.module('schemaForm').directive('sfField',
4848
};
4949

5050
scope.buttonClick = function($event, form) {
51+
var arrayDepth = form.key.filter(function(e) { return e === '' }).length;
52+
var arrayIndices = (arrayDepth > 1 ? Array(arrayDepth - 1).join('$parent.$parent.$parent.') + '$parent.$parent.$index,' : '');
53+
for (var i = arrayDepth; i > 2; i--) {
54+
arrayIndices += Array(i - 1).join('$parent.$parent.$parent.') + '$index,';
55+
}
56+
arrayIndices += '$index';
57+
arrayIndices = scope.$eval('[' + arrayIndices + ']');
58+
5159
if (angular.isFunction(form.onClick)) {
52-
form.onClick($event, form);
60+
61+
form.onClick($event, form, arrayIndices[arrayDepth-1], arrayIndices);
5362
} else if (angular.isString(form.onClick)) {
5463
if (sfSchema) {
5564
//evaluating in scope outside of sfSchemas isolated scope
56-
sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form});
65+
sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form, arrayIndex: arrayIndices[arrayDepth-1], arrayIndices: arrayIndices});
5766
} else {
58-
scope.$eval(form.onClick, {'$event': $event, form: form});
67+
scope.$eval(form.onClick, {'$event': $event, form: form, arrayIndex: '$index', arrayIndices: '[' + arrayIndices + ']'});
5968
}
6069
}
6170
};

src/services/decorators.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ angular.module('schemaForm').provider('schemaFormDecorators',
7474
};
7575

7676
scope.buttonClick = function($event, form) {
77+
var arrayIndices = form.key.filter(function(e) { return e === parseInt(e, 10)});
78+
7779
if (angular.isFunction(form.onClick)) {
78-
form.onClick($event, form);
80+
form.onClick($event, form, arrayIndices[arrayIndices.length-1], arrayIndices);
7981
} else if (angular.isString(form.onClick)) {
8082
if (sfSchema) {
8183
//evaluating in scope outside of sfSchemas isolated scope
82-
sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form});
84+
sfSchema.evalInParentScope(form.onClick, {'$event': $event, form: form, arrayIndex: arrayIndices[arrayIndices.length-1], arrayIndices: arrayIndices});
8385
} else {
84-
scope.$eval(form.onClick, {'$event': $event, form: form});
86+
scope.$eval(form.onClick, {'$event': $event, form: form, arrayIndex: arrayIndices[arrayIndices.length-1], arrayIndices: arrayIndices});
8587
}
8688
}
8789
};

0 commit comments

Comments
 (0)