Skip to content

Commit b3f597b

Browse files
Mattia RoccobertonMattia Roccoberton
authored andcommitted
Added addClass action
1 parent 08d16b3 commit b3f597b

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The easiest way to show how this plugin works is looking the examples [below](#e
1919

2020
## Options
2121

22-
Options are passed to fields using *input_html* parameter:
22+
Options are passed to fields using *input_html* parameter as *data* attributes:
2323

2424
- **data-if**: check a condition, values:
2525
+ **checked**: check if a checkbox is checked
@@ -34,6 +34,7 @@ Options are passed to fields using *input_html* parameter:
3434
+ **hide**: hides elements
3535
+ **slide**: hides elements (using sliding)
3636
+ **fade**: hides elements (using fading)
37+
+ **addClass**: adds classes
3738
+ **setValue**: set a value
3839
+ **callback**: call a function
3940
- **data-value**: value to set
@@ -47,15 +48,19 @@ Options are passed to fields using *input_html* parameter:
4748
```rb
4849
form do |f|
4950
f.inputs 'Article' do
50-
f.input :published, input_html: { 'data-if': 'not_checked', 'data-action': 'hide', 'data-target': '.grp1' }
51+
f.input :published, input_html: { data: { if: 'not_checked', action: 'hide', target: '.grp1' } }
5152
f.input :online_date, wrapper_html: { class: 'grp1' }
5253
f.input :position, wrapper_html: { class: 'grp1' }
5354
end
5455
f.actions
5556
end
5657
```
5758

58-
- Set another field value if a string field is blank:
59+
- Add 3 classes (*first*, *second*, *third*) if a checkbox is true:
60+
61+
`f.input :published, input_html: { data: { if: 'checked', action: 'addClass first second third', target: '.grp1' } }`
62+
63+
- Set another field value if a string field is blank (with alternative syntax for data attributes):
5964

6065
`f.input :title, input_html: { 'data-if': 'blank', 'data-action': 'setValue', 'data-target': '#article_position', 'data-value': '10' }`
6166

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ function dfSetupField( el ) {
7575
}
7676
else console.log( 'Warning - activeadmin_dynamic_fields: ' + cb + '() not available [2]' );
7777
}
78+
else if( action.substr( 0, 8 ) == 'addClass' ) {
79+
var classes = action.substr( 8 ).trim();
80+
if( dfEvalCondition( el, args ) ) target.removeClass( classes );
81+
else target.addClass( classes );
82+
el.on( 'change', function( event ) {
83+
if( dfEvalCondition( $(this), args ) ) target.removeClass( classes );
84+
else target.addClass( classes );
85+
});
86+
}
7887
else if( args.fn ) { // function without action
7988
dfEvalCondition( el, args );
8089
el.on( 'change', function( event ) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ActiveAdmin
22
module DynamicFields
3-
VERSION = '0.1.2'
3+
VERSION = '0.1.4'
44
end
55
end

0 commit comments

Comments
 (0)