选择2,需要将结果分组为一个以供显示

时间:2015-07-22 08:29:22

标签: grouping jquery-select2-4

我使用"多个选择框" (select2 examples)我想分组显示结果: enter image description here

like(仅当选择了多个X选项时):

enter image description here

除此之外,结果的字段太大。另外,我不想在字段结果中进行滚动。

有没有现成的解决方案呢?

1 个答案:

答案 0 :(得分:0)

我创建了解决方案:



$(document).ready(function() {
  $(".test").select2();
});
function select_grouping(obj) {
  var min_grouping_count = 3;
  var title = ' elements selected';
  var count = 0;
  if (obj.children('li').length > min_grouping_count) {
    if (obj.children('li').length > min_grouping_count) {
      count = obj.children('li').length - 1;
      obj.children('li').each(function(index, el) {
        if (index > 0 && index < count) {
          $(this).remove();
        }
      });
    }
    obj.children('li:eq(0)').html(count + title);
  }
}
$(".test").on("select2:select", function(obj) {
  var obj = $('#filtr_test').find('.select2-selection__rendered');
  select_grouping(obj);
});
$(".test").on("select2:unselect", function(obj) {
  var obj = $('#filtr_test').find('.select2-selection__rendered');
  select_grouping(obj);
});
&#13;
.test {
  width: 200px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />

<div id="filtr_test">
  <select class="test" multiple="multiple">
    <option value="1">one</option>
    <option value="2">two</option>
    <option value="3">three</option>
    <option value="4">four</option>
    <option value="5">five</option>
    <option value="6">six</option>
    <option value="7">seven</option>
  </select>
</div>
&#13;
&#13;
&#13;