Multiple Selection with jQuery Select2 Plug-in using JSON Array Data

I have previously shared an article here on my blog on how to use jQuery Select2 Plug-in for multiple selections using HTML <select> element with Asp.Net C# to extract and bind data to the element. Here in this article, I’ll show you how to use jQuery Select2 Plug-in to do multiple selection with JSON array data.

jQuery Select2 changes the way we do multiple selections using a dropdown list. However, in this post, I’ll use an <input> box of type text and show you how the Select2 will bind JSON data to the <input> box and convert it into a multiple selection dropdown list.

Multiple Section using jQuery Select2 with JSON Data

Before you start working on you application, you must first Download Select2 and save in a folder in your computer. It’s a ZIP file, which will have a .js and .css file. You will need to include these files in your webpage, inside the <head> tag.

The Markup

In the markup section, I have added the Select2 library and CSS files inside the <head> tag. I have also added an <input> box of type text inside the <body> tag. The textbox has an id called Books. The Select2 will bind the input box using its id.

<!DOCTYPE html>
<html>
<head>
    <title>jQuery Select2 Example with JSON Array</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

    <script src="select2-3.4.1/select2.js"></script>
    <link href="select2-3.4.1/select2.css" rel="stylesheet"/>
</head>
<body>
    <input type="text" id='Books' value="" style="width:300px;" />
</body>
The Script
<script>
    $(document).ready(function () {
        var empArray =
            [{ id: '001', text: 'Alpha' },
                { id: '002', text: 'Bravo' },
                { id: '003', text: 'Charlie' },
                { id: '004', text: 'Delta' },
                { id: '005', text: 'Echo'}];

        $("#Books").select2({
            data: empArray,
            multiple: true,
            placeholder: 'Select one or more values from the list'
        });
    });
</script>
</html>


Try it yourself

In the script, first I have declared a variable and assigned it with JSON data. Two values each, with Employee ID and Text for Employee names. Next, I have bind the <input> box with Select2 and assigned the JSON array data to it.

I have also explicitly set the multiple property as true. Else, it would allow us to select a single value. We need to select multiple values. The placeholder property specifies a short hint the describes what you should enter in the input box.

Using Select2 with minimumInputLength Property

When you set focus on the input box, it will automatically show you a dropdown list with the values, with infinite scrolls (if you have a huge list of data). This is its default behavior. However, you can set a Minimum Input length, before it shows any value in the dropdown, that is, you can force your uses to enter few letters before Select2 shows matching data in the dropdown list.

This property helps in filtering the data to avoid displaying every value extracted from the source, JSON or some database.

All you have to do is, add the minimumInputLength property with a value. For example,

$("#Books").select2({
    data: empArray,
    minimumInputLength: 2,
    multiple: true,
    placeholder: 'Select one or more values from the list'
});

Try it yourself

I have set the minimumInputLength to 2. You now have to enter 2 letters in the input box and then it will show a list of values.

Well, that’s it. Thanks for reading.

Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.

Enter your email id

Delivered by FeedBurner
Tweet this article Facebook Google+

comments powered by Disqus

Join our Google Plus Community and be a part of a discussion!