I have this .I am able to drag where ever I want.Is there a way to create another table that I will get the positions of each image where it goes?(only the position).An example,as you can see there are positions 1 until 24.In position 12 I have red images.In position 24 I have black images.What I was to do is if I drag from position 12 the image with number a to the position,I want to put this value lets say in the slot 11, then when I did that drag it will be creating a new table like and will write (11,a).Conclusion, 11 will be the position and a the image i transfer.Can that happen?I am trying so far with not a result..thanks in advance.
edited: you drag an image in position lets say 5,a next drag you do with the same image you go to (1,a ) then you can not drag anymore that image -(if that can happen )
It's much easier when you decouple the data from the UI - meaning that a dataset is being processed in the background while the images are dragged around.
In the following little snippet I'll attempt to do that with your case. The items keep track if they can be dragged or not.
Theitems
are now filled with only 4 images, but you can add any number of images there - they can be handled as separate items. The only thing to pay attention to: theid
s of the items must be unique.
const items = [{
id: 0,
dataid: "a",
type: "red",
src: "https://i.imgur.com/ZEZLSOo.png",
draggable: 2,
position: 12,
alt: "Norway",
},
{
id: 1,
dataid: "b",
type: "red",
src: "https://i.imgur.com/ZEZLSOo.png",
draggable: 2,
position: 12,
alt: "Norway",
},
{
id: 2,
dataid: "a",
type: "black",
src: "https://i.imgur.com/dL3J8XS.jpeg",
draggable: 2,
position: 13,
alt: "Norway",
},
{
id: 3,
dataid: "b",
type: "black",
src: "https://i.imgur.com/dL3J8XS.jpeg",
draggable: 2,
position: 13,
alt: "Norway",
}
]
const itemHTML = (item) => {
return `
<span
class="event"
data-id="${ item.dataid }"
draggable="${ !!item.draggable }"
>
${ item.dataid } <img
data-imgid="${ item.id }"
src="${ item.src }"
class="b"
alt="${ item.alt }"
style="width: 30%;"
/>
</span>
`
}
// redrawing the table based on item attributes
const updateTable = (table, items) => {
$(table).find('td').each(function(i, e) {
let html = ''
const filtered = items.filter(({
position
}) => position - 1 === i)
if (filtered.length) {
filtered.forEach(item => {
html += itemHTML(item)
})
} else {
html = !$(e).parent().index() ? i + 1 : 24 - $(e).index()
}
e.innerHTML = html
})
}
updateTable('#targetTable tbody', items)
const dragrowHTML = ({
id,
to
}) => {
return `<tr><td>${ id }</td><td>${ to }</td></tr>`
}
$(function() {
initDragAndDrop();
});
function clearDragAndDrop() {
$('.event').off();
$('#targetTable td').off('dragenter dragover drop');
}
function initDragAndDrop() {
$('.event').on('dragstart', function(event) {
const itemid = event.target.getAttribute('data-imgid')
var dt = event.originalEvent.dataTransfer;
//add dat-attr and class from where to remove
dt.setData('id', itemid);
});
$('.event').on('dragend', function(event) {
clearDragAndDrop();
initDragAndDrop();
});
// updated event handling
$('#targetTable td').on('dragenter dragover drop', function(event) {
event.preventDefault();
if (event.type === 'drop') {
const itemid = event.originalEvent.dataTransfer.getData('id');
const item = items.find(({
id
}) => id == itemid)
// if item is draggable
if (item && item.draggable) {
// calculate position
item.position = ($(event.target).parent().index() * $(event.target).parent().children().length) + ($(event.target).index() + 1)
// update table
updateTable('#targetTable tbody', items)
// update item's draggable property
$('#dragtable tbody').append(dragrowHTML({
id: item.id,
to: item.position
}))
item.draggable--
}
}
});
}
html,
body {
margin: 0;
}
table {
border-collapse: collapse;
}
td span {
display: flex;
align-items: center;
justify-content: center;
background-color: #09C;
font-weight: bold;
}
img {
padding: 10px;
}
#dragtable {
border-collapse: collapse;
}
#dragtable th,
#dragtable td {
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="targetTable" class="b" border="10" bordercolor="blue" id="myT">
<tbody>
<tr>
<td> 1</td>
<td> 2</td>
<td> 3</td>
<td> 4</td>
<td> 5</td>
<td> 6</td>
<td> 7</td>
<td> 8</td>
<td> 9</td>
<td> 10</td>
<td> 11</td>
<td> 12</td>
</tr>
<tr>
<td> 24</td>
<td> 23</td>
<td> 22</td>
<td> 21</td>
<td> 20</td>
<td> 19</td>
<td> 18</td>
<td> 17</td>
<td> 16</td>
<td> 15</td>
<td> 14</td>
<td> 13</td>
</tr>
</tbody>
</table>
<table id="dragtable">
<thead>
<tr>
<td>ID</td>
<td>TO</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.