Select2 change container position

房东的猫 提交于 2019-12-10 09:19:22

问题


How can I adjust the position of Select2 container so that the search box is position right over the original select element like in this website

http://www.jobnisit.com/en

It look cleaner in terms of UI in my opinion.

Ps. sorry, I can't post the image now.


回答1:


There is 2 ways to do this.

1) With css:

.select2-dropdown--below {
    top: -2.8rem; /*your input height*/
}

This will not affect a container (.select2-container), but will move dropdown and search field, so you will have a desired effect.

2) With js:

$('select').select2().on('select2:open', function() {
  var container = .$('.select2-container').last();
  /*Add some css-class to container or reposition it*/
});

This code attaches a handler to 'select2:open' event, which will be fired every time when user opens a dropdown. This method is better if you have more than one select on page.

Tested with select2 4.0.0




回答2:


The proper way of positioning the dropdown is using the core feature provided by select2 plugin.

It provides us with 'dropdownParent' property to place to dropdown inside the particular element

select field: #edit-field-job-skillsets-tid

parent item: div.form-item-field-job-skillsets-tid

jQuery("#edit-field-job-skillsets-tid").select2(
  {dropdownParent: jQuery('div.form-item-field-job-skillsets-tid')}
 );


来源:https://stackoverflow.com/questions/31556402/select2-change-container-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!