I got some problem with my code but I simply cannot put my finger on what would be at fault in that situation I am lost.
So the situation is as follows, I am making a simple drag and drop interface by using jquery, all it does is: Make a grid, add/remove “pawns” depending on a certain table in the same page and drag&drop whatever pawn you want in whatever square you wish to. Everything works as expected but almost always when I try to drop the pawn on a square it teleports in a random position, rarely it does drop where it is expected to. I am not sure it is relevant but I am using laravel 10 and this is a blade file, Any suggestions I could try?
<div class="col-md-3 gridframe">
<h3 class="text-center">Character Grid</h3>
<div id="grid" class="row mx-auto">
</div>
</div>
<style>
.gridframe {
width: 300px;
border: 1px solid black;
overflow: hidden;
}
.grid-cell {
width: 50px;
height: 50px;
border: 1px solid black;
text-align: center;
position: relative;
float: left;
}
.character {
width: 30px;
height: 30px;
background-color: black;
color: white;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
z-index: 999;
position: absolute;
top: 10px;
left: 10px;
}
</style>
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script>
function insertCharacter(nickname, hp) {
var cells = $('.grid-cell');
cells.each(function() {
if (!$(this).children('.character').length) {
var row = $(this).data('row');
var col = $(this).data('col');
$(this).append('<div class="character" data-hp="' + hp + '">' + nickname + '<br>' + hp + '</div>');
return false;
}
});
// make characters draggable
$('.character').draggable({
revert: 'invalid',
zIndex: 100,
start: function(event, ui) {
$(this).css('cursor', 'pointer');
},
stop: function(event, ui) {
$(this).css('cursor', 'auto');
}
});
// make grid cells droppable
$('.grid-cell').droppable({
accept: '.character',
drop: function(event, ui) {
var character = ui.draggable;
// var characterHP = character.data('hp');
character.appendTo($(this));
}
});
}
$(document).ready(function() {
$('#character-table tbody').on('DOMNodeInserted', 'tr', function() {
var nickname = $(this).find('td:eq(0)').text();
var hp = $(this).find('td:eq(1)').text();
insertCharacter(nickname, hp);
});
$('#character-table tbody').on('DOMNodeRemoved', 'tr', function() {
var nickname = $(this).find('td:eq(0)').text();
$('.character').each(function() {
if ($(this).text().includes(nickname)) {
$(this).remove();
}
});
});
var gridSize = 10;
var grid = $('#grid');
for (var i = 0; i < gridSize; i++) {
for (var j = 0; j < gridSize; j++) {
$('<div class="grid-cell" data-row="' + i + '" data-col="' + j + '"></div>').appendTo(grid);
}
}
});
</script>```