javascript - (re)store position and size of EACH cloned draggable JQuery -
i have div (canvas), acts droppable rects. rects, dropped on div, cloned , can dragged , resized within div.
my questions is: how can (re)store position, size of dynamically cloned elements?
how works: drag more 1 rect onto canvas resize or drag within rect click save
by now, gives me correct number of cloned rects, saves position , size of last cloned element.
how can add hidden textfields each cloned rect separately?
$(function() { $('#rect').draggable({ revert: "invalid", helper: function(event, ui) { return $(this).clone(true); } }); $('#bu').click(function() { alert("no of rect set: " + $('.rectset').length); $('.rectset').each(function() { var postop = $('input#postop').val(); var posleft = $('input#posleft').val(); var height = $('input#sizeheight').val(); var width = $('input#sizewidth').val(); alert("left: " + posleft + ", top: " + postop + " ,height: " + height + ", width: " + width); }); }); $("#canvas").droppable({ accept: "#rect", drop: function(e, ui) { if ($(ui.draggable).hasclass('ui-draggable-dragging')) { /*alert("rect dragged , not copied again");*/ return } var droppedrect = $(ui.draggable).clone().addclass('rectset') droppedrect.append('<input type="hidden" id="posleft" value="empty"></input>'); droppedrect.append('<input type="hidden" id="postop" value="empty"></input>'); droppedrect.append('<input type="hidden" id="sizewidth" value="empty"></input>'); droppedrect.append('<input type="hidden" id="sizeheight" value="empty"></input>'); droppedrect.appendto(this); droppedrect.draggable({ containment: "#canvas", scroll: false, stop: function(event, ui) { // alert($('input#posleft').val() + " " + $('input#postop').val()) ; var posleft = ui.position.left; var postop = ui.position.top; $('input#posleft').attr('value', posleft); $('input#postop').attr('value', postop); alert($('input#posleft').val() + " " + $('input#postop').val()); } }).resizable({ ghost: true, containment: "#canvas", stop: function(event, ui) { $('#size').attr('value', ui.size) var width = ui.size.width; var height = ui.size.height; // alert($('input#sizewidth').val() + " " + $('input#sizeheight').val()) ; $('input#sizewidth').attr('value', width); $('input#sizeheight').attr('value', height); alert($('input#sizewidth').val() + " " + $('input#sizeheight').val()); } }); } }); });
#canvas { width: 700px; height: 400px; border: 10px solid black; padding: 15px 15px 15px 150px; } #rect { border: 3px solid black; background: #ffff99; width: 100px; height: 100px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.1/themes/base/jquery-ui.css" rel="stylesheet" /> <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script> <button id="bu" onclick="save()">save</button > <div id="rect" class="ui-widget-content"> </div> <div id="canvas" class="ui-widget-header">
like mike said, ids need unique. solved adding counter var = 0
, on each new droppedrect
setting id
attribute number incremented. also, think better way achieve same goal of putting data onto rectangles through jquery.data. used jqueryui 1.9 because being used in fiddle.
$(function() { $('#rect').draggable({ revert: "invalid", helper: function(event, ui) { return $(this).clone(true); } }); var = 0; $('#bu').click(function() { console.log("no of rect set: " + $('.rectset').length); $('.rectset').each(function(a, b) { console.log("left: " + $(b).data('posleft') + ", top: " + $(b).data('postop') + " ,height: " + $(b).data('height') + ", width: " + $(b).data('width')) }); }); $("#canvas").droppable({ accept: "#rect", drop: function(e, ui) { if ($(ui.draggable).hasclass('ui-draggable-dragging')) { return; } var droppedrect = $(ui.draggable).clone().addclass('rectset').attr('id', i++) .appendto(this) .data({ 'posleft': ui.position.left, 'postop': ui.position.top, 'width': ui.draggable[0].offsetwidth, 'height': ui.draggable[0].offsetheight }); console.log("dropped - left: " + ui.position.left + " top:" + ui.position.top + " width: " + ui.draggable[0].offsetwidth + " height: " + ui.draggable[0].offsetheight); droppedrect.draggable({ containment: "#canvas", scroll: false, stop: function(event, ui) { $(this).data('posleft', ui.position.left); $(this).data('postop', ui.position.top); console.log("moved - left: " + ui.position.left + " top:" + ui.position.top); } }).resizable({ ghost: true, containment: "#canvas", stop: function(event, ui) { $(this).data('width', ui.size.width); $(this).data('height', ui.size.height); console.log("resized - width: " + ui.size.width + " height: " + ui.size.height); } }); } }); });
#canvas { width: 700px; height: 400px; border: 10px solid black; padding: 15px 15px 15px 150px; } .rect { border: 3px solid black; background: #ffff99!important; width: 100px; height: 100px; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.1/themes/base/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script> <button id="bu">save</button > <div id="rect" class="rect ui-widget-content"> </div> <div id="canvas" class="ui-widget-header">
Comments
Post a Comment