views:

627

answers:

2

I'm creating a day planner using jquery, draggable and droppable.

When a job is dragged from the unassigned column into a hour time slot, the original draggable item is removed, and a new div is placed into page, with the job details.

This newly created div block of code, has all the parameters in it to make it draggable . When it's rendered on the page load, these assigned jobs are draggable .

It's only when they are created on the fly using javascript is something occurring that stops them from being dragged about.

When I create these new div tags on the fly with Javascript, is there something I need to do to tell the jquery that these new items are meant to be dragged?

(thanks in advance as always)

Steps

  • We have a div container with a list of jobs (more divs) inside it. #unassignedjobs is the parent div for these jobs, with the following jobs inside, #jobN ,#jobN2, #jobN3
  • We have another area which we can drop these items into, and inside this area is a container for displaying jobs, per person, lets call these #person1 and #person2.
  • The aim is to drag a job from the unassigned jobs list, and place it into the persons lists of jobs
  • At the minute when I do this, I refresh all of the html inside of #person1 when an item is dropped and using ajax/php to remake the list of jobs for this person (#person1) and repopulate #person1 using innerHTML.
  • Every time a page is created from php any jobs inside #person1 are present when jquery is activated, making them draggable. It's only when the refresh and re-population of the innerHTML is ran are the items (#jobN or #jobN2) unable to be dragged

    var createdContent = httpRequest.responseText;

    jobcolp.innerHTML = createdContent;

Hopefully this makes the problem a little more clearer.

+2  A: 

Rather than replacing the new div, why not disable the draggable?

$(element).draggable('disable');

Then when you want it to be draggable agian, use this

$(element).draggable('enable');
Gausie
The new div is absolute position, sitting inside a relatively positioned div tag.Basically jobs are dragged onto peoples day views. So when a job is dropped it has to be injected into a particular spot in the html.
Matt
Hmm.. I don't think I understand what it is you're trying to do. Could you run through it bullet point style?
Gausie
I've added to the main thread, does this make it clearer? P.S thanks for taking the time.
Matt
Ok. Your current method means you will have to re-apply the drag stuff. I think it would be more wise to change your method, i.e. build the elements within the person rather than refresh the HTML each time. In any case, you can refer to the element after the refresh is applied (by the id you gave it) and give it the same draggable options.
Gausie
+1  A: 

Simply apply draggable to the newly created DIV. If the DIV is exactly the same, except for position, you could also not recreate it, but just reposition it. This would retain all of it's handlers.

 // remove, reposition, reapply handlers
 $('#jobN').remove().appendTo('#dayViewN').draggable();

or

 // reposition, retain handlers
 $('#jobN').appendTo('#dayViewN');
tvanfosson
Brilliant, I'm new to jquery, but let me just see if I've understood this correctly.We've got a div container called #unassignedjobsand inside this container is #jobN which is draggable.I've got another div container #person1 which is where I want #jobN to be moved into.So the above code is simply moving #jobN out of #unassignedjobs and plopping it into the #person1?
Matt
Yep -- it appends it as the last element in the #person1 container.
tvanfosson