简体   繁体   English

select2 中的右对齐下拉菜单

[英]Right-align dropdown menu in select2

Is there a way to right-align the dropdown menu with dropdownAutoWidth: true in a select2, instead of the standard left-align?有没有办法在 select2 中使用dropdownAutoWidth: true右对齐下拉菜单,而不是标准的左对齐? I want the select to stay the same, but the dropdown to move to the left so it aligns with the select on the right side.我希望选择保持不变,但下拉菜单向左移动,以便与右侧的选择对齐。 See snippet below with the wrong alignment...请参阅下面错误对齐的片段...

Note: I want different sizes of the select and the dropdown just as shown.注意:我想要不同大小的选择和下拉菜单,如图所示。

Edit // Updated with suggestion to add direction: rtl; text-align: right;编辑 // 更新了添加direction: rtl; text-align: right;建议direction: rtl; text-align: right; direction: rtl; text-align: right; . . This only right-aligns the text.这只会右对齐文本。 I want the whole dropdown to right-align with the selected option above it.我希望整个下拉菜单与其上方的选定选项右对齐。 Eg the dropdown should stick out to the left of the selected option, not to the right.例如,下拉菜单应该突出在所选选项的左侧,而不是右侧。

Edit 2 // Updated with .select2-dropdown { left: -69px !important; }编辑 2 // 更新为.select2-dropdown { left: -69px !important; } .select2-dropdown { left: -69px !important; } This makes it look the way I want, but is there a way to not have to set a fixed value of -69px? .select2-dropdown { left: -69px !important; }这使得它看起来就是我想要的,但有没有办法不用设置-69px固定值?

 $(".form-control").select2({ width: '100px', dropdownAutoWidth : true, });
 .select2-container--default .select2-results > .select2-results__options { direction: rtl; text-align: right; } .select2-dropdown { left:-69px !important; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script> <select class="form-control"> <option selected="selected">orange</option> <option>white</option> <option selected="selected">purple</option> </select>

I want it to look like this, but not with a fixed value to achieve it.我希望它看起来像这样,但没有固定值来实现它。

在此处输入图片说明

As you want to have different sizes of the select and the dropdown, here is my solution using select2 built-in options and some CSS , but maybe not perfect one. 由于你想要有不同大小的选择和下拉列表,这里是我使用select2内置选项和一些CSS的解决方案,但可能不是完美的。

Making rtl is dead easy, just use dir , but we need to pass some other options as shown below: 使rtl简单易用,只需使用dir ,但我们需要传递一些其他选项,如下所示:

$('select').select2({
    dir: "rtl",
    dropdownAutoWidth: true,
    dropdownParent: $('#parent')
});

The key here is dropdownParent , also you need to edit your HTML as follow: 这里的关键是dropdownParent ,您还需要编辑HTML ,如下所示:

<div id='parent'>
    <select>
      <option selected="selected">orange</option>
      <option>white</option>
      <option selected="selected">purple</option>
    </select>
</div>

And finally you need some CSS: 最后你需要一些CSS:

#parent {
  /* can be any value */
  width: 300px;
  text-align: right;
  direction: rtl;
  position: relative;
}
#parent .select2-container--open + .select2-container--open {
  left: auto;
  right: 0;
  width: 100%;
}

You can see it in action here on codepen 您可以在codepen上看到它的运行情况

If you have more that one select, you need some JS code to handle dropdownParent option. 如果你有更多选择,你需要一些JS代码来处理dropdownParent选项。 btw your life will be easier if you remove dropdownAutoWidth option ;) 顺便说一句,如果你删除dropdownAutoWidth选项,你的生活会更容易;)

If still looking for an alternative method. 如果还在寻找替代方法。 I recently needed to achieve this as well and the RTL answer was not a viable solution for my project. 我最近也需要实现这一点,RTL答案对我的项目来说不是一个可行的解决方案。 If you are using the select2.js(4.0.6) and do not mind modifying the script you can replace the following code and achieve right positioning with adding a class to the target element. 如果您正在使用select2.js(4.0.6)并且不介意修改脚本,则可以替换以下代码并通过向目标元素添加类来实现正确定位。

Look for lines 4381 to 4384 寻找4381至4384行

var css = {
  left: offset.left,
  top: container.bottom
};

Replace with the following 替换为以下内容

var containerWidth = this.$container.outerWidth()
var right = ($("body").outerWidth() - (offset.left + containerWidth));

if (this.$element.hasClass("right-align")) {
    var css = {
        right: right,
        top: container.bottom
    };
} else {
    var css = {
        left: offset.left,
        top: container.bottom
    };
}

Add the class right-align to your target select input. 将类右对齐添加到目标选择输入。

<select id="client_select" class="form-control right-align">
     <option value="01">Example One</option>
     <option value="02">Example Two</option>
     <option value="03">Example Three</option>
</select>

That should be all you need. 这应该就是你所需要的。 If the target element has the class then the code will position the dropdown using right instead of left positioning. 如果目标元素具有类,则代码将使用right定位下拉列表而不是左定位。 Should work the same if container is set to a parent. 如果容器设置为父级,则应该工作相同。 However, this is not thoroughly tested. 但是,这还没有经过彻底的测试。 It may not be an elegant solution, but it does not require overriding anything with CSS. 它可能不是一个优雅的解决方案,但它不需要用CSS覆盖任何东西。 Should do the trick until an update is made to add an option for handling right aligned dropdowns. 在进行更新以添加用于处理右对齐下拉列表的选项之前,应该这样做。 Hope it helps. 希望能帮助到你。

init select with dir: "rtl" such as: 用dir选择init:“rtl”如:

$('select').select2({
   dir: "rtl,
   ...
   ...
   ...
   ...
   })

I managed to make it work slightly different.我设法使它的工作略有不同。 I needed to expand the dropdown to the left while keeping the options texts aligned left.我需要将下拉菜单向左展开,同时保持选项文本向左对齐。 I thought it's worth sharing.我认为值得分享。 Here is the setup:这是设置:

HTML HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<div id="select2-container">
    <select class="form-control">
        <option selected="selected">orange</option>
        <option>white</option>
        <option>purple</option>
    </select>
</div>

CSS CSS

.select2-container.select2-container--default.select2-container--open {
    right: auto;
    left: unset !important;
}

.select2-container.select2-container--default.select2-container--open .select2-dropdown.select2-dropdown--below {
    width: fit-content !important;
    left: unset;
    right: 0;
}

JavaScript JavaScript

$('.form-control').select2({ dropdownParent: $('#select2-container') });

Result looks like:结果看起来像: 在此处输入图片说明

选择的选项标签不能被css修改它需要做jquery插件http://selectric.js.org/demo.html它应该帮助你我的朋友

I think it will help you to get what exactly you need.if u wont get it ping me back 我想它会帮助你得到你究竟需要的东西。如果你不让它回来我

 select { text-align-last: right; } option { direction: rtl; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script> <select class="form-control"> <option selected="selected">orange</option> <option>white</option> <option selected="selected">purple</option> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM