[英]jQuery Sortable - Move item to top of list if dragged out.
嗨,我正在使用jQuery Sortable,我有两个用横幅广告填充的列表,并且可以更改“选定横幅广告”中横幅广告的顺序。 我们有3套横幅:
我有两个列表,一个称为“可用横幅”,另一个称为“选定的横幅”。 默认标语以红色定义,如果存在两个或多个,则可以将其删除。
但是,当我删除这些默认横幅时,它们要么放置在列表的底部,要么放置在我放置它们的位置,具体取决于放置的精度。
是他们使用jQuery的一种方式,我可以将具有defaultbanner类名称的所有项目移至#sortable2中列表的顶部?
我的代码在下面,或者您可以查看我的JSFiddle
JS.js
$( document ).ready(function() {
$(function() {
$("#sortable1").sortable({
cancel: ".ui-state-disabled",
handle: ":not(input)"
});
$("#sortable2").sortable({
cancel: ".ui-state-disabled",
receive: function (event, ui) {
if (ui.item.hasClass("defaultbanner")) {
$(ui.sender).sortable('cancel');
alert("This is a default banner, it can be sorted but not removed.");
}
}
});
//new function to define that if a Partner has more than 2 selectable banners then the defaultbanner. If less than two selectable banners than the default banner cannot be
$("#sortable2").sortable({
cancel: ".ui-state-disabled",
receive: function (event, ui) {
if (ui.item.hasClass("defaultbanner") && $('#sortable1 li').length <= 1) {
$(ui.sender).sortable('cancel');
alert("This is a default banner, it can be sorted but not removed.");
}
else if($('#sortable1 li').length <= 1) {
$(ui.sender).sortable('cancel');
alert('You must have at least two banners');
}
}
});
$( "#sortable1, #sortable2" ).sortable({
connectWith: ".connectedSortable"
}).disableSelection();
$("#sortable1 li,#sortable2 li").disableSelection();
// no more than 7 banners allowed
$("#sortable1").sortable({
connectWith: '.connectedSortable',
//receive: This event is triggered when a connected sortable list has received an item from another list.
receive: function(event, ui) {
// so if > 7
if ($(this).children().length > 7) {
//ui.sender: will cancel the change. Useful in the 'receive' callback.
$(ui.sender).sortable('cancel');
alert('Your selection has been cancelled. A maximum 7 banners are allowed in the carousel.');
}
if ( $('#sortable1 .selectablebanner').length > 4) {
alert('Your selection has been cancelled. A maximum 4 custom banners are allowed in the carousel.');
$(ui.sender).sortable('cancel');
}
}
}).disableSelection();
});
});
是的,只需添加一个伪sorting
函数作为receive
的最后一次调用
function doSortMe(){
console.log('sorting');
$('#sortable2').prepend($('#sortable2 .defaultbanner'));
}
///// .....
$("#sortable2").sortable({
cancel: ".ui-state-disabled",
receive: function (event, ui) {
if (ui.item.hasClass("defaultbanner") && $('#sortable1 li').length <= 1) {
$(ui.sender).sortable('cancel');
alert("This is a default banner, it can be sorted but not removed.");
} else if ($('#sortable1 li').length <= 1) {
$(ui.sender).sortable('cancel');
alert('You must have at least two banners');
}
doSortMe(); //<-- Added call here
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.