views:

530

answers:

2

Hi,

I'm working with jQuery's UI sortables and have created a system of divs in this format;

<div class='mainDiv'>
    <label>text: <input type='text' name='textbox' /></label>

    <div class='children'>
        <div class='mainDiv'>...</div>
        <div class='mainDiv'>...</div>
        <div class='mainDiv'>
            <label>text: <input type='text' name='textbox' /></label>
            <div class='children'>...</div>
        </div>
    </div>
</div>

The divs can have an infinite number of children, and I want to be able to drag and drop all of the .mainDiv's within their parent box, so I have used this:

$(".mainDiv").parent().sortable({items: ".mainDiv", containment: "parent"});

However, this allows elements to be dragged into their child or sibling .children divs. which I do not want to happen. I want to restrict elements to staying within their parent.

Any help here would be hot.

Cheers

+2  A: 

I managed to solve it like this:

$(this).parent().sortable({
    items: '> li',
    axis: 'y',
    ...
});

In your case, it should work with:

items: '> .mainDiv'

containment: "parent" is not required.

In my case, I enable "sortable" during a previous click event, as it is faster. I think its easier if you bind it to the "mousedown" event. If you create the tree dynamically (e.g.Ajax), I recommend you to use "livequery".

lepe
Thanks man, 6 months later and I can finally fix this :) Luckily the project wasn't vital! Thanks a lot.
Joel
I'm glad it was still useful for you. Have a nice day.
lepe
@Joel consider accepting the answer by clicking on the arrow to the left of the main question
methodin
A: 

Hi, lepe

will you please explain it by giving full example, I am beginner to jquery ui

Anil