views:

24

answers:

1

Hi Experts,

I am trying to make a drag drop stage using Jquery, I am having problem that I want only a single item inside the droppable box. But right now, it is adding multiple items inside it. Here is my code.

$("#item_list_2" ).droppable({
        drop:
                function(e, ui)
                {
                    var param = $(ui.draggable).attr('src');
                    var div_id = '#'+ $(this).attr("id");

                    addlist(param,div_id);
                }
});

Where addlist() is a function which is creating item contents using ajax. I there any way that I can restrict my #item_list_2 to get only one droppable object.

Thanks

A: 

You can implement the accept option in droppable to disable it when you have an element already dropped (you have to implement that condition depending on your case):

$("#item_list_2" ).droppable({
        accept: function() {
            return elementsAlreadyDropped == 0;   //<-- Implement for your concrete case
        },
        drop:
                function(e, ui)
                {
                    var param = $(ui.draggable).attr('src');
                    var div_id = '#'+ $(this).attr("id");

                    addlist(param,div_id);
                }
});
gustavogb
You have to put there a condition checking if there was a previous element dropped in that droppable. The implementation depends on your case. If you can share the addlist funcion implementation I can try to help you with a concrete implementation.
gustavogb
Thanks gustavogb for your help. One thing is not understandable for me where it says return elementsAlreadyDropped == 0; Can you please explain a bit more about "elementsAlreadyDropped" and how I will check if the object is already dropped. Actually I have total 8 items to be dropped in more than 20 boxes.Many Thanks
kakaajee
function addlist(param,div_id){ $.ajax({ type: "POST", url: "ajax/addtobox.php", data: 'img='+encodeURIComponent(param), dataType: 'json', beforeSend: function(x){$('#ajax-loader').css('visibility','visible');}, success: function(msg){ $('#ajax-loader').css('visibility','hidden'); if(parseInt(msg.status)!=1) { return false; } else { $(div_id).append(msg.txt); } }); }}I don't know why it is not displaying in code format in comments
kakaajee