question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dropdown not closing when using custom adapter

See original GitHub issue

Hey

I created my box with

$('select').select2()

. To style the dropdown, I specified containerCssClass in the options,

*$('select').select2({
    containerCssClass: 'select2-side-services-phone-time'
});

but the dropdown does not receive the class for some reason, also the console shows no error. Next try was to change the dropdown attachment to the container instead of the body which was really a hassle

$.fn.select2.amd.require([
    "select2/utils",
    "select2/dropdown",
    "select2/dropdown/attachContainer"
], function (Utils, DropdownAdapter, AttachContainer) {
    $('select').select2({
        dropdownAdapter: Utils.Decorate(DropdownAdapter, AttachContainer)
    });
});

. The dropdown now is created in place and I can style it accordingly but it does not close anymore. Not with the escape key, not when losing focus, not when chosing an item. Adding the option closeOnSelect: true did not help as well.

Any idea what I am doing wrong?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
digitalsoundscommented, Feb 2, 2016

I’ve managed to make this work using the following code:

$.fn.select2.amd.require([
    "select2/utils",
    "select2/dropdown",
    "select2/dropdown/attachContainer",
    "select2/dropdown/search",
    "select2/dropdown/closeOnSelect"
], function (Utils, DropdownAdapter, AttachContainer, DropdownSearch, CloseOnSelect) {
    MyCustomAdapter = Utils.Decorate(
        Utils.Decorate(
            Utils.Decorate(
                DropdownAdapter,
                DropdownSearch
            ),
            AttachContainer
        ),
        CloseOnSelect
    );

    $('select').select2({
        dropdownAdapter: MyCustomAdapter
    });
});
1reaction
videsignzcommented, Mar 8, 2016

@digitalsounds Dude, you are a life saver. I was just struggling with the closeOnSelect being attached to the container. It would work as is but I would lose the search decorator, or vice versa depending on the order I would call them.

Here is my final setup, with your helper code…notice the select initialization is separate for more portability.

$.fn.select2.amd.define('select2/customDropdownAdapter', [
  "select2/utils",
  "select2/dropdown",  
  "select2/dropdown/closeOnSelect",
  "select2/dropdown/attachContainer",
  "select2/dropdown/search",
], function (Utils, DropdownAdapter, CloseOnSelect, AttachContainer, DropdownSearch) {

    var CustomAdapter = Utils.Decorate(     
        Utils.Decorate(
            Utils.Decorate(
            DropdownAdapter,
            DropdownSearch  
            ),
            AttachContainer         
        ),
        CloseOnSelect
    );

    return CustomAdapter; 

});

var CustomAdapter = $.fn.select2.amd.require('select2/customDropdownAdapter');

$('select').select2({
    dropdownAdapter: CustomAdapter
}); 
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Spinner not hiding the dropdown menu after selection
GONE) works to hide the dropdown but it seems to cause issues with the Spinner state, i.e. you will be unable to reopen...
Read more >
BaseAdapter with Android ListView - CodePath Cliffnotes
Before we create our custom BaseAdapter implementation, we need to create the layout for the ListView row and also a model for the...
Read more >
Customizing Android Popup Spinner (Dropdown List) with ...
PowerSpinner provides another custom adapter called IconSpinnerAdapter . We can implement a list that items have an icon like the below ...
Read more >
Using lists in Android wth ListView - Tutorial - Vogella.com
The input of a list (items in the list) can be arbitrary Java objects. The adapter extracts the correct data from the data...
Read more >
Android Spinner Disable Item Dropdown Selecetd Item
You need to close or hide the whole dropdown menu when you have only one item in your spinner. Let us achieve this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found