jquery-select2

How to dynamically filter a select2 listing based on option class

孤街浪徒 提交于 2019-11-30 09:36:47
问题 I have a dropdown select of optional 'opportunities' ( id="opportunities" in the example below) which I enhance using the select2 jquery plugin, and wish to dynamically filter this list in order to reduce the possible options presented to a user using a 2nd select dropdown ( id="group-select" in the example below), <label> Select an opportunity<br> <select id="opportunities" style="width:300px;" multiple="multiple" name="selected-opps" value=""> <optgroup label="Class A"> <option class="group

Select2 drop-down for countries, with flags

风流意气都作罢 提交于 2019-11-30 09:25:32
Does somebody have an easy to use example of a country drop-down, with country flags, for Select2 ? I am about to implement one based on this suggestion, but I would prefer to avoid reinventing the wheel. Starx I was working on a similar problem and here is how I solve it. (function($) { $(function() { var isoCountries = [ { id: 'AF', text: 'Afghanistan'}, ... ]; //Assuming you have a select element with name country // e.g. <select name="name"></select> $("[name='country']").select2({ placeholder: "Select a country", data: isoCountries }); }); })(jQuery); I also have made a gist about it and

Select2 v4.0 make optgroups selectable

风流意气都作罢 提交于 2019-11-30 08:57:47
问题 I'm using the latest version of select2 (4.0.0) and I can't find the option to make optgroups selectable. An optgroup is used to group different options of the dropdown, as shown in their basic examples: I need this optgoup to be selectable too! It was possible in 3.5.1 but it isn't the default setting in 4.0.0 anymore. My Code Looks like this: $(document).ready(function() { $('#countrySelect').select2({ data: [{ text: "group", "id": 1, children: [{ "text": "Test 2", "id": "2" }, { "text":

How to change the css of color of select2 tags?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 08:44:16
I just started using project for showing multiple tags from a select box and it works great, thanks for the library. I just need to modify the color or css of the tags shown in multi-value select-boxes. Right now the color of the tag is shown as grey and I would like to change that to some other color according to the type of the tag. Or at least is there a way to change the default color? Also is it possible to change the css class of tags? There is an option such as formatResultCssClass but when I tried to add css classes through that property nothing changed, I would appreciate if someone

Select2 - Sorting results by query

孤者浪人 提交于 2019-11-30 08:36:14
I'm using Select2 version 4.0.0. If my results contain multiple words, and the user enters one of those words, I want to display the results sorted by where the entered word is within the result. For example, a user enters "apple", and my results are: "banana orange apple" "banana apple orange" "apple banana orange" Then "apple banana orange" should appear first in the list of select2 results, because that is the result in which "apple" appears earliest within the result. I don't care so much about the ordering past that. What do I override or configure to get something like this? It seems

JSON format for jquery-select2 multi with ajax

我与影子孤独终老i 提交于 2019-11-30 08:18:27
I'm thinking in moving from Chosen to Select2 because Select2 has native methods for ajax. Ajax is critical because usualy you have to load a lot of data. I executed sucessfully the example with the JSON of api.rottentomatoes.com/api/ I did a JSON file to test the ajax, but it didn't works. I don't know how the JSON should be. It seems that there is no detailed documentation: https://github.com/ivaynberg/select2/issues/920 I tried unsucessfully several JSON formats, so I tried to copy a JSON format similar to api.rottentomatoes but it doesn't works. I may be missing something stupid. function

Adding “data-” attributes with select2

依然范特西╮ 提交于 2019-11-30 07:08:14
问题 I've seen a lot examples of Select2 option tags set with "data-" attributes and I would like to do it. I'm using ajax to get the data. I get the ID and the TEXT needed to build the select. But how can I add more attributes to it? I just didn't find the way to add them. $(element).select2({ placeholder: 'Select one...', width: '100%', minimumInputLength: 2, ajax: { url: '/my/url', dataType: 'json', data: function(params) { return { q: params.term, page: params.page }; }, processResults:

Select2 - use JSON as local data

若如初见. 提交于 2019-11-30 06:55:50
问题 I can get this to work... var options = [{id: 1, text: 'Adair, Charles'}] $('#names').select2({ data: options, }) But i cant work out how to get from here... alert(JSON.stringify(request.names)) gives me... [{"id":"1","name":"Adair,James"}, {"id":"2","name":"Anderson,Peter"}, {"id":"3","name":"Armstrong,Ryan"}] To something that Select2 will accept as local data 回答1: Load data from a local array The webpage of jquery-select2 with the examples contains a demo to use Select2 with local data (an

Using Select2's createSearchChoice function

微笑、不失礼 提交于 2019-11-30 06:38:53
I'm trying to use the createSearchChoice function to allow users to enter their own choice when the default list won't suffice. When I try to use this function on a <select> element, I get the following error: Error: Error: Option 'createSearchChoice' is not allowed for Select2 when attached to a <select> element. I tried using an <input type='hidden'> element instead, but now get the following error: Error: uncaught exception: query function not defined for Select2 'MyInputName' I'd prefer to use the select element to stay in line with existing code (need the ability to select multiple

SELECT2 -> Add data without replacing content

廉价感情. 提交于 2019-11-30 05:48:49
问题 I've had a look at some other threads but nothing quite as specific. It's not really something that I would assume is hard but I'm not sure how about to do it. Currently I'm using Select2 for a tagging system and next to it I have suggested tags which users would be able to click on and it will add to the box. Instead, each tag is replacing the content and adding itself. I need the adding to be appended into the box without replacing what's already in there. Here's my code: $(document).on(