jquery-select2

How to change the css of color of select2 tags?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 11:59:40
问题 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

Select2 Dropdown autoselect if only 1 option available

£可爱£侵袭症+ 提交于 2019-11-29 11:55:01
I have a select2 dropdown box using remote datasource. What I would like to do is if/when there is only one option returned by the search, auto select it. ie, the user doesn;t have to click on the option to make the selection. $("#searchInfo_Entity_Key").select2({ ajax: { url: "/Adjustment/GetEntity", dataType: 'json', delay: 250, data: function (params) { return { term: params.term, // search term }; }, processResults: function (data) { return { results: data }; }, results: function (data) { return { results: data }; }, }, initSelection: function (element, callback) { var data = []; callback

Add class to select2 element

好久不见. 提交于 2019-11-29 11:30:16
问题 The documentation is either terrible or I'm missing something. I'm trying to add an error class to a select2 box for form validation. It's just a 1px red border. I found the containerCssClass method in the documentation, but I'm not sure how to apply it. I have tried the following with no luck: $("#myBox").select2().containerCssClass('error'); 回答1: jQuery uses JSON objects to initialize, so I guess this one will as well:\ $("#myBox").select2({ containerCssClass : "error" }); If you want to

JSON format for jquery-select2 multi with ajax

那年仲夏 提交于 2019-11-29 11:22:30
问题 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

Select2 v4.0 make optgroups selectable

独自空忆成欢 提交于 2019-11-29 09:36:55
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": "Test 3", "id": "3", "selected": true }] }] }); }); <script src="https://ajax.googleapis.com/ajax/libs

Using Select2's createSearchChoice function

坚强是说给别人听的谎言 提交于 2019-11-29 05:39:30
问题 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

Select 2 version 4.0 allow user to enter free text

守給你的承諾、 提交于 2019-11-29 03:46:36
I am using the latest version of Select2: Select2 4.0 . I would like to allow users to enter free text. In other words if a user cannot find a option in the drop down (data returned by ajax), I want them to be able to 'select' whatever text they have typed in. This is my markup: <select class="required form-control" id="businessName" data-placeholder="Choose An Name" > </select> And this is the JavaScript that I am using to initialize Select2: $("#businessName").select2({ ajax: { url: "/register/namelookup", dataType: 'json', delay: 250, type: 'post', data: function (params) { return {

Adding “data-” attributes with select2

眉间皱痕 提交于 2019-11-29 03:45:22
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: function(data, page) { console.log(data); return { results: data }; }, cache: true } }); This solution

Select2 - use JSON as local data

纵饮孤独 提交于 2019-11-29 03:19:09
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 Load data from a local array The webpage of jquery-select2 with the examples contains a demo to use Select2 with local data (an array) . The html <input type="hidden" id="e10" style="width:300px"/> The javascript $(document).ready

Fill Select2 dropdown box from database in MVC 4

て烟熏妆下的殇ゞ 提交于 2019-11-29 01:38:18
问题 I need help writing the jquery/ajax to fill a Select2 dropdown box. For those who don't know what Select2 is, it is a javascript extension to provide Twitter Bootstrap looks and search / type-ahead functionality to an html select list dropdown box. For more information look at the examples here: Select2 Github page UPDATED - Solved! So I finally put this all together, and the solution to my problems was that I was missing functions to format the results and the list selection. The code below