Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <style type='text/css'>
- #ddList-fruitTypeList > a > label, #ddList-fruitTypeList > a > small { -ms-text-overflow: ellipsis; -o-text-overflow: ellipsis; text-overflow: ellipsis; white-space: nowrap; }
- .ddListContainer { position: relative; display: inline-block; text-align: center; font: 1.1em/1.4 "Georgia"; letter-spacing: 0.04em; }
- /* Fonts setup */
- .ddListContainer > a > label, .ddListContainer > ul li a > label, .ddListContainer > a > small,
- .ddListContainer > ul li a > small { font: 1em "Verdana"; }
- /* The 'selection' rectangle of the dropdownlist */
- .ddListContainer > a { background: #000000; border: solid 1px #000000; border-radius: 4px; cursor: pointer; overflow: hidden; display: block; padding: 5px 0 5px 0; color: yellow; }
- .ddListContainer > a:hover { border: solid 1px #000000; color: #f2e8c9; }
- .ddListContainer.ddListDisabled > a { border: solid 2px #e0e0e0; cursor: default; background: #f8f8f8; color: #999; }
- /* The 'selection' rectangle of the dropdownlist when open */
- .ddListIsOpen > a, .ddListIsOpen > a:hover { border:solid 1px #000000; background-color:#000000; color:#f2e8c9; }
- /* The right arrow in the 'selection' rectangle of the dropdownlist */
- .ddListArrow { width: 0; height: 0; position: absolute; right: 8px; top: 50%; border: solid 4px transparent; border-top: solid 7px #f2e8c9; }
- .ddListDisabled .ddListArrow { border-top: solid 5px #999; }
- .ddListIsOpen .ddListArrow { margin-top: -10px; border: solid 4px transparent !important; border-bottom: solid 7px #f2e8c9 !important; }
- /* The 'options' list of the dropdownlist */
- .ddListContainer > ul { display: none; position: absolute; z-index: 2000; border: solid 1px #000000; border-radius: 4px; border-top: none; list-style: none; margin: 0; padding: 0; background: #000000; overflow: auto; -moz-box-shadow: 0 1px 5px #ddd; -webkit-box-shadow: 0 1px 5px #ddd; box-shadow: 0 1px 5px #ddd; }
- .ddListContainer > ul li { margin-bottom: 0; }
- .ddListContainer > ul > li:last-child > a { border-bottom: none; }
- /* Any 'option' inside the 'options' list of the dropdownlist */
- .ddListContainer > ul li a { padding: 5px 0 5px 0; display: block; overflow: hidden; text-decoration: none; color: #b5a77d; cursor: pointer; -webkit-transition: all 0.25s ease-in-out; -moz-transition: all 0.25s ease-in-out; -o-transition: all 0.25s ease-in-out; -ms-transition: all 0.25s ease-in-out; transition: all 0.25s ease-in-out; }
- .ddListContainer > ul li a:hover { background: grey; color: #ffffff; }
- /* A 'selected' option inside the 'options' list of the dropdownlist */
- .ddListOptionIsSelected { background: #000000; }
- /* Additional styling of text, img, description for 'selection' and 'option' */
- .ddListContainer:not(.ddListDisabled) > a > label,
- .ddListContainer:not(.ddListDisabled) > ul li a > label { cursor: pointer; }
- .ddListContainer > a > label { padding-right: 15px; }
- .ddListContainer > a > small { display:block; overflow: hidden; padding-right: 15px; }
- .ddListContainer > ul li a > small { display:block; overflow: hidden; }
- .ddListContainer > a > img, .ddListContainer > ul li a > img { vertical-align: middle; float: left; margin-left: 5px; max-width: 64px; }
- </style>
- </head>
- <body>
- <div id="ddList-fruitTypeList" >
- <select id="fruitTypeList" name="cargoType" >
- <option value="1" selected="selected" data-description="oranges">Yellow</option>
- <option value="2" data-description="bananas">White</option>
- <option value="3" data-description="lemon">Green</option>
- </select>
- </div>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
- <script type="text/javascript">
- $(function() { $('#fruitTypeList').ddlist({
- width: 200,
- onSelected: function (index, value, text) {
- if (value == 1) { /* do something */ }
- if (value == 2) { alert ("second choice"); }
- }
- });
- });
- // ----------------------------------------------- Plugin ----------------------------------------
- ; (function ($, win, document, undefined) {
- var version = '1.1.1';
- var pluginName = 'ddlist';
- var ENTER = 13,
- ESCAPE = 27,
- SPACE = 32,
- UP = 38,
- DOWN = 40;
- function Plugin(element, options) {
- // Get the main element
- this.element = element;
- this.selObj = $(element);
- this.isOpen = false;
- this.settings = {
- width: 260,
- selectionIndex: 0,
- disabled: false,
- showSelectionTextOnly: false,
- onSelectedOnInit: false,
- onSelected: function (index, value, text) { },
- itemsSource: null
- };
- this._init(options);
- };
- Plugin.prototype = {
- // Select index
- _selectIndex: function(index) {
- // Remove current 'ddListOptionIsSelected' class
- this.ddOptions.find('a').removeClass('ddListOptionIsSelected');
- // Set 'ddListOptionIsSelected' class to element that corresponds with index
- var selectedOption = this.ddOptions.find('a').eq(index);
- selectedOption.addClass('ddListOptionIsSelected');
- // Update selected index/value/text
- var optionData = this.options[index];
- this.selectedIndex = index;
- this.selectedValue = optionData.value;
- this.selectedText = optionData.text;
- // Update <select> element (if we are not dealing with json-provided data)
- if (this.settings.itemsSource == null) {
- // Clear current 'selected'
- this.selObj.find('option').attr('selected', false);
- // Set new 'selected'
- var selOption = this.selObj.find('option').eq(index);
- selOption.attr('selected', true);
- }
- // Update ddListSelection element
- if (this.settings.showSelectionTextOnly) {
- this.ddSelection.html(optionData.text);
- }
- else {
- this.ddSelection.html((optionData.imageSrc ? '<img src="' + optionData.imageSrc + '" />' : '') +
- (optionData.text ? '<label>' + optionData.text + '</label>' : '') +
- (optionData.description ? '<small>' + optionData.description + '</small>' : ''));
- }
- },
- _open: function() {
- // Close possible other open dropdown lists
- var otherOpenListObjs = $('.ddListIsOpen').not(this.ddListObj);
- if (otherOpenListObjs.length != 0) {
- otherOpenListObjs.removeClass('ddListIsOpen');
- otherOpenListObjs.find('> ul').slideUp(50);
- }
- this.ddListObj.find('> ul').slideDown('fast');
- this.ddListObj.addClass('ddListIsOpen');
- this.isOpen = true;
- },
- _close: function () {
- // Close drop down
- this.ddListObj.removeClass('ddListIsOpen');
- this.ddListObj.find('> ul').slideUp(50);
- this.isOpen = false;
- },
- _enable: function () {
- var self = this;
- this.settings.disabled = false;
- this.ddListObj.removeClass('ddListDisabled');
- // Bind event handlers
- // this.ddSelection.on('click.ddlist', function () {
- this.ddSelection.parent().on('click.ddlist', function (e) {
- if (self.isOpen) {
- self._close();
- }
- else {
- self._open();
- }
- e.stopPropagation();
- });
- this.ddOptions.find('a').on('click.ddlist', function (e) {
- // Select (new) index
- self._selectIndex($(this).closest('li').index());
- // Close
- self._close();
- // Callback function on selection
- self.settings.onSelected.call(self, self.selectedIndex, self.selectedValue, self.selectedText);
- e.stopPropagation();
- });
- //Click anywhere to close
- this.ddListObj.on('click.ddlist', function (e) {
- e.stopPropagation();
- });
- $('body').on('click.ddlist-' + this.selObj.attr('id'), function () {
- self._close();
- });
- },
- _disable: function () {
- this.settings.disabled = true;
- // Unbind event handlers
- $('body').off('.ddlist-' + this.selObj.attr('id'));
- this.ddListObj.off('.ddlist');
- this.ddOptions.find('a').off('.ddlist');
- // this.ddSelection.off('.ddlist');
- this.ddSelection.parent().off('.ddlist');
- this.ddListObj.addClass('ddListDisabled');
- },
- // Add items to the ddOptions list
- _bindListItems: function (disableAccess) {
- var self = this;
- // Add options to <ul> element
- $.each(this.options, function (index, item) {
- if (item.selected) { self.settings.selectionIndex = index; }
- self.ddOptions.append('<li>' + '<a>' + (item.imageSrc ? ' <img src="' + item.imageSrc + '" />' : '') + (item.text ? ' <label>' + item.text + '</label>' : '') + (item.description ? ' <small>' + item.description + '</small>' : '') + '</a>' + '</li>');
- });
- // Bind event handlers
- if (!disableAccess) { this._enable(); }
- else { this.ddListObj.addClass('ddListDisabled'); }
- // Show 'selected' item in the top selection element
- this._selectIndex(this.settings.selectionIndex);
- // Invoke 'onSelected' callback (unless configured not to do that during initialization)
- if (this.settings.onSelectedOnInit) {
- this.settings.onSelected.call(this, this.selectedIndex, this.selectedValue, this.selectedText);
- }
- },
- // Unbind event handlers and remove items from the ddOptions list
- _unbindListItems: function () {
- this._disable();
- this.ddOptions.empty();
- },
- // Initialize
- _init: function (options) {
- this.isOpen = false;
- this.settings = $.extend({}, this.settings, options);
- var self = this;
- // Load data from HTML select options or from a user-provided json object
- // in a list options data array
- this.options = [];
- if (this.settings.itemsSource == null) {
- this.selObj.find('option').each(function () {
- var optObj = $(this);
- self.options.push({
- text: $.trim(optObj.text()),
- value: optObj.val(),
- selected: (optObj.attr('selected') == 'selected'),
- description: optObj.data('description'),
- imageSrc: optObj.data('imagesrc')
- });
- });
- }
- else {
- this.options = this.settings.itemsSource;
- }
- // Hide select element
- this.selObj.hide();
- // Insert select replacement container
- this.ddListObj = $('<div id="ddList-' + this.selObj.attr('id') + '" class="ddListContainer"><a></a><span class="ddListArrow"></span><ul></ul></div>');
- this.ddListObj.insertAfter(this.selObj);
- // Get newly created <a> element (which shows the selected item) and set its width
- this.ddSelection = this.ddListObj.find('> a');
- this.ddSelection.css({ width: this.settings.width });
- // Get newly created <ul> element (which will hold the list options) and set its width
- this.ddOptions = this.ddListObj.find('> ul');
- this.ddOptions.css({ width: this.settings.width });
- // Insert all list items, bind event handlers and set 'selected' item
- if (!this.settings.disabled) {
- this.settings.disabled = this.selObj.is(':disabled');
- }
- this._bindListItems(this.settings.disabled);
- },
- // Select a given option
- // Arguments:
- // - argsArray: An array where the first element is an object in which the user can
- // mark the item to be selected in 3 ways:
- // - through property 'index'
- // - through property 'text'.
- // - through property 'value'.
- select: function (argsArray) {
- var arg = argsArray[0];
- if (arg.index) {
- this.settings.selectionIndex = arg.index;
- this._selectIndex(arg.index, false);
- }
- else if (arg.text) {
- // Find text in the list options data array
- for (var i = 0; i < this.options.length; i++) {
- if (this.options[i].text == arg.text) {
- this.settings.selectionIndex = i;
- this._selectIndex(i);
- break;
- }
- }
- }
- else if (arg.value) {
- // Find value in the list options data array
- for (var i = 0; i < this.options.length; i++) {
- if (this.options[i].value == arg.value) {
- this.settings.selectionIndex = i;
- this._selectIndex(i);
- break;
- }
- }
- }
- },
- // Enable/disable dropdown
- // Arguments:
- // - argsArray: An array where the first element is an boolean.
- // - When set to true the dropdown list is enabled
- // - When set to false the dropdown list is disabled
- enable: function (argsArray) {
- var isEnable = argsArray[0];
- if (isEnable) {
- this._enable();
- }
- else {
- this._disable();
- }
- },
- // isDisabled
- isDisabled: function () {
- return this.settings.disabled;
- },
- // setItemsSource
- // Arguments:
- // - argsArray: An array where the first element is a reference to a json
- // ItemsSource array.
- setItemsSource: function (argsArray) {
- // Remove event handlers and unbind the current list items
- var disableAccess = this.settings.disabled;
- this._unbindListItems();
- // Set itemsSource
- this.options = argsArray[0];
- // Insert all list items, bind event handlers and set 'selected' item
- this._bindListItems(disableAccess);
- },
- };
- $.fn[pluginName] = function (methodOrOptions) {
- var instance = $(this).data(pluginName);
- if (instance && methodOrOptions.indexOf('_') != 0) {
- return instance[methodOrOptions](Array.prototype.slice.call(arguments, 1));
- }
- if (typeof methodOrOptions === 'object' || !methodOrOptions) {
- instance = new Plugin(this, methodOrOptions);
- $(this).data(pluginName, instance);
- return $(this);
- }
- $.error('Wrong call to ' + pluginName);
- };
- })(jQuery);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement