GithubHelp home page GithubHelp logo

tanduong / select2_simple_form Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lndl/select2_simple_form

0.0 2.0 0.0 30 KB

Select2 inputs for SimpleForm

License: MIT License

Ruby 42.52% JavaScript 57.48%

select2_simple_form's Introduction

Select2SimpleForm

Select2SimpleForm is a custom input for SimpleForm which wraps and simplifies the Select2 functionality, without messing around with JS stuff.

Installation

Assuming you are using SimpleForm and a working Select2 plugin (a fast way of doing so is installing and configuring a Rails Select2 plugin, like this one). After having done this, then:

Add this to your Gemfile:

gem 'select2_simple_form', github: 'lndl/select2_simple_form', tag: '0.7.3'

And this into your app/assets/javascripts/application.js (making sure that the following line have to be placed after Select2 JS):

//= require select2_simple_form

And you're done my dear friend!

Usage

All you're gonna do is to use SimpleForm inputs as: :select2.

with collections

Render a Select2 simple collection (selecting a single item):

<%= f.input :countries, as: :select2, collection: Country.all %>

Render a Select2 multiple collection (selecting several items):

<%= f.input :countries, as: :select2, collection: Country.all, multiple: true %>

with ajax

Render a Select2 performing an AJAX request that finds and shows the data (selecting a single item).

<%= f.input :countries, as: :select2, ajax: 'http://route/to/get/data/for/input' %>

Render a Select2 performing an AJAX request that finds and shows the data (selecting several items).

<%= f.input :countries, as: :select2, ajax: 'http://route/to/get/data/for/input', multiple: true %>

i18n

Internationalization (i18n) can be achieved yielding a hash whose keys are equals to js format functions. There are two ways:

global way

Using a initializer:

Select2SimpleForm.configure do |config|
  # Spanish messages
  config.i18n = {
    formatNoMatches:       "No se encontraron resultados",
    formatInputTooShort:   "Por favor ingrese :n: caracter/es",
    formatInputTooLong:    "Por favor elimine :n: caracter/es",
    formatSelectionTooBig: "Sólo puede seleccionar :limit: elemento/s",
    formatLoadMore:        "Cargando más resultados...",
    formatSearching:       "Buscando..."
  }
end

in particular input

<%= f.input :persona_id, as: :select2, ajax: search_path, i18n: { formatNoMatches: 'No se ha encontrado a la persona buscada' } %>

i18n messages can have parameters (check out the 'global' example), like :n:, :limit:, in order to render the proper quantities in JS.

select2_simple_form's People

Contributors

lndl avatar ncuesta avatar silviorelli avatar yuanping avatar tanduong avatar z4ppy avatar

Watchers

 avatar  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.