Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mix of Select2 Loading Remote Data with Placeholders dropdown

I'm new to the Javascript, jQuery, Ajax and jSON world.

What I need to do is to mix 2 options available with SELECT2

Placeholders

$("#e2_2").select2({
    placeholder: "Select a State"
});

and

Loading Remote Data

$("#e6").select2({
     placeholder: "Search for a movie",
     minimumInputLength: 1,
     ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
         url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
         dataType: 'jsonp',
         data: function (term, page) {
             return {
                 q: term, // search term
                 page_limit: 10,
                 apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
             };
         },
         results: function (data, page) { // parse the results into the format expected by Select2.
             // since we are using custom formatting functions we do not need to alter remote JSON data
             return {
                 results: data.movies
             };
         }
     },
     formatResult: movieFormatResult, // omitted for brevity, see the source of this page
     formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
     dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
 });

As you can see from the Select website, these options are quite different. When I click on the Loading Remote Data filed, it opens a search option. But I don't want that. I want the drop down with the options available line the PlaceHolder example.

In the placeholder example, the options available in the dropdown are hardcoded in the HTML. What I need is that when you open, it goes to the jSON file and returns the info available on the json.

The ideal is to use the UI of the Placeholder Select2 with the functionality (fetch json on the server) of the Loading Remote Date form the other Select2 example.

Any idea? I'm open to any super quick Ajax solution if the 2 cannot be combined.

like image 673
Joe Avatar asked Sep 12 '12 08:09

Joe


People also ask

How do you add a placeholder in Select 2?

Select2 supports displaying a placeholder by default using the placeholder option. This can be either a data object matching the placeholder option, or a string to display as the placeholder if you are using a blank placeholder option.

How do I create a Select2 dynamically?

HTML. Create a <select class="select2_el" > element to initialize select2 on page load and create <div id="elements" > container to store <select > element on button click using jQuery AJAX.

What is initSelection in Select2?

In the past, Select2 required an option called initSelection that was defined whenever a custom data source was being used, allowing for the initial selection for the component to be determined. This has been replaced by the current method on the data adapter. AMD Modules: select2/compat/initSelection"

What Select2 dropdown?

By default, Select2 will attach the dropdown to the end of the body and will absolutely position it to appear above or below the selection container. Select2 will display the dropdown above the container if there is not enough space below the container, but there is enough space above it.


1 Answers

If you only need to load data to your select2 via ajax (no need search via remote api) you can do it this way:

$.get( "/path/to/your/data.json", function(data){window.ajaxData=data;});
$("#e2_2").select2({data: window.ajaxData, placeholder: "Select a State"});

(using global variables is often bad practice, but it's just an example)

like image 71
Evgenii Malikov Avatar answered Oct 20 '22 19:10

Evgenii Malikov