Git Product home page Git Product logo

dependent-fields-rails's Introduction

Dependent Fields for Rails

DependentFields makes it easy to hide or show dependent fields in forms based on select or checkbox values.

Installation

  1. Add gem 'dependent-fields-rails' to your Gemfile.

  2. Run bundle install.

  3. Add //= require dependent-fields to your Javascript manifest file (usually found at app/assets/javascripts/application.js).

  4. Bind events, for example with jquery:

     $(document).ready(function() {
         DependentFields.bind()
     });
    
  5. Restart your server and everything should be set up. See Usage below on how to declare your dependent fields in views.

Dependencies

Be sure to include underscorejs and jquery in your page.

Usage

Dependent Fields may depend on select or checkbox input elements. You have to surround your dependent fields with a div with class js-dependent-fields.

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.

Depending on a select element

Note the data attributes on the js-dependent-fields div.

= simple_form_for(@filing) do
    = f.input :registration_office, collection: ['habm', 'dpma']

    .js-dependent-fields[data-select-id='filing_registration_office' data-option-value='habm']
        = f.input :language, collection: ['english', 'german']

The language selector will only be shown if the user selects 'habm' in the registration office.

You can also specify multiple option values by seperating them with |. For example: data-option-value='habm|dpma'

Depending on a checkbox input element

Note the data attributes on the js-dependent-fields div.

= simple_form_for(@filing) do
    = f.input :priority_enabled

    .js-dependent-fields[data-checkbox-id='filing_priority_enabled' data-checkbox-value='true']
      = f.input :priority_date
      = f.input :priority_filing_nr

The date and filing_nr fields will only be shown if the user checks the priority_enabled field.

Depending on a radio input element

Relevant radio inputs are selected by name and not by id, because radio inputs in the same group have different ids but the same name. Note the data attributes on the js-dependent-fields div.

= simple_form_for(@filing) do
    = f.input :registration_office, collection: ['habm', 'dpma'], as: :radio_buttons

    .js-dependent-fields[data-radio-name='filing[registration_office]' data-radio-value='habm']
        = f.input :language, collection: ['english', 'german']

The language selector will only be shown if the user selects 'habm' in the registration office.

You can also specify multiple option values by seperating them with |. For example: data-radio-value='habm|dpma'

Disabling instead of hiding fields

Add data-method='disable' to the js-dependent-fields div.

Minimal Demo

http://jsfiddle.net/mwhSt/

Changelog

0.4.2

  • make it compatible with rails4. thanks to mcbridejc for the pull request.
  • fix an example in the README. thanks to michaelkeenan for the pull request.

0.4.1

  • fix escaping of attribute names. thanks to nagyt234 for the pull request.

dependent-fields-rails's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.