Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I have created a web page with drag and drop features for some of the elements.

Once the drag and drop are done, I store the elements which are in the droppable area in the local storage of the browser.

Later when the page is accessed again, I take the values from local storage and restore them on the web page.

After I restore, I couldn't drag the elements in the droppable area within its containment. Could anyone please help? Below is the code I have used.


Here is the fiddle link of my work:

FIDDLE

What I have tried:

JS:

JavaScript
$(function () {
        if (localStorage.length != 0) {
            for (var i = 0; i < localStorage.length; i++) {
                var dropClass = localStorage.key(i);
                var clonediv = $("." + dropClass.substr(4, dropClass.length - 4)).clone();
                $("#droppable").append(clonediv.draggable({ containment: "#droppable", grid: [30, 30] }).resizable({ containment: "#droppable" }));
                console.log(clonediv);
            }
        }
        else { }

        $(".bat_voltage").draggable({ helper: 'clone', snap: true, grid: [30,30] });
        $(".left_flight").draggable({ helper: 'clone', snap: true, grid: [30, 30] });
        $(".cnt_flight").draggable({ helper: 'clone', snap: true, grid: [30, 30] });
        $("#droppable").droppable({
            drop: function (event, ui) {
                var dragged = ui.draggable;
                var id = dragged.attr("class").split(' ');
                var droppable = $("#droppable");
                var find = (droppable.find("." + id[0]));
                console.log(find);
                if (find.length != 0) { }
                else {
                    localStorage.setItem("drop" + id[0], droppable);

                    ui.helper.css({ 'top': 0, 'left': 0, 'position': 'relative', 'margin': '0px auto', 'padding': '0.5em', 'z-index': 0 });
                    ui.helper.clone().appendTo("#droppable").draggable({ snap: true, grid: [30, 30] }).resizable({ containment: "#droppable" });
                }
            }
        });
      });



CSS:

.bat_voltage { width: 250px; height: 100px; padding: 0.5em; margin: 10px 10px 10px 0; z-index:1; }
.floatleft { float:left; }
.left_flight { width: 250px; height: 100px; padding: 0.5em; margin: 10px 10px 10px 0; z-index:1; }
.cnt_flight { width: 250px; height: 100px; padding: 0.5em; margin: 10px 10px 10px 0; z-index:1; }
#droppable { width: 50%; height: 400px; padding: 0.5em; margin: 10px; resize:both; border: 2px; overflow:auto; }

HTML:

HTML
<div>
    <div>
    <div class="bat_voltage floatleft ui-widget-content" >
      <p>Battery Voltage</p>
    </div>

    <div class="left_flight floatleft ui-widget-content" >
      <p>Flight Time Left</p>
    </div>

    <div class="cnt_flight floatleft ui-widget-content" >
      <p>Current Flight Time</p>
        <div class="curFlight"></div>
    </div>
    <div style="clear:both"></div>
   </div>
    <div id="droppable" class="ui-widget-header">
      <p>Drop here</p>
    </div>

</div>
Posted
Updated 25-Sep-21 8:55am
v6
Comments
ZurdoDev 10-Nov-16 7:10am    
Debug your code. After you load from localStorage is the draggable method being called against the items?
Giridharan_BE 10-Nov-16 21:05pm    
I can drag after restoring from localstorage. But cannot drop in another place within its containment also.
ZurdoDev 11-Nov-16 6:46am    
Make sure droppable is getting wired up properly.
Giridharan_BE 13-Nov-16 9:11am    
Sorry. What you mean wired up properly? Could you please elaborate?
ZurdoDev 13-Nov-16 19:06pm    
Check $("#droppable").length() to make sure it is > 0.

1 solution

C#
It must be somehow caused by the element not being dragged into the droppable area, making the revert parameter active because of this. Not sure how to solve this better than by just disabling it for the elements present inside it at the start:



$('#droppable .ui-draggable').draggable( "option", "revert", false );


FIDDLE[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900