Giter Club home page Giter Club logo

multi.js's Introduction

multi.js

multi.js is a user-friendly replacement for select boxes with the multiple attribute. It has no dependencies, is mobile-friendly, and provides search functionality. multi.js is also easy to style with CSS and optionally supports jQuery.

Check out the demo.

Preview of multi.js

Installation

Install with npm:

npm install --save multi.js

Install with yarn:

yarn add multi.js

After installing with npm or yarn, import both the library and stylesheet:

import multi from "multi.js/dist/multi-es6.min.js";
import "multi.js/dist/multi.min.css";

Install manually by cloning or downloading the repository to your project and including both files in the dist directory.

<link rel="stylesheet" type="text/css" href="multijs/dist/multi.min.css">
<script src="multijs/dist/multi.min.js"></script>

Usage

multi.js can be applied to any select element with the multiple attribute enabled.

var select_element = document.getElementById("your_select_element");
multi(select_element);

To customize multi a few options can be passed with the function call. Below are all the default values.

multi(select_element, {
    "enable_search": true,
    "search_placeholder": "Search...",
    "non_selected_header": null,
    "selected_header": null,
    "limit": -1,
    "limit_reached": function () {},
    "hide_empty_groups": false,
});

Column headers

To add headers to both columns set values for these options:

multi(select_element, {
    'non_selected_header': 'All options',
    'selected_header': 'Selected options'
});

Limit

You can add a limit of option selected for your select using the limit parameter. Default is -1, which means "no limit".

multi(select_element, {
    'limit': 10
});

Additionally, there a callback is available: limit_reached, invoked when the user selects the last available option before reaching the limit (for example, the 10th element of a maximum of 10).

multi(select_element, {
    'limit': 10,
    'limit_reached': function () {
      alert('You have selected 10/10 elements.');
    }
});

jQuery

multi.js is fully native Javascript but also has jQuery support. If you have jQuery included multi can be applied to a select element as follows:

$('#your_select_element').multi();

Contributing

All contributions, big and small, are very welcome.

Use Prettier to format all code after editing. Build the project with Grunt and bump the version number before creating a pull request.

License

multi.js is licensed under MIT.

multi.js's People

Contributors

bmcorum avatar ciberpiranha avatar danilopolani avatar dbleier avatar dependabot[bot] avatar fabianlindfors avatar harrykiselev avatar loarca avatar miguelsanchezgonzalez avatar nijel avatar okon3 avatar zrooda 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  avatar  avatar  avatar

multi.js's Issues

No update on new data in the list; Not handling properly list changes when we click on the elements.

I have a similar problem to #20 with VueJS.
The data is loaded dynamically with Vue but as no element was available at first, I added a dummy.

    <select multiple id="light-select">
            <option v-bind:value="value.name" v-for="value in lights">{{value.name}}</option>
            <option >ohoh</option>
    </select>

Yet, once I click on the dummy, it refreshes the data but it selects the first element in the real list and not the dummy.

Accessibility / WCAG 2.0 Conformance

Looks cool but alas I see no mention of conformance with WCAG 2.0 - it needs to be conformant to at least AA else it can't be used by otherwise conformant websites.

There is no git tags on GitHub repository

Hi @fabianlindfors ,
We are cdnjs team, we're going to host this library.
The git auto-updater in cdnjs relies on git tags so that it can recognize the correct version and automatically update the repo.
Could you please add git tags for the repo?

And could you please help me add more information into package.json to upstream?
eg. description, keywords, license, author, repository ... etc.
ref: https://docs.npmjs.com/files/package.json

If you have any suggestions, please let me know.
Thank you.

cdnjs/cdnjs#10550

call back function when clicking disabled options?

in order to report to the UX why a selection option is currently disabled a call back function would be be useful much alike attaching a function to limit_reached, Only this one should be sending back the option.value and option.text of the clicked disabled item for it to be further handled externally.

disabled option support

Again, great plugin. Thank you. it would be nice if you could add support for selected but "disabled" options. For example, a list contains names of people in a sales team and manager's name is "disabled" so it cannot be moved e.g.

<option value="manager" selected disabled>Manager</option>

I have made some changes in your minimized code to support this feature , so perhaps you can make use of it if you want to make it an "official" feature.

b.find("option").each(function(){
  var b=a(this),c=b.prop("value"),d=b.text(),f=b.is(":selected"),g=b.is(":disabled"); 
  e.push({value:c,label:d,selected:f,disabled:g, element:b})									
}),

and

if(c.selected){
  e.addClass("selected");
  if(c.disabled){ e.removeClass("selected").addClass("disabled"); } 
  var f=e.clone();
  f.click(function(){
    if(!c.disabled) { c.element.prop("selected",!1), b.change()
  }
}),

I guess, a similar thing should be done for a keyboard events handler.
Also in css i defined disabled class as:

.multi-wrapper .item.disabled {
    color:#ccc;
}

This is just a quick fix - it can be done much better :-)

How to refresh the multiple-select with Jquery (or javascript)

Hi,
I'm trying to empty and then fill again the multi-select via Ajax. I see the Ajax works for a common multi-select filed, but if it is already using multi.js, it stays the same. How can I refresh the content? I see the multi.js already has a refresh_select function, but I don't know how to call it. Tahnks in advance!

Optgroup Support

Love the simplicity of this plugin, it would be great to have optgroup support though

Method to programmatically select option

Hi, This is a simple and nice plugins. I really like the UI. Good Job.

Is it possible to select option programatically?
I've tried to set value on select element using val() then trigger change() event but item is not moved to selected wrapper.

Thanks!

Required Validation

I noticed the plugin doesn't bring over "required" attributes or a functionality that validates if the field is required then no submission can be made. Is there a fix to this or is there an ability to do so already that I'm just not aware?

Dynamic change data source.

Hello,
Awsome work!
I have one question is it possible to dynamicly change the data source with AngularJS?
I'm getting data from http response and in every change I need to click on the list to see new data.
Thanks for the response :D

Method to disable and enable the select box?

Is there a way to programmatically disable the box? I set disabled dynamically on the select element (depending on the value of other input elements) but Multi.js doesn't seem to pick it up. The simplest solution would be a method in the API to turn it off and on.

Thanks!

Submit to NPM

I'd love to be able to use this library through NPM / Yarn. Making updating to the newest version easier.

NOTE: There already are libraries using the name multi.js, not sure if that is a potential issue.

Thanks for the amazing work :)

Initialization does not check for limit reached

Initializing multi does not check to see if the number of preselected options matched the passed in limit.

Example:

I am editing an existing record and thus my form loads with the following select HTML

<select id="multi" multiple="multiple">
  <option selected="selected">one</option>
  <option>two</option>
  <option selected="selected">three</option>
  <option>four</option>
</select>

When calling

multi(document.getElementById('multi'), { limit: 2 });

It initializes and I am able to select more than two options. If I select a third and then remove one of my options the limit will be triggered. Looks like the initialize function do not check for limit reached like the toggle_option function does.

Better README

I think that readme is a bit incomplete. My proposal for improve it is this:

In installation or usage via NPM/Yarn I suggest to specify different ways to use it via modules:

For example import via js:

import multi from "multi.js/dist/multi-es6.min.js";
import 'multi.js/src/multi.css';
var select = document.getElementById("fruit_select");
multi(select);

Or another option is import js via module and styles via SASS:
@import 'multi.js/src/multi.css';

If you want I can make a PR including these two steps in README

Thanx!

preserve selected order

Is there an option to preserve the order in wich the items are selected?
right now it keeps the selected column ordered like the available column

Missing tag for 0.2.4

The tag for the 0.2.4 release seems to be missing on GitHub and thus is not available in yarn. Can you please add/push it?

How to reuse ?

I'm using multi in a popup , after i close the popup and pop again, there's old value in multi. how to clean it?

Selected items order

Is there any way that I can remove the order of the selected items? I want to arrange them by the order I selected them

Limit dosnt work when manually selecting items in code

when automaticly selecting items in code using the "Selected" value, the selected items is updating.. however, if there is a limit, and the selected number hits the limit, the rest isnt disabled as they are when im selecting it manually with the mouse.
If i deselect and item and select it again, the limit function triggers.

Is it possible to fix that or maybe have a go around? :-)

Triggering events

loads a long list (> 2000 items) very fast, way faster than multi-select script. However how do you trigger events on select or deselect? Or can you select an item by clicking a link? Such functionality would be extremely useful.

Option not selected

Hi there,

I have this code:

 $('#retention').remove(); // A previous instance of this dual list box.
for(let i  = 0; i < allCategories.length; i++) {

                        for(let j  = 0; j < selected.length; j++) {
                            let toggleSelected = (selected[j].id===allCategories[i].id)
                                ? " selected='selected'"
                                : ""
                            ;
                            options += '<option value="' + allCategories[i].id + '"' + toggleSelected + '>'
                                + allCategories[i].name
                                + '</option>';
                        }
                    }
                    console.log(options);

As you can see, the final statement is a console.log where is displayed the following result:
<option value="6">TEst</option><option value="5">Categoría 2</option><option value="4" selected='selected'>Categoría 1</option>

Again, as you can see, the last option has the selected attribute.

After doing this, the following lines were written:

document.getElementById("retentionDiv").style.display = "block";

                    // I get a clean instance of this object
                    $('#retention').multi({ non_selected_header: 'Categorías de retención disponibles',
                        selected_header: 'Categorías de retención que aplican'
                    });

                    $("#retention").append(options);

The issue here is none of these items are selected:
image

What should happen?
The last item should be selected and moved into the right select

What happens?
None of them are selected.
image

Disable or readonly

How can i let the multi select disabled or in readonly mode?
Tried some parameters but none worked.
Thanks

Preselected items

Hi, thank you for the good work! I was wondering if there is a possibility to set preselected items?

Clear Filter/Searchbox Input

Hello !

Great plugin ! Looks AMAZING and really helped my users !

I want to know if there is a way to programmatically clear Filter/Searchbox input and show all the options available.

I tried everything, nothing works to show the options list back again...

     var select = $("select#" + select_id);

            select.parent().find(".search-input").focus();
            select.parent().find(".search-input").val('');  //Clear the input value...
            select.parent().find(".search-input").keyup();
            select.parent().find(".search-input").change();

            select.trigger("change");
            select.trigger("click");
            select.parent().find(".search-input").trigger("click");
            select.parent().find(".search-input").trigger("change");
            select.parent().find(".search-input").trigger("keyup");
            select.parent().find(".search-input").trigger("keydown");

Select all / Select none

Would be nice to have a select all / select none button. Possibly done as "Select All" on the left and "Clear" on the right.

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.