简体   繁体   English

选择多个/所有选项时,选择多个选择变得很大

[英]Chosen multiple select becomes huge when many/all options are selected

When using Chosen.js on a multiple select field, if there are over 500 options that the user has selected, the list just becomes ridiculously long. 在多选字段上使用Chosen.js时,如果用户选择了超过500个选项,则列表变得非常长。

在此输入图像描述

Is there any way I could limit the number of show elements? 有什么办法可以限制节目元素的数量吗? For example when chosing over 3 options, the user will have (4) options are selected... , instead of them being listed. 例如,当选择超过3个选项时,用户将选择(4)选项...而不是列出它们。

在此输入图像描述

I wonder why there's no such option in their documentation. 我想知道为什么他们的文档中没有这样的选项。

Thanks in advance. 提前致谢。

You can use something like this: 你可以使用这样的东西:

$('.element').chosen().change(function() {
    var totalSelected = $(this).find('option:selected').size();
    var placeholder = $(this).find('option:first-child').text();
    if(totalSelected > 3) {
        $(this).next().find('.chosen-choices').find('li.search-choice').hide(),
        $(this).next().find('.chosen-choices').find('.literal-multiple').show();
        $(this).next().find('.chosen-choices').find('span.literal-multiple').text(placeholder + " ("+totalSelected+")");
    }
});

The class literal-multiple is a custom element to show the totalSelected elements. class literal-multiple是一个自定义元素,用于显示totalSelected元素。 You need to add it in the prototype of the chosen plugin: 您需要将其添加到所选插件的原型中:

file chosen.jquery.js file chosen.jquery.js

Chosen.prototype.set_up_html = function() {
   //stuff
   if(this.is_multiple) {
     var selVal  = this.default_text;
     this.container.html('<ul class="chosen-choices"><span class="literal-multiple"></span></ul>');
   }
};

SOrry I am unable to comment since I don't have enough reputation. SOrry我无法评论,因为我没有足够的声誉。 But to add to the previous answer, instead of adding a separate container, why don't we just append the n users selected as a <li> item. 但是要添加到上一个答案,我们为什么不添加选择为<li>项目的n个用户,而不是添加单独的容器。

Something like this - 像这样的东西 -

$('.element').chosen().change(function() {
    var totalSelected = $(this).find('option:selected').size();
    var placeholder = $(this).find('option:first-child').text();
    if(totalSelected > 3) {
       $(this).next().find('.chosen-choices').find('li.search-choice').hide(),
       $(this).next().find('.chosen-choices')append('<li class="search-choice" <span>'+totalSelected+' users selected. </li>');
}
});

This seems to work for me. 这似乎对我有用。

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

相关问题 jQuery 选择:如何从多个选择中获取所选选项文本的数组 - jQuery Chosen : How to get array of selected options text from a multiple select 使用selected-js选择多个选项 - Multiple options select with chosen-js 选择特定选项后,请从选定的多选下拉列表中禁用所有选项 - Disable all options from a chosen multiselect dropdown when a particular option is selected 如何在 EJS 中的多个选择字段中检查所有选定的选项 - how to check all selected options in multiple select field in EJS 如何使用多个引导选择显示所有选定的选项? - How to display all selected options using multiple bootstrap-select? 如何使用Chosen.JS多重选择以编程方式选择多个选项 - How to programmatically select multiple options with Chosen.JS multiple select 放置div时,JQuery反转多个选择框中的所选选项不起作用 - Invert chosen options in multiple select boxes by JQuery does not act when putting a div around 在多选中选择所选选项 - Picking selected options in a multiple select 多个 select get Selected options in order to selected - Multiple select get Selected options in order selected 在特定下拉列表中选择某个选项后,从所有其他选定的多个下拉列表中删除一个选项 - Removing an option from all other chosen multiple dropdowns, when selected on a particular dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM