Code Monkey home page Code Monkey logo

jquery-multiselect's Introduction

jQuery MultiSelect

GitHub release npm jsDelivr GitHub issues GitHub pull requests

Turn a multiselect list into a nice and easy to use list with checkboxes. This plugin is simply an alternative interface for the native select list element. When you check an option in the plugin the value is selected on the native list. This allows the value to be submitted in a form as well as retreived through normal POST/GET and javascript methods.

Support

I only provide limited support through the Github Issues area. DO NOT ask for support via email, socialmedia, or other means. Also check the closed issues before opening a new issue.

Design Methodology

This plugin is not meant to be a full featured multiselect plugin. I envision this as a multiselect plugin that contains a base set of features that can be enhanced using callbacks by the developer. Because of this I am hesitant on adding functionality but am open to adding callbacks where it makes sense. But feel free to open an issue and suggest a feature request if you feel that most people will benefit from it.

Demo

http://springstubbe.us/projects/jquery-multiselect/

Usage

// BASIC
$('select[multiple]').multiselect();

// 4 COLUMNS with custom placeholder text
$('select[multiple]').multiselect({
    columns: 4,
    texts: {
        placeholder: 'Select options'
    }
});

// RELOAD multiselect (in case you modify options or selected options in the native select list since loading the plugin)
$('select[multiple]').multiselect('reload');

// DYNAMICALLY LOAD OPTIONS
$('select[multiple]').multiselect( 'loadOptions', [{
    name   : 'Option Name 1',
    value  : 'option-value-1',
    checked: false,
    attributes : {
        custom1: 'value1',
        custom2: 'value2'
    }
},{
    name   : 'Option Name 2',
    value  : 'option-value-2',
    checked: false,
    attributes : {
        custom1: 'value1',
        custom2: 'value2'
    }
}]);

Options

Option Values Default Description
columns int 1 # of columns to show options
search bool false enable option search/filering
searchOptions object
- delay int 250 time (in ms) between keystrokes until search happens
- showOptGroups bool false show option group titles if no options remaining
- searchText bool true search within option text
- searchValue bool false search within option value
- onSearch function fires before search on options happens
texts object
- placeholder string Select options default text for dropdown
- search string Search search input placeholder text
- searchNoResult string No results search results not found text
- selectedOptions string selected selected suffix text
- selectAll string Select all select all text
- unselectAll string Unselect all unselect all text
- noneSelected string None Selected None selected text
selectAll bool false add select all option
selectGroup bool false add select all optgroup option
minHeight number 200 min height of option overlay
maxHeight number null max height of option overlay
maxWidth number null maximum width of option overlay (or selector)
maxPlaceholderWidth number null maximum width of placeholder button
maxPlaceholderOpts number 10 maximum number of placeholder options to show until "# selected" shown instead
showCheckbox bool true display the option checkbox
checkboxAutoFit bool false auto calc space requirements for checkbox instead of css padding on label
onLoad function fires at end of initial loading, hides native select list
onOptionClick function fires after on option is clicked
onControlOpen function fires when the options list is open
onControlClose function fires when the options list is closed
onSelectAll function fires when (un)select all is clicked
onPlaceholder function fires when the placeholder txt is updated
optionAttributes array array of attribute keys to copy to the checkbox input

Methods

loadOptions( options, overwrite, updateSelect )

Update options of select list. Default state will replace existing list with this one.

  • Set the second parameter to false to append to the list. (default = true)
  • Set the third parameter to false to leave the native select list as is. (default = true)

This will NOT modify the original select list element.

$('select[multiple]').multiselect( 'loadOptions', [{
    name   : 'Option Name 1',
    value  : 'option-value-1',
    checked: false
},{
    name   : 'Option Name 2',
    value  : 'option-value-2',
    checked: false
}]);

settings

Update Multiselect list settings after it has been rendered. It accepts the same options listed above.

This will reload the plugin for the select list it references

$('select[multiple]').multiselect( 'settings', { columns: 2 });

unload

Disable the jquery multiselect list and show the native select list.

This is distructive. You will have to reinitialize with all options to enable the plugin for the list.

$('select[multiple]').multiselect( 'unload' );

reload

This is a quick unload/load while maintaining options during plugin initialization.

$('select[multiple]').multiselect( 'reload' );

reset

Reset the element back to its default selected values.

$('select[multiple]').multiselect( 'reset' );

disable

Disable or enable the select list. If no second parameter is passed then true is assumed.

$('select[multiple]').multiselect( 'disable', true ); $('select[multiple]').multiselect( 'disable', false );

Callbacks

onLoad

Fires after initial loading and hides native select list

onLoad( element )

element: select list element object

onOptionClick

Fires after an option is clicked

onOptionClick( element, option )

element: select list element object

option: option element object

onControlOpen

Fires when the options list is opened

onControlOpen( element )

element: select list element object

onControlClose

Fires when the options list is closed

onControlClose( element )

element: select list element object

onSelectAll

Fires when (un)select all is clicked

onSelectAll( element, selected )

element: select list element object

selected: the total number of options selected

onPlaceholder

Fires when the placeholder txt is updated (only fires if there are selected options)

onPlaceholder( element, placeholder, selectedOpts )

element: select list element object

placeholder: placeholder element object

selectedOpts: selected options

onSearch

Fires before search on options happens

searchOptions.onSearch( element )

element: select list element object

jquery-multiselect's People

Contributors

alanhogan avatar elemke avatar gonardfreeman avatar haidarz avatar metala avatar nobleclem avatar okami-no-shu avatar pvkekem avatar serios avatar zhigalin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery-multiselect's Issues

Search Box

I don't get a search box when I import your files. I tried setting search option to false but I still do not get a search box. Can you help me with this?

Columns option is not working IE

i have used this jquery plugin in my application and tried to use the multiple columns option in my drop down but my drop down is not able to show the values in multiple columns.

Another issue is with the "select all" option if i tried to click on that option it is highlighting all the option values but it is unable to show those values in the text box after deselecting that then it is showing the number.

One thing i am not able to figure it out how to add the select all options in each optgroup.
Help me with the code if you have any

demo to getting selected items in array

Hi @nobleclem very good work
I am very new to jquery, can you please guide me how to fetch all the elements selected by their values or by their names. I am using this with asp:DropDownList and feeding data via asp:SqlDataSource adaptor.

In modal window option selected

Hello

I ask your help

As in the modal window (bootstrap) in the form of set option selected?
And how to do that when you click on a link in a modal window (bootstrap) with a form, set option selected?

Thanks!

Search by group name

Hi,
If it is possible to search the group?
e.g. if i enter "Server-side scripting" in the search bar, it shows all programming languages under that category for me to choose?

Text box not populated

If I give it a data source with elements that are checked, I would expect that the title of these elements would be populated in the input. Am I doing something wrong or is this functionality missing?

hello

how to make the item has been selected by default as Washington in the demo?

Limiting the data loaded onto the selector

Hi, first of all, this is a great plugin -- it's exactly what I've been looking for for my application.

I was just wondering if there's any way to limit the number of data being loaded. If I have a store with a million or two items and I was retrieving from that, I want to be able to limit the load so that it won't be too laggy. In turn, through searching for the item, it would bring up the non-loaded item in which I can now check.

Is there anything I can do to achieve that effect?

Checkbox formatting issues

Does anyone know why I'm getting display issues? For me, the checkboxes don't seem to line up with the text. And the text even seems to overwrite part of the checkbox.

I created an HTML page from scratch. The only styling I added was setting the font-size and font-family, and using the CSS file that comes with jQuery-MultiSelect.

multiselect

Using loadOptions to dynamically set columns

Can you show an example of using 'loadOptions' to dynamically set the columns of existing select lists when the 'onOptionClick' event is fired ? e.g. I have 2 cascading select lists. The user makes a selection in the 1st select list, and based on the number of results returned, i want to dynamically set the number of columns in the 2nd select list. So if the number of results is < 10, i want 1 column. If its > 10 i want 3 columns.

Thanks.

maxHeight

Hi there,

When I set a maxHeight, I found 2 problems:

In the line 210:
maxheight
must be
maxHeight

And in the line 212:
maxHeight = maxHeight < instance.options.minHeight ? instance.options.minHeight : maxHeight;
must be
maxHeight = maxHeight > instance.options.maxHeight ? instance.options.maxHeight : maxHeight;

Is it correct?

On Uncheck Event

This plugin has the event onOptionClick which Fires after an option is clicked and selected
how to make an event which Fires after an option is clicked and unselected

Setting maxHeight does not work in IE 9

When setting the maxHeight option, the script fails with the message
'maxheight' is undefined
jquery-multiselect.js, line 122, character 17

Looks like a variable is being referenced incorrectly. Going to that line and setting swapping maxheight with maxHeight fixes the issue.

Translation issue

I have not seen any option to add lang variable inside script.
Is this any option?
Need help

Conflict with Mootools

Hi Nobleclem,

I'm going to make this brief as I'm under some time constraints. Thank you for developing such a fabulous plugin.

When I run the plugin on a webpage with both jQuery and Mootools installed the selection dropdown options gets messed up like so:

multiselect

Note: I'm using jQuery no conflict. The solution was to change line 255 from:

var groupOptions = [];
to
var groupOptions = {};

IE8 fixes

I know that it's about time to forget about supporting this crappy browser, but nevertheless some people still have to do that. Here are few fixes to 'jquery.multiselect.js' that make the script work in IE8:

line 26: change defalut to 'default'
line 138: change $(window) to $(document)
line 178: change options.searchOptions.default to options.searchOptions['default']

Use proper property access

Please see my patch below. Using attributes for selected and checked is not ideal (see http://api.jquery.com/prop/).

--- jquery.multiselect.js
+++ jquery.multiselect.js
@@ -51,10 +51,10 @@
                         .attr( 'id', 'ms-opt-'+ msCounter )
                 );
 
-                if( $(option).attr( 'selected' ) ) {
+                if( $(option).prop( 'selected' ) ) {
                     container.addClass('default');
                     container.addClass('selected');
-                    container.find( 'input[type="checkbox"]' ).attr( 'checked', 'checked' );
+                    container.find( 'input[type="checkbox"]' ).prop( 'checked', true );
                 }
 
                 var label = $('').attr( 'for', 'ms-opt-'+ msCounter );
@@ -333,7 +333,7 @@
                 var select = optionsWrap.parent().prev();
 
                 // toggle clicked option
-                select.find('option[value="'+ $(this).val() +'"]').attr(
+                select.find('option[value="'+ $(this).val() +'"]').prop(
                     'selected', $(this).is(':checked')
                 ).closest('select').trigger('change');
 
@@ -356,7 +356,7 @@
 
                 // get selected options
                 var selOpts = [];
-                select.find('option[selected="selected"]').each(function(){
+                select.find('option:selected').each(function(){
                     selOpts.push( $(this).text() );
                 });
 

Item Sort Order

I have added items to my element in the correct order. But that is not the order they the plugin displays them. Is there a way to prevent the plugin from changing the order?

Select all taking very long

Select all is taking a very long time to finish when we are trying to select 700 options at once. Any suggestions ?

Issue Reports

Sorry to pester you (and thanks for accepting my other code changes), but I'm seeing a couple of other issues.

Placeholder Text Grows without Limit

If I click the Select All option, all items in the list appear in the placeholder text, and the width of the placeholder container grows way too wide.

Looking at the code, this line doesn't look quite right:

// if copy is larger than button width use "# selected"
if( copyWidth > placeWidth ) {
    placeholder.text( selOpts.length + instance.options.texts.selectedOptions );
}

Prior to this code, you have just created a copy of the placeholder, and then set the text of the original placeholder to hold all selected items. So how could the copyWidth be wider than placeWidth?

Perhaps I'm not fully understanding the logic behind this, but would appreciate if you could look at this issue.

Reverse Handling for Select All

If I start with a multiselect control with nothing checked, and then click the Select All option, all items in the list are correctly checked. But the selected items do not appear in the placeholder text. If I click the Select All option a second time, now all items in the list are correctly unchecked. But the selected items now do show up in the placeholder text.

The placeholder text and the checkmarks are now out of sync.

Thank you for any attention you can give these issues.

jQuery Multiselect working with AngularJS ng-Options

I have been working with your jQuery.Multiselect control (v2.1.2) for almost a week, trying to get it to work with a that has the options dynamically generated by AngularJS. The data is retrieved from a web service call. In the promise, the data is stored in an array, a filtered list is created and stored into a separate array that the element is bound to, and then initialization of your control is called. Here is the declaration of the element... <select multiple="multiple" name="commodity" id="Commodity" ng-model="selectedCommodity" ng-options="commodity for comm in availableCommodities"> When the page renders, all of the static options are created successfully, but the dynamic one is not. If I use the tools, I can see that the options list is being created and is in the hidden control. Each of the options contains an ID, a value, and text.

I have tried moving the control's initialization all over the page and even into the JavaScript file for the page. I have also attempted to hold the control's initialization until after the AngularJS data has been retrieved and bound to the scope variable to make sure that the element had the elements within it. I have also tried the jquery.actual plugin but I'm not sure what options to set in your control for the other plugin. Any help that you could give me would be greatly appreciated! EDIT: I just updated to 2.3.0 and tried it again. I get a single check box with no text and no "value" when I click it. Thanks, Jim Evans

Recommended code change

When multiple items are selected and displayed in a comma-delimited list in the placeholder text, there is an extra space before the comma.

Therefore, I recommend the following code change in _updatePlaceholderText:

Change the line:

placeholder.text(selOpts.join(', '));

To:

placeholder.text(selOpts.map(function (e) { return e.trim(); }).join(', '));

disabled option in dropdown

Hello,

I'm using the multiselect code in my project. All is working fine, except one thing.

In my dropdown list I have items that are disabled. For example:
<option value="7123" **disabled="disabled"**>Option TEXT</option>

Is there a possibility to disable this with the plugin also?

Kind regards,

Wim

Help setup the plugin as it is on the demo site

this plugin meets my demand while searching for jQuery plugin for autocomplete, multiselect plugin. This is my first time experience with it and i'm a bit unable to set it up exactly like it is on the demo. I'll need your assistance in making it exactly the you have on the your demo site - http://springstubbe.us/projects/demos/jquery-multiselect/

This is what I have so far

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script src="C:\Users\ken4ward\Desktop\examples\multi-selection\jquery.multiselect.js"></script>
    <link href="C:\Users\ken4ward\Desktop\examples\multi-selection\jquery.multiselect.css" rel="stylesheet" />
</head>
<body>
<select name="basicOptgroup[]" multiple="multiple" class="select2">
    <optgroup label="Programming Languages">
        <option value="C++ / C#">C++ / C#</option>
        <option value="Java">Java</option>
        <option value="Objective-C">Objective-C</option>
    </optgroup>
    <optgroup label="Client-side scripting Languages">
        <option value="JavaScript">JavaScript</option>
    </optgroup>
    <optgroup label="Server-side scripting Languages">
        <option value="Perl">Perl</option>
        <option value="PHP">PHP</option>
        <option value="Ruby on Rails">Ruby on Rails</option>
    </optgroup>
    <optgroup label="Mobile Platforms">
        <option value="Android">Android</option>
        <option value="iOS (iPhone, iPad and iPod Touch)">iOS (iPhone, iPad and iPod Touch)</option>
    </optgroup>
    <optgroup label="Document Markup Languages">
        <option value="HTML">HTML</option>
        <option value="SGML">SGML</option>
    </optgroup>
</select>
<script>
    $('select[multiple]').multiselect();

$('select[multiple]').multiselect({
    columns: 4,
    placeholder: 'Select options'
});
searchOptions.onSearch( basicOptgroup )
$('select[multiple]').multiselect('reload');

$('select[multiple]').multiselect( 'loadOption', [{
    name   : 'Option Name 1',
    value  : 'option-value-1',
    checked: false,
    selectAll:true,
    search:true
},{
    name   : 'Option Name 2',
    value  : 'option-value-2',
    checked: false,
    selectGroup:true
}]);
</script>
    </body>
    </html>

Question about element data attribute

Hi,

I'd like to get my list options custom data attribute in the generated inputs, I took a closer look in the code and try some changes in _addOption() but I failed.

Do you have any idea how I could do that ?

My code sample :

<select id="multi-select-2" name="export[]" multiple="multiple" style="display:none;">
<option value="1" data-sub="1">test01</option>
<option value="2" data-sub="1">test02</option>
<option value="3" data-sub="1">test03</option>
</select>

Thanks for any help

maxHeight is not correctly calculated when the page has vertical scroll bar

If the page has a vertical scroll bar, maxHeight is always set to minHeight as offset().top will be eventually greater than $(window).height().

I think it is just a matter of adding $(window).scrollTop() every time maxHeight is calculated.

For example:
var maxHeight = ($(window).height() - optionsWrap.offset().top + $(window).scrollTop() - 20);

Does this support single selection?

This is a nice plugin however, It would be great if it has the option to decide between single or multi-selection, keeping all other functions as is.

I need to use single selection and multiselction dropdowns on a single form. but if I use this multiselect, and some other single select, then the look and feel is different which is not a big deal to adjust but I wanted other functions like seach on my single select too.

html5 required

Hi,
nice plugin, thanks.

when using html5 [required] attribute, the browser provided message comes out at top of window, on Mozilla. Opera and Chrome show no mesage still not allowing to submit the form. I believe this happens since the original select is hidden, I replaced

             // hide native select list
             $(this).hide();

with

            $(this).css({'position':'absolute', 'visibility':'hidden', 'overflow' : 'hidden', 'height' : '2em'});

but unfortunately this works only in mozilla, I think because of visibility:hidden, so I came out with this, that also tries to place the message on bottom of input, like in the image. Probably the margin-top should be calculated somehow.

          $(this).css({'position':'absolute', 'margin-top':'22px', 'margin-left':'6px', 'border':'0px', 'overflow' : 'hidden', 'height' : '1px'});

testmultiselect

Let me know what you think,
Bye

maxHeight Error

There are two errors:
In line 143 is: maxHeight = ($(window).height() - optionsWrap.offset().top + $(window).scrollTop() - 20); the correct is: instance.options.maxHeight; ... the same occurs on the line 209

Otherwise it will never be assigned the value instantiated

Sorry I do not speak english !

"texts" options with no effect

Hi,

I'm seeing that texts options such as "placeholder" and "selectAll" have no effect, i'm still reading the default values "Select options" and "select all".

I tried to find out what the issue is by changing some other options but this seems to happen only why the texts.

Here's my plugin call :

$("#multi-select-1").multiselect({
                    columns: 1,
                    texts: {
                        placeholder: 'test1',
                        selectAll: 'test2'
                    },
                    minHeight : 150,
                    search: true,
                    selectAll: true
                });

Also, I should like to point out that the "default" option for the search input placeholder text is a reserved word in Jquery plugin and this isn't working either, in my case I mean.

EDIT: Forget about the "reserved word" mention, I didn't notice the quotes surrounding "default", but it still doesn't effect by the way.

Placement of .ms-options

Hi there,

Firstly: thanks for providing jQuery Multiselect!

I have one question about it: is it possible to place the .ms-options window above the button, instead of below it? When the element is near the bottom of a page it will stretch out the page. In this case it would maybe be better if the .ms-options would be above the button.

Thanks in advance!

Is there an event that fires when the dropdown is closed?

The subject should say it all. I am looking for an event like onOptionClick that is called once the user closes the dropdown (ie: clicks out of the control) so that I can send the whole lot back to the server in one hit.
cheers,
Lucas.

Clicking on "select all" checks all entries instead of just the filtered ones.

Enable search on your multiselect list. Filter it by some value and then click on "select all". All checkboxes are marked instead of just the filtered ones.

I suppose the following change will fix it:

            // handle select all option
            optionsWrap.on('click', '.ms-selectall', function( event ){
                event.preventDefault();

                if( $(this).hasClass('global') ) {
                    // check if any selected if so then select them
                    if( optionsList.find('li:not(.optgroup)').filter(':not(.selected)').filter(':visible').length ) {
                        optionsList.find('li:not(.optgroup)').filter(':not(.selected)').find('input[type="checkbox"]:visible').trigger('click');
                    }
                    // deselect everything
                    else {
                        optionsList.find('li:not(.optgroup).selected input[type="checkbox"]:visible').trigger('click');
                    }
                }
                else if( $(this).closest('li').hasClass('optgroup') ) {
                    var optgroup = $(this).closest('li.optgroup');

                    // check if any selected if so then select them
                    if( optgroup.find('li:not(.selected)').filter(':visible').length ) {
                        optgroup.find('li:not(.selected) input[type="checkbox"]:visible').trigger('click');
                    }
                    // deselect everything
                    else {
                        optgroup.find('li.selected input[type="checkbox"]:visible').trigger('click');
                    }
                }
            });

Insert data from mysql

Hello.
I need help, to show in multiselect input data from mysql table, I can't understand how.
where table structure include only three columns: num,name and group. Finaly i want to get in multiselect something like: num - name and would be awesome if it be included into groups from group column

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.