简体   繁体   中英

Is it possible to override defined options of the jQuery-ui sortable widget?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM