最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding custom tags style in Select2 MultiSelect Dropdown - Stack Overflow

programmeradmin3浏览0评论

so I'm using Select2 4 library (/) and I have a problem.

<select id="test" multiple style="width:800px;">
    <option value="a">aaaaa</option>
    <option value="b">bbbbb</option>
    <option value="c">cccc</option>
    <option value="d">ddddd</option>
</select>

(...)

$("#test").select2({
   tags: true,
   tokenSeparators: [',', ' ']
})

This is generating, after making some choices DOM element, which contains some <li>s

So - it works fine. But I need to add my classes to those <li> elements after changing. I don't know how to do that because Select2 rewrites my classes instantly after adding.

I've tried something like this:

selectEl.change(function() {
  var elements = $('.select2-selection > ul > li');
  elements.each(function() {
    $(this).addClass('my-test-class');
  });
});

but nothing changes (except for the last element, which is an input). I mean - I think it works, but Select2 instantly rewrites it.

I've tried doing it using change, select2:select listeners and none of those works fine.

The idea is - adding 'select2-auto' to <li>s that are chosen from the drop-down list, and 'select-manual' class to the others.

so I'm using Select2 4 library (https://select2.github.io/) and I have a problem.

<select id="test" multiple style="width:800px;">
    <option value="a">aaaaa</option>
    <option value="b">bbbbb</option>
    <option value="c">cccc</option>
    <option value="d">ddddd</option>
</select>

(...)

$("#test").select2({
   tags: true,
   tokenSeparators: [',', ' ']
})

This is generating, after making some choices DOM element, which contains some <li>s

So - it works fine. But I need to add my classes to those <li> elements after changing. I don't know how to do that because Select2 rewrites my classes instantly after adding.

I've tried something like this:

selectEl.change(function() {
  var elements = $('.select2-selection > ul > li');
  elements.each(function() {
    $(this).addClass('my-test-class');
  });
});

but nothing changes (except for the last element, which is an input). I mean - I think it works, but Select2 instantly rewrites it.

I've tried doing it using change, select2:select listeners and none of those works fine.

The idea is - adding 'select2-auto' to <li>s that are chosen from the drop-down list, and 'select-manual' class to the others.

Share Improve this question edited Apr 25, 2017 at 15:13 Curiousdev 5,7883 gold badges25 silver badges38 bronze badges asked Apr 25, 2017 at 10:10 alanmckneealanmcknee 1992 gold badges4 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You can use templateSelection: for custom tags styles please find below snippet for your reference..

templateSelection: function(selection) {
        if(selection.selected) {
            return $.parseHTML('<span class="customclass">' + selection.text + '</span>');
        }
        else {
            return $.parseHTML('<span class="customclass">' + selection.text + '</span>');
        }
    }

$("#test").select2({
   tags: true,
   allowClear: true, //
   closeOnSelect: false,
   tokenSeparators: [',', ' '],
   templateSelection: function(selection) {
        if(selection.selected) {
            return $.parseHTML('<span class="customclass">' + selection.text + '</span>');
        }
        else {
            return $.parseHTML('<span class="customclass">' + selection.text + '</span>');
        }
    }

});

$("#test").on("select2:select", function(e) { 
$("li[aria-selected='true']").addClass("customclass");
$("li[aria-selected='false']").removeClass("customclass");
$('.select2-search-choice:not(.my-custom-css)', this).addClass('my-custom-css');
});

$("#test").on("select2:unselect", function(e) { 
$("li[aria-selected='false']").removeClass("customclass");
});
.selectRow {
    display : block;
    padding : 20px;
}
.select2-container {
    width: 200px;
}

.customclass
{
  color:red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare./ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<select id="test" multiple style="width:800px;">
    <option value="a">aaaaa</option>
    <option value="b">bbbbb</option>
    <option value="c">cccc</option>
    <option value="d">ddddd</option>
</select>

dropdownCssClass Property use for adding class to the select2 drop-down menu. Something like this,

$("#test").select2({
     tags: true,
     tokenSeparators: [',', ' '],
     containerCssClass: "my-test-class",
     dropdownCssClass: "test-class"
 });

I hope this will work for you. Thanks.

$("#test").select2({
   tags: true,
   allowClear: true, //
   closeOnSelect: false,
   tokenSeparators: [',', ' ']
});

$("#test").on({
  "select2:select": function(e) { 
      $("li[aria-selected='true']").addClass("customclass");
      $("li[aria-selected='false']").removeClass("customclass");
    },
    "select2:opening": function(e) { 
        setTimeout(function(){
          $("li[aria-selected='true']").addClass("customclass");
          $("li[aria-selected='false']").removeClass("customclass");
        },0)
    },
    "select2:unselect" : function(e) { 
        $("li[aria-selected='false']").removeClass("customclass");
    }
});
.selectRow {
      display : block;
      padding : 20px;
  }
  .select2-container {
      width: 200px;
  }

  .customclass
  {
    color:red;
  }
<link href="https://cdnjs.cloudflare./ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/select2/4.0.3/js/select2.full.min.js"></script>


<select id="test" multiple style="width:800px;">
    <option value="a">aaaaa</option>
    <option value="b">bbbbb</option>
    <option value="c">cccc</option>
    <option value="d">ddddd</option>
</select>

发布评论

评论列表(0)

  1. 暂无评论