0

Hey guys i am using Django templeting system and bootstrap for a project where i have a reuirement of jquery image drag and drop. I dont have any knowledge of jquery or javascript. So far i have implemented the image drag and drop but now i want to add a class on drop event.

Here in my requirement i have a image cut out in five pieces. Each pieces are stored in one container. user can drag a piece to drop it in another container to complete the image. but when i am droping it in another container all the images are aligned vertically instead of geeting joined to previous piece.

so i have decided to add a class on drop event which can add style="position:absolute; property to image.

I hope u have understood my problem. i know this is a nub question but please help we with code

Here i have added it on jsfiddle http://jsfiddle.net/binit/JnYB9/ So that you can play around help me

code


<script>
$(function() {

$( "#sortable1" ).sortable({
connectWith: "div"
});

$( "#sortable2" ).sortable({
connectWith: "div"

 });

$( "#sortable1, #sortable2" ).disableSelection();
 });
</script>

{% endblock headercontent %}


{% block content %}

<div class="row-fluid" >
    <div class="span4">
        <div class="span1"></div>
        <div id="sortable1" class="well sidebar-nav sidebar-nav-fixed span11">
            <legend>Collect Coupon parts</legend>
            1st part
            <img class="ui-state-highlight" src="{% static 'images/demo/north.png' %}" >
            2nd part
            <img class="ui-state-highlight" src="{% static 'images/demo/south.png' %}" >
            3rd part
            <img class="ui-state-highlight" src="{% static 'images/demo/east.png' %}" >
            4th part
            <img class="ui-state-highlight" src="{% static 'images/demo/west.png' %}" >
            5th part
            <img class="ui-state-highlight" src="{% static 'images/demo/center.png' %}"  >
        </div>
    </div>
    <div id="sortable2" class=" well sidebar-nav sidebar-nav-fixed span8">

        <legend>Canvas section</legend>
     </div>
</div>
Binit Singh
  • 973
  • 4
  • 14
  • 35

1 Answers1

1

Several points:

  • When you're asking a client-side Javascript question, it would be best not to muddy the waters with your server-side markup. So non-HTML templates like this should probably either be expanded (from, say, View Source) or removed:

     {% extends "dashboard.html" %}
    
     {% block headercontent %}
    
  • Your code already has an example of adding a class on drop (at least if I'm guessing correctly about the jQuery drag-and-drop; it's been a long time) in this block.

    drop: function( event, ui ) {
        $( this )
            .addClass( "fullbowl" )
            ^^^^^^^^^^^^^^^^^^^^^^^^
            .find( "div" )
            .html( "" );
    

    So you can either duplicate this if it's a different group of sortables you want to style and add a different class, add a second addClass('something') call to this one -- which would be fine -- or change the CSS for fullbowl to include your new properties. Lots of options.

Update

With your new Fiddle, it's easier to test, and it becomes straightforward to notice that you're not using an event of JQuery UI's sortable API, but one from droppable. If you used stop, from sortable, it works fine:

$( "#sortable2" ).sortable({
    connectWith: "div",
    stop: function( event, ui ) {
        ui.item.addClass( "fullbowl" )
    }
});

You can see it in action on my fork of your Fiddle.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • Actually its not working. I have edited the question as you said – Binit Singh Jan 21 '14 at 12:13
  • What's not working; the addClass call? And I still see what I assume are Django templates in there... :-) – Scott Sauyet Jan 21 '14 at 12:19
  • In the **[API documentation](http://api.jqueryui.com/droppable/#event-drop) for the `drop` event, there is no mention of what `this` is bound to. But the second parameter, `ui`, has a reference to the draggable object, so perhaps instead of `$(this).addClass("fullbowl")...` you could do `ui.draggable.addClass("fullbowl")...` – Scott Sauyet Jan 21 '14 at 12:27
  • can u edit my current code because any how that thing dose not work so i have removed that completely. – Binit Singh Jan 21 '14 at 12:38
  • No, but I'd suggest you post a JSFiddle, JSBin, Plunkr, codepen, or other sandboxed version to show the issue you're having. People might be able to help if they could play around with it. – Scott Sauyet Jan 21 '14 at 12:47
  • If i have that few lines in my code my drag and drop is also not working that why i have removed that – Binit Singh Jan 21 '14 at 12:50
  • I have added the JSFiddle link in my question – Binit Singh Jan 21 '14 at 13:30
  • what changes i need to do if i want to add different class on drop of different image by assiging id to each image – Binit Singh Jan 22 '14 at 06:05