|
| 1 | +# ActiveAdmin Dynamic Fields [](https://badge.fury.io/rb/activeadmin_dynamic_fields) |
| 2 | + |
| 3 | +An Active Admin plugin to add dynamic behaviors to fields. |
| 4 | + |
| 5 | +Features: |
| 6 | + |
| 7 | +- set conditional checks on fields |
| 8 | +- trigger some actions on other fields |
| 9 | +- create links to load some content in a dialogs |
| 10 | + |
| 11 | +The easiest way to show how this plugin works is looking the examples (below). |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +- Add to your Gemfile: `gem 'activeadmin_dynamic_fields'` |
| 16 | +- Execute bundle |
| 17 | +- Add at the end of your ActiveAdmin styles (_app/assets/stylesheets/active_admin.scss_): |
| 18 | +`@import 'activeadmin/dynamic_fields';` |
| 19 | +- Add at the end of your ActiveAdmin javascripts (_app/assets/javascripts/active_admin.js_): |
| 20 | +`//= require activeadmin/dynamic_fields` |
| 21 | + |
| 22 | +## Options |
| 23 | + |
| 24 | +Options are passed to fields using *input_html* parameter: |
| 25 | + |
| 26 | +- **data-if**: check a condition, values: |
| 27 | + + **checked**: check if a checkbox is checked |
| 28 | + + **not_checked**: check if a checkbox is not checked |
| 29 | + + **blank**: check if a field is blank |
| 30 | + + **not_blank**: check if a field is not blank |
| 31 | +- **data-eq**: check if a field has a specific value |
| 32 | +- **data-not**: check if a field hasn't a specific value |
| 33 | +- **data-function**: check the return value of a custom function |
| 34 | +- **data-target**: target css selector |
| 35 | +- **data-action**: the action to trigger, values: |
| 36 | + + **hide**: hides elements |
| 37 | + + **slide**: hides elements (using sliding) |
| 38 | + + **fade**: hides elements (using fading) |
| 39 | + + **setValue**: set a value |
| 40 | + + **callback**: call a function |
| 41 | +- **data-value**: value to set |
| 42 | +- **data-callback**: custom function for setting a value |
| 43 | +- **data-arg**: argument passed to the custom set function |
| 44 | + |
| 45 | +## Examples of dynamic fields |
| 46 | + |
| 47 | +- A checkbox that hides other fields if false (ex. model *Article*): |
| 48 | + |
| 49 | +```rb |
| 50 | +form do |f| |
| 51 | + f.inputs 'Article' do |
| 52 | + f.input :published, input_html: { 'data-if': 'not_checked', 'data-action': 'hide', 'data-target': '.grp1' } |
| 53 | + f.input :online_date, wrapper_html: { class: 'grp1' } |
| 54 | + f.input :position, wrapper_html: { class: 'grp1' } |
| 55 | + end |
| 56 | + f.actions |
| 57 | +end |
| 58 | +``` |
| 59 | + |
| 60 | +- Set another field value if a string field is blank: |
| 61 | + |
| 62 | +`f.input :title, input_html: { 'data-if': 'blank', 'data-action': 'setValue', 'data-target': '#article_position', 'data-value': '10' }` |
| 63 | + |
| 64 | +- Use a custom function for conditional check (*title_not_empty()* must be available on global scope): |
| 65 | + |
| 66 | +`f.input :title, input_html: { 'data-function': 'title_empty', 'data-action': 'slide', 'data-target': '#article_description_input' }` |
| 67 | + |
| 68 | +```js |
| 69 | +function title_empty( el ) { |
| 70 | + return ( $('#article_title').val().trim() === '' ); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +- Call a callback function as action: |
| 75 | + |
| 76 | +`f.input :published, input_html: { 'data-if': 'checked', 'data-action': 'callback', 'data-callback': 'set_title', 'data-args': '["[unpublished]"]' }` |
| 77 | + |
| 78 | +```js |
| 79 | +function set_title( args ) { |
| 80 | + if( $('#article_title').val().trim() === '' ) { |
| 81 | + $('#article_title').val( args[0] ); |
| 82 | + $('#article_title').trigger( 'change' ); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +- Custom function without action: |
| 88 | + |
| 89 | +`f2.input :category, as: :select, collection: [ [ 'Cat 1', 'cat1' ], [ 'Cat 2', 'cat2' ], [ 'Cat 3', 'cat3' ] ], input_html: { 'data-function': 'on_change_category' }` |
| 90 | + |
| 91 | +```js |
| 92 | +function on_change_category( el ) { |
| 93 | + var target = el.closest( 'fieldset' ).find( '.pub' ); |
| 94 | + target.prop( 'checked', ( el.val() == 'cat2' ) ); |
| 95 | + target.trigger( 'change' ); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Example to open a dialog |
| 100 | + |
| 101 | +Example with 2 models: *Author* and *Article* |
| 102 | + |
| 103 | +Prepare the content dialog - in Active Admin Author config: |
| 104 | + |
| 105 | +```rb |
| 106 | +ActiveAdmin.register Author do |
| 107 | + # ... |
| 108 | + member_action :dialog do |
| 109 | + content = '<dl>' |
| 110 | + [:name, :age, :created_at].each do |field| |
| 111 | + content += "<dt>#{Author.human_attribute_name(field)}:</dt><dd>#{resource[field]}</dd>" |
| 112 | + end |
| 113 | + content += '</dl>' |
| 114 | + render plain: content |
| 115 | + end |
| 116 | + # ... |
| 117 | +end |
| 118 | +``` |
| 119 | + |
| 120 | +Add a link to show the dialog - in Active Admin Article config: |
| 121 | + |
| 122 | +```rb |
| 123 | +ActiveAdmin.register Article do |
| 124 | + # ... |
| 125 | + show do |object| |
| 126 | + attributes_table do |
| 127 | + # ... |
| 128 | + row :author do |
| 129 | + link_to object.author.name, dialog_admin_author_path( object.author ), title: object.author.name, 'data-df-dialog': true, 'data-df-icon': true |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + # ... |
| 134 | +end |
| 135 | +``` |
| 136 | + |
| 137 | +The link url is loaded via AJAX before opening the dialog. |
| 138 | + |
| 139 | +## Do you like it? Star it! |
| 140 | + |
| 141 | +If you use this component just star it. A developer is more motivated to improve a project when there is some interest. |
| 142 | + |
| 143 | +## Contributors |
| 144 | + |
| 145 | +- [Mattia Roccoberton](http://blocknot.es) - creator, maintainer |
| 146 | + |
| 147 | +## License |
| 148 | + |
| 149 | +[MIT](LICENSE.txt) |
0 commit comments