mrt:fancy-select

v0.0.2Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

FancySelect

A better select for discerning web developers everywhere, lovingly crafted by Octopus Creative. You can download it here, or check out the demo.

Installation

mrt add fancy-select

I did not include the CSS because most people will probably want to style it themselves

Basic Usage

FancySelect is easy to use. Just include jQuery or Zepto, target any select element on the page, and call .fancySelect() on it. If the select has an option with no value, it'll be used as a sort of placeholder text.

By default, FancySelect uses native selects and styles only the trigger on iOS devices. To override this, pass an object with forceiOS set to true when initializing it.

FancySelect also passes any classes specified in the select's data-class attribute, which you can use to style specific FancySelect instances.

HTML

1<select class="basic">
2  <option value="">Select something…</option>
3  <option>Lorem</option>
4  <option>Ipsum</option>
5  <option>Dolor</option>
6  <option>Sit</option>
7  <option>Amet</option>
8</select>

JavaScript

1$('.basic').fancySelect();

Updating Options

If the options in your select change after initializing FancySelect, you can tell it to rebuild the list of options by triggering update.fs on the select element.

JavaScript

1var mySelect = $('.my-select');
2
3mySelect.fancySelect();
4
5mySelect.append('<option>Foo</option><option>Bar</option>');
6
7mySelect.trigger('update.fs');

Enabling/Disabling

FancySelect will automatically pick up your select's disabled property on initialization. If you need to enable or disable it again later, you can do that by triggering enable.fs or disable.fs on your select element.

HTML

1<select class="my-select" disabled>
2  <option>First Option</option>
3  <option>Second Option</option>
4</select>

JavaScript

1var mySelect = $('.my-select');
2mySelect.fancySelect(); // currently disabled because of html property
3
4// later…
5mySelect.trigger('enable.fs'); // now enabled
6
7// even later…
8mySelect.trigger('disable.fs'); // now disabled again

Templates

If you need to do something fancy with the trigger or the individual options, you can use triggerTemplate or optionTemplate, which are both functions passed an option element (jQuery-wrapped) and returning an HTML string to render.

HTML

1<select class="bulbs">
2  <option data-icon="old">Incandescent</option>
3  <option data-icon="curly">CFL</option>
4  <option data-icon="work">Halogen</option>
5</select>
1$('.bulbs').fancySelect({
2  optionTemplate: function(optionEl) {
3    return optionEl.text() + '<div class="icon-' + optionEl.data('icon') + '"></div>';
4  }
5}
6})