# ActiveAdmin Dynamic Fields [![Gem Version](https://badge.fury.io/rb/activeadmin_dynamic_fields.svg)](https://badge.fury.io/rb/activeadmin_dynamic_fields) An Active Admin plugin to add dynamic behaviors to fields. Features: - set conditional checks on fields - trigger some actions on other fields - create links to load some content in a dialog The easiest way to show how this plugin works is looking the examples [below](#examples-of-dynamic-fields). ## Install - Add to your Gemfile: `gem 'activeadmin_dynamic_fields'` - Execute bundle - Add at the end of your ActiveAdmin javascripts (_app/assets/javascripts/active_admin.js_): `//= require activeadmin/dynamic_fields` ## Options Options are passed to fields using *input_html* parameter as *data* attributes: - **data-if**: check a condition, values: + **checked**: check if a checkbox is checked + **not_checked**: check if a checkbox is not checked + **blank**: check if a field is blank + **not_blank**: check if a field is not blank - **data-eq**: check if a field has a specific value - **data-not**: check if a field hasn't a specific value - **data-function**: check the return value of a custom function - **data-target**: target css selector - **data-action**: the action to trigger, values: + **hide**: hides elements + **slide**: hides elements (using sliding) + **fade**: hides elements (using fading) + **addClass**: adds classes + **setValue**: set a value + **callback**: call a function - **data-value**: value to set - **data-callback**: custom function for setting a value - **data-arg**: argument passed to the custom set function ## Examples of dynamic fields - A checkbox that hides other fields if false (ex. model *Article*): ```rb form do |f| f.inputs 'Article' do f.input :published, input_html: { data: { if: 'not_checked', action: 'hide', target: '.grp1' } } f.input :online_date, wrapper_html: { class: 'grp1' } f.input :position, wrapper_html: { class: 'grp1' } end f.actions end ``` - Add 3 classes (*first*, *second*, *third*) if a checkbox is true: `f.input :published, input_html: { data: { if: 'checked', action: 'addClass first second third', target: '.grp1' } }` - Set another field value if a string field is blank (with alternative syntax for data attributes): `f.input :title, input_html: { 'data-if': 'blank', 'data-action': 'setValue', 'data-target': '#article_position', 'data-value': '10' }` - Use a custom function for conditional check (*title_not_empty()* must be available on global scope): `f.input :title, input_html: { 'data-function': 'title_empty', 'data-action': 'slide', 'data-target': '#article_description_input' }` ```js function title_empty( el ) { return ( $('#article_title').val().trim() === '' ); } ``` - Call a callback function as action: `f.input :published, input_html: { 'data-if': 'checked', 'data-action': 'callback', 'data-callback': 'set_title', 'data-args': '["[unpublished]"]' }` ```js function set_title( args ) { if( $('#article_title').val().trim() === '' ) { $('#article_title').val( args[0] ); $('#article_title').trigger( 'change' ); } } ``` - Custom function without action: `f2.input :category, as: :select, collection: [ [ 'Cat 1', 'cat1' ], [ 'Cat 2', 'cat2' ], [ 'Cat 3', 'cat3' ] ], input_html: { 'data-function': 'on_change_category' }` ```js function on_change_category( el ) { var target = el.closest( 'fieldset' ).find( '.pub' ); target.prop( 'checked', ( el.val() == 'cat2' ) ); target.trigger( 'change' ); } ``` ## Example to open a dialog Example with 2 models: *Author* and *Article* Prepare the content dialog - in Active Admin Author config: ```rb ActiveAdmin.register Author do # ... member_action :dialog do content = '