Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ui draggable + sortable helper style

I am using jquery ui plugin to realize a drag and drop linked to a sortable list element. When I move the draggable element, a helper is created with a custom width and height. When my element is above the sortable area, an inline style is created and affects my custom width and height. The helper no longer has the right dimensions and takes 100% of the width of the sortable zone. You can see an example on the jquery ui example here http://jqueryui.com/draggable/ # sortable My goal is to prevent the insertion of inline style for the height and width of the helper. Is that possible?

I have try the sortable forcehelpersize parameter but with no success.

edit : I noticed that when I'm over the sortable area, the helper takes the dimensions of the initial element draggable.

like image 407
Arnaud Ncy Avatar asked Mar 05 '13 17:03

Arnaud Ncy


1 Answers

Unfortunately, the ui.sortable code that sets the width of your helper does not consider a width on the class as specified. I'm using jQuery 1.8.x and stumbled into this same behaviour. Looking at the source code, I saw that ui.sortable is checking if the width style is specified on the element and if not, will set it to the width of the first element in the sortable. My solution was to explicitly set the width and height on the helper using the function form of the draggable helper option.

$('#draggable').draggable({
    helper: function() {
        var helper = $(this).clone(); // Untested - I create my helper using other means...
        // jquery.ui.sortable will override width of class unless we set the style explicitly.
        helper.css({'width': '462px', 'height': '300px'});
        return helper;
    }
});

Those values don't have to be hard coded. You could copy them from another element, for example. But they do need to be set on the helper element itself (not inherited).

like image 175
Tony the Tech Avatar answered Sep 22 '22 21:09

Tony the Tech