GithubHelp home page GithubHelp logo

pcboy / select-multiple Goto Github PK

View Code? Open in Web Editor NEW

This project forked from krazedkrish/select-multiple

0.0 3.0 0.0 180 KB

"An awesome user-friendlier drop-in replacement for the standard select with multiple attribute activated.

License: MIT License

CSS 6.23% JavaScript 92.63% HTML 1.14%

select-multiple's Introduction

jquery.select-multiple.js

This is an awesome user-friendlier drop-in replacement for the standard <select> with multiple attribute activated.

Features

  • Free (under MIT license)
  • Works in an unobtrusive fashion
  • Fully open sourced
  • Keyboard support
  • Provides some callbacks
  • Fully customizable via CSS
  • Depends on jQuery 1.8+
  • Tiny code ~8kb minified
  • Rails gem

#Dependencies

  • jQuery 1.8+

Usage

HTML

<html>
  <head>
    <link href="path/to/select-multiple.css" media="screen" rel="stylesheet" type="text/css">
  </head>
  <body>
    <select multiple="multiple" id="my-select" name="my-select[]">
      <option value='elem_1'>elem 1</option>
      <option value='elem_2'>elem 2</option>
      <option value='elem_3'>elem 3</option>
      <option value='elem_4'>elem 4</option>
      ...
      <option value='elem_100'>elem 100</option>
    </select>
    <script src="path/to/jquery.select-multiple.js" type="text/javascript"></script>
  </body>
</html>

JavaScript

$('#my-select').selectMultiple()

Options

Name type default description
afterInit function function(container){} Function to call after the selectMultiple initilization.
afterSelect function function(values){} Function to call after one item is selected.
afterDeselect function function(values){} Function to call after one item is deselected.
selectableHeader HTML/Text null Text or HTML to display in the selectable header.
selectableFooter HTML/Text null Text or HTML to display in the selectable footer.
disabledClass String 'disabled' CSS class for disabled items.
selectableOptgroup Boolean false Click on optgroup will select all nested options when set to true.
dblClick Boolean false Replace the defautl click event to select items by the dblclick one.
cssClass String "" Add a custom CSS class to the selectmultiple container.

Methods

.selectMultiple(options)

Activates your content as a selectmultiple. Accepts an optional options object

$('#your-select').selectMultiple({});

Note: You must init the multiple select with $('#your-select').selectMultiple() before calling one of the following methods. .selectMultiple('select', String|Array)

Select the item with the value given in parameter. The value can be either a string ('elem_1') matching the value of the option oran Array of values (['elem_1', 'elem_42']).

$('#your-select').selectMultiple('select', String|Array);

.selectMultiple('deselect', String|Array)

Deselect the item with the value given in parameter. The value can be either a string ('elem_1') matching the value of the option oran Array of values (['elem_1', 'elem_42']).

$('#your-select').selectMultiple('deselect', String|Array);

.selectMultiple('select_all')

Select all elements.

$('#your-select').selectMultiple('deselect_all');

.selectMultiple('deselect_all')

Deselect all items previously selected.

$('#your-select').selectMultiple('select_all');

.selectMultiple('refresh')

Refresh current selectmultiple.

$('#your-select').selectMultiple('refresh');

.selectMultiple('addOption', Hash)

Dynamically add option to the selectmultiple. The options hash is described below:

key type required desription
value String true The value of the option to create
text String true The text of the option to create
index Number false The index where to insert the option. If none given, it will be inserted as last option.
nested String false If there are optgroups you can choose under which optgroup you want to insert the option.
$('#your-select').selectMultiple('addOption', { value: 'test', text: 'test', index: 0, nested: 'optgroup_label' });

Keyboard

key function
Down arrow Select next item in the focused list
Up arrow Select previous item in the focused list
Space Add/remove item depending on which list is currently focused

Pre-selected options

<select id='pre-selected-options' multiple='multiple'>
  <option value='elem_1' selected>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4' selected>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('#pre-selected-options').selectMultiple();

Callbacks

<select id='callbacks' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('#callbacks').selectMultiple({
  afterSelect: function(values){
    alert("Select value: "+values);
  },
  afterDeselect: function(values){
    alert("Deselect value: "+values);
  }
});

Public methods

  • select all / deselect all
  • select 100 elems / deselect 100 elems
  • Add option
  • refresh
<a href='#' id='select-all'>select all</a>
<a href='#' id='deselect-all'>deselect all</a>
<a href='#' id='select-100'>select 100 elems</a>
<a href='#' id='deselect-100'>deselect 100 elems</a>
<select id='public-methods' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2' disabled>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_1000'>elem 100</option>
</select>
$('#public-methods').selectMultiple();
$('#select-all').click(function(){
  $('#public-methods').selectMultiple('select_all');
  return false;
});
$('#deselect-all').click(function(){
  $('#public-methods').selectMultiple('deselect_all');
  return false;
});
$('#select-100').click(function(){
  $('#public-methods').selectMultiple('select', ['elem_0', 'elem_1' ..., 'elem_99']);
  return false;
});
$('#deselect-100').click(function(){
  $('#public-methods').selectMultiple('deselect', ['elem_0', 'elem_1' ..., 'elem_99']);
  return false;
});
$('#refresh').on('click', function(){
  $('#public-methods').selectMultiple('refresh');
  return false;
});
$('#add-option').on('click', function(){
  $('#public-methods').selectMultiple('addOption', { value: 42, text: 'test 42', index: 0 });
  return false;
});

Optgroup

<select id='optgroup' multiple='multiple'>
  <optgroup label='Friends'>
    <option value='1'>Yoda</option>
    <option value='2' selected>Obiwan</option>
  </optgroup>
  <optgroup label='Enemies'>
    <option value='3'>Palpatine</option>
    <option value='4' disabled>Darth Vader</option>
  </optgroup>
</select>
$('#optgroup').selectMultiple({ selectableOptgroup: true });

Disabled attribute

<select id='disabled-attribute' disabled='disabled' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('#disabled-attribute').selectMultiple();

Note: You can also deactivate option one by one by adding disabled attribute to each option you want to disable

bar

Custom headers and footers

<select id='custom-headers' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('#custom-headers').selectMultiple({
  selectableHeader: "<div class='custom-header'>Selectable items</div>",
  selectableFooter: "<div class='custom-header'>Selectable footer</div>"
});

Searchable

Note: This feature is not built-in but depends on an other plugin. I personnally use the excellent quicksearch library, but you can use whatever library you like.

<select id='custom-headers' multiple='multiple'>
  <option value='elem_1' selected>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4' selected>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('.searchable').selectMultiple({
  selectableHeader: "<input type='text' class='search-input' autocomplete='off' placeholder='try \"12\"'>",
  afterInit: function(ms){
    var that = this,
        $selectableSearch = that.$selectableUl.prev(),
        selectableSearchString = '#'+that.$container.attr('id')+' .ms-elem-selectable';

    that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
    .on('keydown', function(e){
      if (e.which === 40){
        that.$selectableUl.focus();
        return false;
      }
    });
  },
  afterSelect: function(){
    this.qs1.cache();
  },
  afterDeselect: function(){
    this.qs1.cache();
  }
});

Documentation

http://krazedkrish.github.io/select-multiple

Demos

http://krazedkrish.github.io/select-multiple/#demos

References:

Based on https://github.com/lou/multi-select/

select-multiple's People

Contributors

krazedkrish avatar artskydj avatar emp3ror avatar rupakraj avatar

Watchers

James Cloos avatar David Hagege 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.