【问题标题】:how to make inserted items sortable如何使插入的项目可排序
【发布时间】:2018-12-13 19:05:11
【问题描述】:

$('.parent').sortable({
	containment: "parent",
	tolerance: "pointer",
	axis: "x"
 });
 
 $('button').on('click', function(){
 var str = $('#store').html();
 $(str).insertBefore($('.parent').eq(0));
 });
.parent{
background:silver;
margin:9px 0;
}
.title{
background:gold;
display:inline-block;
width:25%;
cursor:cell;
}
<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>

<div id='store'>
<div class='parent'>
<div class='title'>lorem</div>
<div class='title'>ipsum</div>
</div>
</div>
<br>
<button>CLICK</button>

如何使插入的项目可排序?

我尝试了所有解决方案 herehere 和 ...

没有任何作用。

【问题讨论】:

  • 添加项目后需要在 sortable 上运行 refreshapi.jqueryui.com/sortable/#method-refresh
  • 您想将这两个项目克隆到同一个列表中吗?还是您打算制作一个新清单?
  • @Twisty,我需要新项目以与旧项目相同的方式排序 - 在其父项内。 $( ".selector" ).sortable( "refresh" ); 不起作用。
  • 看我回答的第二部分。

标签: jquery jquery-ui jquery-ui-sortable


【解决方案1】:

添加项目时,需要运行refresh:

刷新可排序的项目。触发所有可排序项目的重新加载,从而识别新项目。

在展示位置方面,您选择的似乎比您需要的要多。我怀疑您只需要在同一个列表中插入更多项目。考虑以下几点:

$(function() {
  $('.parent').sortable({
    containment: "parent",
    tolerance: "pointer",
    axis: "x"
  });

  $('button').on('click', function() {
    var str = $('#store .parent').html();
    $(str).insertBefore($('.parent div:eq(0)'));
    $('.parent').sortable("refresh");
  });
});
.parent {
  background: silver;
  margin: 9px 0;
}

.title {
  background: gold;
  display: inline-block;
  width: auto;
  cursor: cell;
}
<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>

<div id='store'>
  <div class='parent'>
    <div class='title'>lorem</div>
    <div class='title'>ipsum</div>
  </div>
</div>
<br>
<button>CLICK</button>

如果您需要一个新列表,那么您需要添加它并将可排序重新分配给新项目。

$(function() {
  $('.parent').sortable({
    containment: "parent",
    tolerance: "pointer",
    axis: "x"
  });

  $('button').on('click', function() {
    var str = $('#store .parent:last').prop("outerHTML");
    $(str).insertBefore($('.parent:eq(0)'));
    $("#store .parent").sortable({
      containment: "parent",
      tolerance: "pointer",
      axis: "x"
    });
  });
});
.parent {
  background: silver;
  margin: 9px 0;
}

.title {
  background: gold;
  display: inline-block;
  width: 25%;
  cursor: cell;
}
<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>

<div id='store'>
  <div class='parent'>
    <div class='title'>lorem</div>
    <div class='title'>ipsum</div>
  </div>
</div>
<br>
<button>CLICK</button>

希望对您有所帮助。

【讨论】:

  • 谢谢,它有效。您的链接上的refresh 功能是什么。你没有使用它。
  • @puerto 因为我们没有将新项目添加到同一个列表中,所以我不需要。我们为新项目创建了新列表并重新初始化了可排序。
猜你喜欢
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
相关资源
最近更新 更多