Issue
I created an html file with 2 button (add row, add column) to add Row-box and column-box. To create row-box click add row button , to add column-box inside the row, click add column and then click any added row-box and can drag column-box to another row-box also. I am looking for a solution to drag the entire row-box inside to another column-box.
When clicking add row , the row-box appending to the 'contentBox' id Container
HTML:
<div class="wrapper">
<div class="main-content">
<div class="header">
<button class="add-row" onclick="addRow()">Add Row +</button>
<button class="add-column" onclick="addCol()">Add Column +</button>
</div>
<div class="content-box" id="contentBox">
</div>
</div>
</div>
JS:
// Row Addition
var i=0;
let newColNode = null;
function addRow() {
const row = document.createElement('div');
row.className = 'row-block';
row.id = "b" + ++i;
document.getElementById('contentBox').appendChild(row);
row.addEventListener('click', function (event) {
if(newColNode != null)
row.appendChild(newColNode);
newColNode = null
});
row.addEventListener('dragover', dragOver);
row.addEventListener('dragenter', dragEnter);
row.addEventListener('dragleave', dragLeave);
row.addEventListener('drop', Drop);
}
// Row Addition
function addCol() {
const col = document.createElement('p');
col.className = 'column-block';
col.setAttribute('draggable', true);
col.textContent = `Column`;
newColNode = col;
col.addEventListener('dragstart', dragStart);
col.addEventListener('dragend', dragEnd);
}
// drag and drop
var p = document.getElementsByTagName('p');
var choice = document.getElementsByClassName('row-block');
var dragItem = null;
for (var i of p) {
i.addEventListener('dragstart', dragStart);
i.addEventListener('dragend', dragEnd);
}
function dragStart() {
dragItem = this;
setTimeout=(()=>this.style.display = "none", 0)
}
function dragEnd() {
setTimeout=(()=>this.style.display = "none", 0)
dragItem = null;
}
for(j of choice){
j.addEventListener('dragover', dragOver);
j.addEventListener('dragenter', dragEnter);
j.addEventListener('dragleave', dragLeave);
j.addEventListener('drop', Drop);
}
function Drop(){
this.append(dragItem);
}
function dragOver(e){
e.preventDefault();
}
function dragEnter(e){
e.preventDefault();
}
function dragLeave(){
// this.style.borderColor = "none";
}
CSS:
* {
box-sizing: border-box;
}
.wrapper {
float: left;
width: 100%;
height: 100vh;
}
body {
padding: 0;
margin: 0;
position: relative;
}
.sidebar {
float: left;
width: 300px;
background: #03A9F4;
height: 100%;
padding: 10px;
}
.main-content {
float: left;
width: calc(100% - 300px);
height: 100%;
background: #fafafa;
}
.each-draggable-item {
width: 100%;
height: 40px;
padding: 0 10px;
display: flex;
align-items: center;
border: 1px solid #ccc;
background: white;
margin-bottom: 10px;
border-radius: 6px;
}
.header {
height: 60px;
display: flex;
align-items: center;
justify-content: flex-end;
padding: 0 20px;
}
.content-box {
width: 100%;
height: calc(100% - 60px);
padding: 15px;
}
button {
background: #000;
border: 0;
padding: 0 20px;
height: 40px;
margin-left: 10px;
font-weight: 600;
color: white;
cursor: pointer;
}
.row-block {
width: 100%;
border: 2px dashed #848484;
padding: 20px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin-bottom: 20px;
}
.row-block:hover {
border-color: #2654d1;
}
.column-block {
position: relative;
width: 100%;
min-height: 1px;
padding-right: 10px;
padding-left: 10px;
margin: 0 10px;
-ms-flex-preferred-size: 0;
flex-basis: 0;
-webkit-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
max-width: 100%;
border: 2px dashed #848484;
background-color: #dedede;
padding: 20px;
}
.column-block:first-child {
margin-left: 0;
}
.column-block:last-child {
margin-right: 0;
}
.container .ex-moved {
background-color: #e74c3c;
}
.container.ex-over {
background-color: rgba(255, 255, 255, 0.3);
}
.handle {
background-color: rgba(0, 0, 0, 0.4);
cursor: move;
margin-right: 5px;
padding: 0 5px;
}
.sidebar .column-block {
min-height: auto;
margin: 0 0 20px;
background: white;
border: 1px solid #ccc;
}
.column-block h5 {
margin: 0;
}
Attaching the js fiddle along with this.
https://jsfiddle.net/gph82mfa/
Solution
You should add events to columns and rows. https://jsfiddle.net/Lzp3mu5a/
// Row Addition
var i = 0;
var j = 0;
let newColNode = null;
function addRow() {
const row = document.createElement('div');
row.className = 'row-block';
row.setAttribute('draggable', true);
++i;
row.textContent = `Row-${i}`;
row.id = "row" + i;
document.getElementById('contentBox').appendChild(row);
row.addEventListener('click', function(event) {
if (newColNode != null)
row.appendChild(newColNode);
newColNode = null
});
row.addEventListener('dragstart', dragStart);
row.addEventListener('dragend', dragEnd);
row.addEventListener('dragover', dragOver);
row.addEventListener('dragenter', dragEnter);
row.addEventListener('dragleave', dragLeave);
row.addEventListener('drop', Drop);
}
// Row Addition
function addCol() {
const col = document.createElement('p');
col.className = 'column-block';
col.setAttribute('draggable', true);
++j;
col.textContent = `Column-${j}`;
col.id = "col" + j;
newColNode = col;
col.addEventListener('dragstart', dragStart);
col.addEventListener('dragend', dragEnd);
col.addEventListener('dragover', dragOver);
col.addEventListener('dragenter', dragEnter);
col.addEventListener('dragleave', dragLeave);
col.addEventListener('drop', Drop);
}
// drag and drop
var dragItem = null;
var dropItem = null;
function dragStart() {
if (dragItem !== null) return;
console.log("from: ", this.id);
dragItem = this;
setTimeout = (() => this.style.display = "none", 0)
}
function dragEnd() {
setTimeout = (() => this.style.display = "none", 0)
dragItem = null;
dropItem = null;
}
function Drop() {
if (dropItem !== null) return;
console.log("to: ", this.id);
dropItem = this;
if (dragItem !== null)
this.append(dragItem);
}
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
}
function dragLeave() {
// this.style.borderColor = "none";
}
Answered By - hcharles25
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.