I created a field editor where you can create several blocks via drag & drop and they are cloned, because you can use as much blocks as you want. Then there is the chance to put in fields from a list on the left side.
This works fine so far.
<div >
<h2>Structure</h2>
<div >
<div data->New block</div>
<div data->New headline</div>
</div>
<h2>Fields</h2>
<div >
<div >Date</div>
<div >Time</div>
<div >Relation</div>
<div >Text</div>
</div>
</div>
<div ></div>
My script looks like this:
$(".structure .item1").draggable({
connectToSortable: ".sortable",
helper: "clone",
stop: function (event, ui) {
$(".sortable .item1").sortable({
connectWith: ".sortable .item1"
});
}
});
$(".sortable").sortable({
receive: function (event, ui) {
var itemClass = $(ui.item).attr("data-class");
$(".sortable ." itemClass).css(
{ height: "auto", width: "auto" }
);
}
});
$(".fields").sortable({
connectWith: ".sortable .block",
items: ".item2"
});
Unfortunately it's not possible to remove fields from a block on the right side.
Can you tell me what I have to do and why this doesn't work?
This is what my code looks like https://codepen.io/alphafrau/pen/gOvzjGj
CodePudding user response:
When you initialize the Sortable inside the Sortable, you will want to define the connectWith
option. I would suggest:
connectWith: ".sortable .item1, .fields"
This allows it to be moved to any of the sortable lists.
$(function() {
$(".structure .item1").draggable({
connectToSortable: ".sortable",
helper: "clone",
stop: function(event, ui) {
$(".sortable .item1").sortable({
connectWith: ".sortable .item1, .fields"
});
}
});
$(".sortable").sortable({
receive: function(event, ui) {
var itemClass = $(ui.item).attr("data-class");
$(".sortable ." itemClass).css({
height: "auto",
width: "auto"
});
}
});
$(".fields").sortable({
connectWith: ".sortable .block",
items: ".item2"
});
});
.draggable {
background: #CCC;
border: 2px solid #666;
padding: 0 10px;
position: absolute;
top: 20px;
left: 20px;
bottom: 20px;
width: 300px;
}
.draggable .item1 {
background: lime;
border: 2px solid green;
padding: 5px;
margin: 2px;
}
.draggable .item2 {
background: Yellow;
border: 2px solid Orange;
padding: 5px;
margin: 2px;
}
.fields {
background: aqua;
padding: 10px;
}
.sortable {
background: Orange;
border: 2px solid OrangeRed;
padding: 10px;
margin: 0 0 0 350px;
}
.sortable .item1 {
background: fuchsia;
border: 2px solid brown;
padding: 5px;
position: relative;
margin: 2px;
}
.sortable .item2 {
background: Yellow;
border: 2px solid Orange;
padding: 5px;
margin: 2px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<div >
<h2>Structure</h2>
<div >
<div data->New block</div>
<div data->New headline</div>
</div>
<h2>Fields</h2>
<div >
<div >Date</div>
<div >Time</div>
<div >Relation</div>
<div >Text</div>
</div>
</div>
<div ></div>