【问题标题】:Sortable select2 swap value可排序的 select2 交换值
【发布时间】:2018-11-30 09:16:59
【问题描述】:

我正在尝试使用 sortable 更改我的 select2 中的值,但没有白费。我尝试通过 stop 函数中的值交换循环,但似乎无法正确处理。

这是我的简化代码:

$(".select2").select2();

$("ul.select2-selection__rendered").sortable({
  containment: 'parent',
  stop: function(event, ui) {
		console.log($("#form").serializeArray())
  }
});

console.log($("#form").serializeArray())
.select2 {
  width: 100%;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.js"></script>
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <form id="form" onsubmit="return false;">
        <select class="select2" name="test1" multiple>
          <option value="1" selected>test1</option>
          <option value="2" selected>test2</option>
          <option value="3" selected>test3</option>
        </select>
      </form>
    </div>
  </div>
</div>

拖放有效,但我希望能够使用它们更改值,以便console.log 打印出重新排序的值。

任何关于如何使用 sortable 和 select2 来交换 select 中的选项的提示将不胜感激。

小提琴:https://jsfiddle.net/763opz0c/4/

【问题讨论】:

  • 为什么在渲染之前不更新值
  • @NegiRox 你是什么意思?怎么样?
  • 请看一下希望您正在寻找相同的解决方案。如果没有,那么我会根据您的要求制作一个新的。 stackoverflow.com/questions/52927099/…
  • @NegiRox 我无法为我的代码重现它(对不起,我是 JS 新手......),你能告诉我怎么做吗?我赞成你的回答,谢谢!

标签: javascript jquery jquery-ui jquery-select2 jquery-ui-sortable


【解决方案1】:

这是一种方法:

select2 将数据存储在我们可以根据要求使用/映射的列表项中。这是 select2

如何存储数据的控制台日志

$(".select2").select2({width: '300px'});

console.log($("ul.select2-selection__rendered").find('li.select2-selection__choice').data());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.css" />
<div class="container">
  <div class="row" id="row_0">
    <div class="col-md-12">
      <form id="form" onsubmit="return false;">
        <select class="select2" name="test1" id="test1_0" multiple>
          <option value="1" selected>test1</option>
          <option value="2" selected>test2</option>
          <option value="3" selected>test3</option>
        </select>
      </form>
    </div>
  </div>
</div>

使用上面的数据,我们可以在stop回调中映射列表项的数据:

$(".select2").select2({width: '400px'});

$("ul.select2-selection__rendered").sortable({
  containment: 'parent',
  stop: function(event, ui) {
  // event target would be the <ul> which also contains a list item for searching (which has to be excluded)
    var arr = Array.from($(event.target).find('li:not(.select2-search)').map(function () { 
      	return {name: $(this).data('data').text, value: $(this).data('data').id }; 
      }))
    console.log(arr);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.css" />
<div class="container">
  <div class="row" id="row_0">
    <div class="col-md-12">
      <form id="form" onsubmit="return false;">
        <select class="select2" name="test1" id="test1_0" multiple>
          <option value="1" selected>test1</option>
          <option value="2" selected>test2</option>
          <option value="3" selected>test3</option>
        </select>
      </form>
    </div>
  </div>
</div>

编辑:使表单的 serializearray 像提到的那样工作

使用上面的arr,我们可以根据arr 中的值对选项进行排序,#form.serializearray() 完全没有问题。方法如下:

$(".select2").select2({width: '400px'});

$("ul.select2-selection__rendered").sortable({
  containment: 'parent',
  stop: function(event, ui) {
  // event target would be the <ul> which also contains a list item for searching (which has to be excluded)
    var arr = Array.from($(event.target).find('li:not(.select2-search)').map(function () { 
      	return {name: $(this).data('data').text, value: $(this).data('data').id }; 
      }))
    //console.log(arr);
		
    var select = $(event.target).parents('span.select2-container').prev('select');
		// sort the options based on arr
    select.find('option').each(function (i, option) {
    	option.value = arr[i].value;
      $(option).text(arr[i].name);
    });
    
    console.log($("#form").serializeArray());
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.css" />
<div class="container">
  <div class="row" id="row_0">
    <div class="col-md-12">
      <form id="form" onsubmit="return false;">
        <select class="select2" name="test1" id="test1_0" multiple>
          <option value="1" selected>test1</option>
          <option value="2" selected>test2</option>
          <option value="3" selected>test3</option>
        </select>
      </form>
    </div>
  </div>
</div>

希望这会有所帮助。

【讨论】:

  • 感谢您的回答!但我明确表示我希望console.log($("#form").serializeArray()) 能够工作,因为我在stop 之外的几个地方使用它,根据每个select2 的更改值,还有其他几种形式。所以我不能只让你的arr 变量那样工作;不够动态。
  • 好的。我们总是可以让.serializeArray 工作,但我觉得如果你已经在你的应用程序中使用select2,最好利用它的API。我希望你知道select2 是如何工作的,对吧? form.serializeArray 寻找它的子选项/输入,它们不再被 select2 使用(我的意思是在初始化之后),所以它总是给出初始值。如果您仍然希望它工作,则必须根据 sortable 顺序重新排序选项。
  • 我知道并且我理解,但是考虑到我的代码以及我无法随心所欲地更改它,恐怕我必须围绕serializeArray() 函数工作。 . 你能帮我做吗? :)
  • 当然。你认为基于我现在所拥有的 - arr,我们可以切换/重新排序 select2 的值,这样每当你在 stop 的“外部”使用它时,它仍然会有交换的顺序。这样可以吗?无论如何,我将发布与交换实际选项相关的这篇文章的编辑,以便.serializeArray 给出交换的订单。一会儿我会做的。
  • 哈哈。很高兴我能帮助你。如果您遇到任何问题,请告诉我。标记我。快乐编码! :)
【解决方案2】:

您好,您可以像这样更新您的可排序函数。

$(".select2").select2();
var formData=[];
$("ul.select2-selection__rendered").sortable({
  containment: 'parent',
  stop: function(event, ui) {
      formData=[];
      var _li= $('li.select2-selection__choice');
        _li.each(function(idx) {
          var currentObj=$(this);
          var data=currentObj.text();
          data=data.substr(1,data.length);
          formData.push({name:data,value:currentObj.val()})
     })
        console.log(formData)
  },
   update: function() {
        var _li= $('li');
       // _li.removeAttr("value");
        _li.each(function(idx) {
          var currentObj=$(this);
          console.log(currentObj.text());
          $(this).attr("value", idx + 1);
        })
   }
});

请看here

【讨论】:

  • 更新了小提琴和代码,因此您可以在任何地方使用 formData
  • 它仍然没有按照我的要求做,问题很明显我希望console.log($("#form").serializeArray()) 工作,我不是在寻找解决方法.. 无论如何感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
相关资源
最近更新 更多