1

What I'm trying to do

I was here but the problem was different to what I first thought.

I'm using a CMS that has set options for the sortable group already and I don't want to change them. What I do want to do is exclude a <div> with the class of "not-sortable" from being sortable.

Current settings included in the CMS

    $('.meta-box-sortables').sortable({
        placeholder: 'sortable-placeholder',
        connectWith: '.meta-box-sortables',
        items: '.postbox',
        handle: '.hndle',
        cursor: 'move',
        delay: ( isMobile ? 200 : 0 ),
        distance: 2,
        tolerance: 'pointer',
        forcePlaceholderSize: true,
        helper: 'clone',
        opacity: 0.65,
    });

As stated above, I've set one of the 'postbox' sections with an additional class of 'not-sortable'. Then in a custom .js file I have the following.

My custom settings

jQuery(function($) {
    $(".meta-box-sortables").sortable({
        items : ':not(.not-sortable)'
    });
});

This then removes the ability to move any previously draggable sections. Therefore it seems as though I'm overriding the items: option.

Is there a way I can do this without overriding the original settings, or am I wrong in my train of thought here?

Community
  • 1
  • 1
JohnMcCarthy
  • 133
  • 2
  • 2
  • 10

1 Answers1

0

You can use the form of the option method that takes an object:

jQuery(function($) {
    $(".meta-box-sortables").sortable("option", {
        items: ":not(.not-sortable)"
    });
});

This way, the settings you don't specify will be preserved.

The elaborate upon the selector previously stored in the item option, you unfortunately have to specify it again:

items: ".postbox:not(.not-sortable)"

Concatenating to the previous value is feasible:

items: $(".meta-box-sortables").sortable("option", "items")
    + ":not(.not-sortable)"

It will, however, fail if items contains a more complex selector (such as a multiple selector):

items: ".postbox, .other",  // Whoops.
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Thanks @Frédéric Hamidi. I don't think I wrote the question very well. I'd like to keep the item: settings already set but also add the setting of item: not: – JohnMcCarthy Jun 07 '15 at 16:55
  • 1
    Thanks for the updated answer. It confirmed that I was on the right track. After trying both, "specifying again" and then the "concatenating" option, each one resulted in none of the fields being draggable and other functionality was also lost. I think the question may be leaning towards being CMS specific, I wanted to ensure it wasn't my understanding of jQuery getting in the way first. – JohnMcCarthy Jun 07 '15 at 20:07