Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 start with input field instead of dropdown

I use the js library select2. This is a screenshot of what I have now:
Start:
enter image description here
Click on dropdown:
enter image description here

Now is it possible to have an input field to start with and not directly a dropdownlist? I know it's possible because you can find it on the select2 site. An example is this: enter image description hereenter image description here

But the documentation is very brief. This is what I have now:

<input type="text" name="questions[question1]" id="question1" class="question1" style="width:500px"/> 
function createQuestionTags(data, question_number){   $(".question" + question_number).select2({     createSearchChoice: function (term, data) {       if ($(data).filter(function () {         return this.text.localeCompare(term) === 0;       }).length === 0) {         return {           id: term,           text: term         };       }     },     data: data,     placeholder: "Enter Question",     allowClear:true   }); } 

(The data is received from an ajax call)

like image 359
nielsv Avatar asked Nov 26 '13 16:11

nielsv


People also ask

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.

How do I set Select2 values?

To dynamically set the "selected" value of a Select2 component: $('#inputID'). select2('data', {id: 100, a_key: 'Lorem Ipsum'}); Where the second parameter is an object with expected values.

How do I add options to Select2?

New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data.text, data.id, false, false); $('#mySelect2').append(newOption).trigger('change');

What is dropdownParent?

The dropdownParent option allows you to pick an alternative element for the dropdown to be appended to: $('#mySelect2').select2({ dropdownParent: $('#myModal') }); This is useful when attempting to render Select2 correctly inside of modals and other small containers.


2 Answers

What you are seeing is actually a multi-select or multi-value drop down box in that example. It is not a single value drop down box like you are using in your code. Per the Select2 website, select2 will detect that you are trying to use a multi-select box and will automatically apply that styling instead of the default (drop down arrow, etc.).

If you in fact need a single value drop down box, there is no direct way to make it display with the formatting of the multi-select so that it looks like a regular input box. There may be a way to fake it by adding or removing CSS classes. I played around a bit but couldn't find one.

Since you don't want the formatting, the search box, or the multi-select capability (I'm assuming) you probably don't need to use the select2 library.

Update: It looks like you're not the first person to try to do this. They plan to add this feature but it might be a while: https://github.com/ivaynberg/select2/issues/1345

like image 183
KyleK Avatar answered Oct 12 '22 12:10

KyleK


The only workaround that I could come up with is to use both multiple: true and maximumSelectionSize: 1 when setting up.

Please see my example here: http://jsfiddle.net/DanEtchy/Lnf8j/

like image 40
Dan Echeverria Avatar answered Oct 12 '22 11:10

Dan Echeverria