How to change order in select2 for multiple, with given order

时间:2017-08-14 07:05:49

标签: javascript jquery html jquery-select2

I am new in select2 js so please help me for selecting value according to given order. Means when i have to store value according to user decided order.

data =  [{id:1, name:xyz},{id:2, name:abc},{id:3, name:lmn}]

Suppose I want to show id [1,2] as selected but I am getting [2,1]

1 个答案:

答案 0 :(得分:0)

添加此代码:

$('select').on('select2:select', function(e) {
  var element = $(this).find('[value="' + e.params.data.id + '"]');
  $(this).append(element);
  $(this).trigger('change');
});

或者,

$("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");
});