我想(通过键盘操作符)在无序列表中选择多个项目,然后使用jQuery Sortable将它们拖动到同一列表中的另一个点。
答案 0 :(得分:2)
以下是我的做法(为此answer修改我的question ):
$(function() {
$('ul').on('click', 'li', function() {
$(this).toggleClass('selected');
});
$("ul").sortable({
revert: true,
helper: function(e, item) {
if (!item.hasClass('selected')) item.addClass('selected');
var elements = $('.selected').not('.ui-sortable-placeholder').clone();
var helper = $('<ul/>');
item.siblings('.selected').addClass('hidden');
return helper.append(elements);
},
start: function(e, ui) {
var elements = ui.item.siblings('.selected.hidden').not('.ui-sortable-placeholder');
ui.item.data('items', elements);
var len = ui.helper.children().length;
var height = ui.item.height() + 5;
ui.helper.height((len * height))
ui.placeholder.height((len * height))
},
receive: function(e, ui) {
ui.item.before(ui.item.data('items'));
},
stop: function(e, ui) {
ui.item.siblings('.selected').removeClass('hidden');
$('.selected').removeClass('selected');
}
});
});
&#13;
* {
margin: 0;
padding: 0;
}
#sortable {
width: 300px;
padding: 5px;
margin-right: 100px;
background: #eee;
border: 1px solid black;
}
ul li {
width: 250px;
margin: 5px;
padding: 5px;
font-size: 1.2em;
cursor: move;
background-color: white;
list-style-type: none;
}
.selected {
background: red !important;
}
.hidden {
display: none !important;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<ul id="sortable">
<li>Item #1</li>
<li>Item #2</li>
<li>Item #3</li>
<li>Item #4</li>
<li>Item #5</li>
</ul>
&#13;
(在演示中点击每个项目选择多个项目)