HI, I want to save the position of dropped items to database[aspx,javascript]. The user can drop any number of item,resize and delete[hide] and finally when they hit 'save' button ,it should be saved to database. I have code to do this in drop /stop but it will save all the dropped items i want to save only at final stage. I guess lot of developer should have already done this,so please suggest some code.
$(function() {
$('#frame img').live('mousemove', function(event) {
$('#frame img').resizable().parent().draggable();
});
});
$(function() {
$('#frame img').live('dblclick', function(event) {
// $(this).resizable("destroy") not working
$(this).hide();
//$(this).unbind("resizable"); not working
//$(this).removeclass(); not working
});
});
$(document).ready(function() {
//Counter
counter = 0;
//Make element draggable
$("img").draggable({
helper: 'clone',
containment: '#frame',
//When first dragged
stop: function(ev, ui) {
var pos = $(ui.helper).offset();
objName = "#clonediv" + counter
$(objName).css({ "left": pos.left, "top": pos.top });
$(objName).removeClass("drag");
//When an existiung object is dragged
$(objName).draggable({
containment: 'parent',
stop: function(ev, ui) {
var pos = $(ui.helper).offset();
}
});
}
});
//Make element droppable
$("#frame").droppable({
drop: function(ev, ui) {
if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {
var pos = $(ui.helper).offset();
counter++;
var element = $(ui.helper).clone();
//var element = element1.resizable();
element.addClass("tempclass");
$(this).append(element);
$(".tempclass").attr("id", "clonediv" + counter);
//$(".tempclass").attr("onclick",function(){ $(this).remove(););
$("#clonediv" + counter).removeClass("tempclass");
//Get the dynamically item id
draggedNumber = ui.helper.attr('id').search(/drag([0-9])/)
itemDragged = "dragged" + RegExp.$1
//console.log(itemDragged)
//alert('left' + pos.left + ',top' + pos.top + 'of item' + itemDragged);
$("#clonediv" + counter).addClass(itemDragged);
}
}
});
//Make the element resizable
});
Please correct me,if anything is wrong. Thanks in advance
Assuming your elements are list items under a list with an id of items
.
var locations = [];
$('#items li').each(function(i) {
locations[i] = {
x: $(this).offset().left,
y: $(this).offset().top
};
});
Then post this to your server side. Without knowing more specifics, I can't be 100% sure if this is all that is required.
On page load, simply loop through the locations and add an attribute to your li
such as (assuming PHP)
<ul id="items">
<?php foreach($items as $item): ?>
<li style="left: <?php echo $item['x']; ?>px; top: <?php echo $item['x']; ?>px">item</li>
<?php endforeach; ?>
</ul>
of course you may also need to do
#items {
position: relative;
}
#items li {
position: absolute;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With