Select2: How to prevent tags sorting

后端 未结 3 446
臣服心动
臣服心动 2020-12-03 10:51

When user selects many items (tags), they are automatically sorted alphabetically. How to prevent automatic sort and keep user\'s order using Select2 4.0?

Update:

3条回答
  •  爱一瞬间的悲伤
    2020-12-03 11:07

    This has been discussed before for Select2 3.5.2, you can use select2('data') to get the order.

    $("select").select2();
    
    $('#sayResult').click(function () {
      // 'data' brings the unordered list, while val does not
      var data = $('select').select2('data');
      
      // Push each item into an array
      var finalResult = data.map(function () {
        return this.id;
      });
    
      // Display the result with a comma
      alert( finalResult.join(',') );
    });
    
    
    
    
    
    
    
    

提交回复
热议问题