Select2: How to prevent tags sorting

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

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:

The mentioned "possible dublicate question" is for the older version of Select2 v3... I ask about version 4... It differs form older ones and mentioned answers dosn't solve the problem.

回答1:

I've found a solution that works with Select2 v4. It changes the order of items - item selected by user are moved to the end.

$("select").select2();  $("select").on("select2:select", function (evt) {   var element = evt.params.data.element;   var $element = $(element);      $element.detach();   $(this).append($element);   $(this).trigger("change"); });

Update

The solution was found here: github.com/select2/select2/issues/3106. Its author is kevin-brown.



回答2:

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(',') ); });


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!