Reputation: 2084
I have multiple connectedSortable
sections, each div has one div.widget
inside of it, as following :
<div class="row">
<section class="col-xs-4 col-sm-4 col-md-4 col-lg-4 connectedSortable">
<div class="widget">Widget 1</div>
</section>
<section class="col-xs-4 col-sm-4 col-md-4 col-lg-4 connectedSortable">
<div class="widget">Widget 2</div>
</section>
<section class="col-xs-4 col-sm-4 col-md-4 col-lg-4 connectedSortable">
<div class="widget">Widget 3</div>
</section>
</div>
<div class="row">
<section class="col-xs-3 col-sm-3 col-md-3 col-lg-3 connectedSortable">
<div class="widget">Widget 4</div>
</section>
<section class="col-xs-3 col-sm-3 col-md-3 col-lg-3 connectedSortable">
<div class="widget">Widget 5</div>
</section>
<section class="col-xs-3 col-sm-3 col-md-3 col-lg-3 connectedSortable">
<div class="widget">Widget 6</div>
</section>
<section class="col-xs-3 col-sm-3 col-md-3 col-lg-3 connectedSortable">
<div class="widget">Widget 7</div>
</section>
</div>
Using the sortable jQuery plugin, it will insert the dragged div into connectedSortable
section instead of swapping.
What I want to do is instead of inserting that div inside connectedSortable
section, I want to swap it with the div which is inside that connectedSortable
section.
And when a connectedSortable
section has no div.widget
inside of it, I can drop only one div.widget
and nothing to be swapped since it's already empty.
How can I do that ?
This is a jsfiddle of what I tried : http://jsfiddle.net/pus4aff6/
Upvotes: 0
Views: 1350
Reputation: 2084
I created a solution where I worked with the start
and the receive
events as following :
start: function(event, ui){
ui.item.startPos = ui.item.index();
},
receive: function(event, ui) {
var source = ui.sender;
var target = $(this);
var draggedItem = ui.item;
if(target.attr('id') !== 'widgets-container'){
if(target.find('div.widget').length >= 2){
if(source.is('#widgets-container')){
draggedItem.remove();
if(ui.item.startPos === 0) {
source.prepend(draggedItem);
}else{
source.children().eq(ui.item.startPos-1).after(draggedItem);
}
}else{
var elementToSwap = target.find('div.widget').not(draggedItem)[0];
source.html(elementToSwap);
}
}
}
}
Here the update jsfiddle : http://jsfiddle.net/frgj5qmm/
Upvotes: 1
Reputation: 1929
I would not use the bootstrap rows for this widget but create my own grid based on the jQuery UI Sortable description with css flexbox.
$( function() {
$( "ul" ).sortable().disableSelection();
} );
ul{
list-style-type: none; /* remove bullets */
display: flex; /* flexbox */
flex-wrap: wrap; /* multiple lines */
align-items: stretch;
}
li{
flex-grow: 1; /* stretch box width */
width: 20vw; /* 20vw is 20% of the browser width, you can replace it with an absolute value */
min-height:50px;
cursor:move;
}
/* remove margin & padding for this demo */
body, ul{
margin: 0;
padding: 0;
}
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css" type="text/css" media="all" />
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js" type="text/javascript"></script>
<ul>
<li class="ui-state-default">Widget 1</li>
<li class="ui-state-default">Widget 2</li>
<li class="ui-state-default">Widget 3</li>
<li class="ui-state-default">Widget 4</li>
<li class="ui-state-default">Widget 5</li>
<li class="ui-state-default">Widget 6</li>
<li class="ui-state-default">Widget 7</li>
<li class="ui-state-default">Widget 8</li>
<li class="ui-state-default">Widget 9</li>
<li class="ui-state-default">Widget 10</li>
<li class="ui-state-default">Widget 11</li>
</ul>
Remove the classes ui-state-default
to use your own design instead of jQuery UI.
Upvotes: 1