dependent-fields-rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.md +64 -0
- data/lib/assets/javascripts/dependent-fields.js.coffee +48 -0
- metadata +64 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 Lion Vollnhals
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
Dependent Fields for Rails
|
2
|
+
===========
|
3
|
+
|
4
|
+
DependentFields makes it easy to hide or show dependent fields in forms based on select or checkbox values.
|
5
|
+
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
|
10
|
+
1. Add `gem 'dependent-fields-rails'` to your Gemfile.
|
11
|
+
1. Run `bundle install`.
|
12
|
+
1. Add `//= require dependent-fields` to your Javascript manifest file (usually found at `app/assets/javascripts/application.js`).
|
13
|
+
1. Bind events, for example with jquery:
|
14
|
+
|
15
|
+
$ ->
|
16
|
+
DependentFields.bind()
|
17
|
+
|
18
|
+
1. Restart your server and everything should be set up. See Usage below on how to declare your dependent fields in views.
|
19
|
+
|
20
|
+
|
21
|
+
Usage
|
22
|
+
-------
|
23
|
+
|
24
|
+
Dependent Fields may depend on select or checkbox input elements. You have to surround your dependent fields with
|
25
|
+
a `div` with class `js-dependent-fields`.
|
26
|
+
|
27
|
+
The examples below are written in the slim template language with simple_form. But you can use dependent-fields with every other template language out there.
|
28
|
+
|
29
|
+
|
30
|
+
### Depending on a select element
|
31
|
+
|
32
|
+
Note the data attributes on the js-dependent-fields div.
|
33
|
+
|
34
|
+
= simple_form_for(@filing) do
|
35
|
+
= f.input :registration_office, collection: ['habm', 'dpma']
|
36
|
+
|
37
|
+
.js-dependent-fields[data-select-id='filing_registration_office' data-option-value='habm']
|
38
|
+
= f.input :language, collection: ['english', 'german']
|
39
|
+
|
40
|
+
The language selector will only be shown if the user selects 'habm' in the registration office.
|
41
|
+
|
42
|
+
You can also specify multiple option values by seperating them with `|`. For example: `data-option-value='habm|dpma'`
|
43
|
+
|
44
|
+
|
45
|
+
### Depending on a checkbox input element
|
46
|
+
|
47
|
+
Note the data attributes on the js-dependent-fields div.
|
48
|
+
|
49
|
+
= simple_form_for(@filing) do
|
50
|
+
= f.input :priority_enabled
|
51
|
+
|
52
|
+
.js-dependent-fields[data-checkbox-id='filing_priority_enabled' data-checkbox-value='true']
|
53
|
+
= f.input :priority_date
|
54
|
+
= f.input :priority_filing_nr
|
55
|
+
|
56
|
+
The date and filing_nr fields will only be shown if the user checks the priority_enabled field.
|
57
|
+
|
58
|
+
|
59
|
+
### Disabling instead of hiding fields
|
60
|
+
|
61
|
+
Add `data-method='disable'` to the js-dependent-fields div.
|
62
|
+
|
63
|
+
|
64
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Place all the behaviors and hooks related to the matching controller here.
|
2
|
+
# All this logic will automatically be available in application.js.
|
3
|
+
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
|
4
|
+
|
5
|
+
|
6
|
+
toggle = ($parent, showOrHide, method, duration) ->
|
7
|
+
if showOrHide
|
8
|
+
if method == 'disable'
|
9
|
+
# use attr instead of prop, because prop does not work with twitter bootstrap button
|
10
|
+
$parent.find('input,textarea,select,button,.btn').removeAttr('disabled')
|
11
|
+
$parent.find('.select2').select2('enable')
|
12
|
+
else
|
13
|
+
$parent.show(duration)
|
14
|
+
else
|
15
|
+
if method == 'disable'
|
16
|
+
# use attr instead of prop, because prop does not work with twitter bootstrap button
|
17
|
+
$parent.find('input,textarea,select,button,.btn').attr('disabled', 'disabled')
|
18
|
+
$parent.find('.select2').select2('disable')
|
19
|
+
else
|
20
|
+
$parent.hide(duration)
|
21
|
+
|
22
|
+
showOrHideDependentFields = (duration = 'fast') ->
|
23
|
+
$select = $(this)
|
24
|
+
|
25
|
+
# fields that depend on a select option
|
26
|
+
$(".js-dependent-fields[data-select-id=#{$select.attr('id')}]").each ->
|
27
|
+
$this = $(this)
|
28
|
+
# use attr here instead of data because we do not want jquery to cast the string into js types
|
29
|
+
showOrHide = _.contains($this.attr('data-option-value').split('|'), $select.val())
|
30
|
+
toggle($this, showOrHide, $this.data('method'), duration)
|
31
|
+
|
32
|
+
# fields that depend on a checkbox
|
33
|
+
$(".js-dependent-fields[data-checkbox-id=#{$select.attr('id')}]").each ->
|
34
|
+
$this = $(this)
|
35
|
+
showOrHide = $this.data('checkbox-value') == $select.is(':checked')
|
36
|
+
toggle($this, showOrHide, $this.data('method'), duration)
|
37
|
+
|
38
|
+
bind = ->
|
39
|
+
$('select').each _.partial(showOrHideDependentFields, 0)
|
40
|
+
$('select').change showOrHideDependentFields
|
41
|
+
|
42
|
+
$('input[type=checkbox]').each _.partial(showOrHideDependentFields, 0)
|
43
|
+
$('input[type=checkbox]').change showOrHideDependentFields
|
44
|
+
|
45
|
+
|
46
|
+
@DependentFields = {
|
47
|
+
bind
|
48
|
+
}
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dependent-fields-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lion Vollnhals
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: coffee-rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description:
|
31
|
+
email: lion@giantmonkey.de
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- lib/assets/javascripts/dependent-fields.js.coffee
|
37
|
+
- README.md
|
38
|
+
- MIT-LICENSE
|
39
|
+
homepage: https://github.com/vollnhals/dependent-fields-rails
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.8.24
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: DependentFields makes it easy to hide or show dependent fields in forms based
|
63
|
+
on select or checkbox values
|
64
|
+
test_files: []
|