Skip to content

Commit cf55558

Browse files
committed
♻ Fixed hide action; added gtarget attribute
1 parent 50501e2 commit cf55558

4 files changed

Lines changed: 13 additions & 10 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
_misc/
2-
2+
Gemfile.lock
33
*.orig
4+

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Options are passed to fields using *input_html* parameter as *data* attributes:
2929
+ **not_blank**: check if a field is not blank
3030
- **data-eq**: check if a field has a specific value
3131
- **data-not**: check if a field hasn't a specific value
32-
- **data-target**: target css selector
32+
- **data-target**: target css selector (from parent fieldset, look for the closest match)
33+
- **data-gtarget**: target css selector globally
3334
- **data-action**: the action to trigger, values:
3435
+ **hide**: hides elements
3536
+ **slide**: hides elements (using sliding)

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ function dfEvalCondition( el, args ) {
55
else console.log( 'Warning - activeadmin_dynamic_fields: ' + args.fn + '() not available [1]' );
66
}
77
else if( args.if == 'checked' ) {
8-
return !el.is(':checked');
8+
return el.is(':checked');
99
}
1010
else if( args.if == 'not_checked' ) {
11-
return el.is(':checked');
11+
return !el.is(':checked');
1212
}
1313
else if( args.if == 'blank' ) {
1414
return el.val().length === 0 || !el.val().trim();
@@ -33,13 +33,14 @@ function dfSetupField( el ) {
3333
args.eq = el.data( 'eq' );
3434
args.not = el.data( 'not' );
3535
args.fn = el.data( 'function' );
36-
if( el.data( 'target' ) ) target = el.closest( 'fieldset' ).find( el.data( 'target' ) )
36+
if( el.data( 'target' ) ) target = el.closest( 'fieldset' ).find( el.data( 'target' ) ); // closest find for has many associations
37+
else if( el.data( 'gtarget' ) ) target = $( el.data( 'gtarget' ) );
3738
if( action == 'hide' ) {
38-
if( dfEvalCondition( el, args ) ) target.show();
39-
else target.hide();
39+
if( dfEvalCondition( el, args ) ) target.hide();
40+
else target.show();
4041
el.on( 'change', function( event ) {
41-
if( dfEvalCondition( $(this), args ) ) target.show();
42-
else target.hide();
42+
if( dfEvalCondition( $(this), args ) ) target.hide();
43+
else target.show();
4344
});
4445
}
4546
else if( action == 'slide' ) {
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.2.0'
3+
VERSION = '0.2.1'
44
end
55
end

0 commit comments

Comments
 (0)