2015-09-01 30 views
4

如何強制select2總是下拉,永不dropup?select2 - 總是下拉,而不是dropdrop

我想這個解決方案,但是它不工作:This doesn't work

$("#e1").select2() 
.on('select2-open', function() { 

// however much room you determine you need to prevent jumping 
     var requireHeight = 600; 
     var viewportBottom = $(window).scrollTop() + $(window).height(); 

     // figure out if we need to make changes 
     if (viewportBottom < requireHeight) 
     {   
      // determine how much padding we should add (via marginBottom) 
      var marginBottom = requireHeight - viewportBottom; 

      // adding padding so we can scroll down 
      $(".aLwrElmntOrCntntWrppr").css("marginBottom", marginBottom + "px"); 

      // animate to just above the select2, now with plenty of room below 
      $('html, body').animate({ 
       scrollTop: $("#e1").offset().top - 10 
      }, 1000); 
     } 
    }); 

jsFiddle

回答

5

這爲我工作:

$.fn.select2.amd.require([ 
    'select2/selection/multiple', 
    'select2/selection/search', 
    'select2/dropdown', 
    'select2/dropdown/attachContainer', 
    'select2/dropdown/closeOnSelect', 
    'select2/utils' 
], function (MultipleSelection, Search, Dropdown, AttachContainer, CloseOnSelect, Utils) { 
    var SelectionAdapter = Utils.Decorate(
    MultipleSelection, 
    Search 
); 

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

    $('#e1').select2({ 
    dropdownAdapter: DropdownAdapter 
    }); 
}); 

jsFiddle

+0

是這個永久的解決方案,我把這段代碼放在select2.js文件或我的js文件中 –

+0

@ j.code我發現最簡單的方法是將每個require定義爲一個變量,例如'var Utils = $ .fn.select2.amd.require('select2/utils');'然後在全局創建'DropdownAdapter'。這讓我只需在需要的地方添加'dropdownAdapter:DropdownAdapter'。希望有所幫助。 – PanPipes

+0

對於好奇的人來說,'SelectionAdapter'的意思是在select2調用中像使用'selectionAdapter:SelectionAdapter'一樣使用。但我不相信這個解決方案需要它。 – PanPipes