Skip to content

Commit 21f6d3a

Browse files
committed
Expose array index to conditionals.
Makes conditionals work inside an array. Fixes #35
1 parent 7e1a215 commit 21f6d3a

File tree

4 files changed

+78
-10
lines changed

4 files changed

+78
-10
lines changed

docs/index.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,12 @@ They do need a list of ```items``` to have as children.
427427

428428
### conditional
429429

430-
A *conditional* is exactly the same as a *section*, i.e. a ```<div>``` with other form elements in
431-
it, hence they need an ```items``` property. They also need a ```condition``` which is
430+
A *conditional* is exactly the same as a *section*, i.e. a `<div>` with other form elements in
431+
it, hence they need an `items` property. They also need a `condition` which is
432432
a string with an angular expression. If that expression evaluates as thruthy the *conditional*
433433
will be rendered into the DOM otherwise not. The expression is evaluated in the parent scope of
434-
the ```sf-schema``` directive (the same as onClick on buttons) but with access to the current model
435-
under the name ```model```. This is useful for hiding/showing
434+
the `sf-schema` directive (the same as onClick on buttons) but with access to the current model
435+
and current array index under the name `model` and `arrayIndex`. This is useful for hiding/showing
436436
parts of a form depending on another form control.
437437

438438
ex. A checkbox that shows an input field for a code when checked
@@ -476,6 +476,58 @@ Note that angulars two-way binding automatically will update the conditional blo
476476
event handlers and such. The condition need not reference a model value it could be anything in
477477
scope.
478478

479+
The same example, but inside an array:
480+
481+
```javascript
482+
function FormCtrl($scope) {
483+
$scope.persons = []
484+
485+
$scope.schema = {
486+
"type": "object",
487+
"properties": {
488+
"persons": {
489+
"type": "array",
490+
"items": {
491+
"type": "object",
492+
"properties": {
493+
"name": {
494+
"type": "string",
495+
"title": "Name"
496+
},
497+
"eligible": {
498+
"type": "boolean",
499+
"title": "Eligible for awesome things"
500+
},
501+
"code": {
502+
"type":"string"
503+
"title": "The Code"
504+
}
505+
}
506+
}
507+
}
508+
}
509+
}
510+
511+
$scope.form = [
512+
{
513+
"key": "persons",
514+
"items": [
515+
"persons[].name",
516+
"persons[].eligible",
517+
{
518+
type: "conditional",
519+
condition: "persons[arrayIndex].eligible", //or "model.eligable"
520+
items: [
521+
"persons[].code"
522+
]
523+
}
524+
]
525+
}
526+
]
527+
}
528+
```
529+
530+
Note that arrays inside arrays won't work with conditional.
479531

480532
### select and checkboxes
481533

examples/data/array.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020
"pattern": "^\\S+@\\S+$",
2121
"description": "Email will be used for evil."
2222
},
23+
"spam": {
24+
"title": "Spam",
25+
"type": "boolean",
26+
"default": true
27+
},
2328
"comment": {
2429
"title": "Comment",
2530
"type": "string",
2631
"maxLength": 20,
2732
"validationMessage": "Don't be greedy!"
2833
}
2934
},
30-
"required": ["name","email","comment"]
35+
"required": ["name","comment"]
3136
}
3237
}
3338
}
@@ -39,13 +44,24 @@
3944
},
4045
{
4146
"key": "comments",
42-
"add": "New",
43-
"style": {
44-
"add": "btn-success"
47+
"add": "New",
48+
"style": {
49+
"add": "btn-success"
4550
},
4651
"items": [
4752
"comments[].name",
4853
"comments[].email",
54+
{
55+
"type": "conditional",
56+
"condition": "model.comments[arrayIndex].email",
57+
"items": [
58+
{
59+
"key": "comments[].spam",
60+
"type": "checkbox",
61+
"title": "Yes I want spam."
62+
}
63+
]
64+
},
4965
{
5066
"key": "comments[].comment",
5167
"type": "textarea"

src/directives/decorators/bootstrap/array.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h3 ng-show="form.title && form.notitle !== true">{{ form.title }}</h3>
77
type="button" class="close pull-right">
88
<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
99
</button>
10-
<sf-decorator form="copyWithIndex($index)"></sf-decorator>
10+
<sf-decorator ng-init="arrayIndex = $index" form="copyWithIndex($index)"></sf-decorator>
1111
</li>
1212
</ol>
1313
<div class="clearfix" style="padding: 15px;">
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<div ng-if="!form.condition || evalExpr(form.condition,{ model: model })">
1+
<div ng-if="!form.condition || evalExpr(form.condition,{ model: model, 'arrayIndex': arrayIndex })">
22
<sf-decorator ng-repeat="item in form.items" form="item"></sf-decorator>
33
</div>

0 commit comments

Comments
 (0)