Pedro Bernardo Pedro Bernardo - 2 years ago 186
jQuery Question

jQuery sortable cancel event if not valid

I have a list which is sortable. Before I start sorting, I want to check if all the elements of that list are valid. If not, cancel the event and leave the list intact.

You can find the code here

When I use this, the event is canceled, but the element is removed.

start: function (event, ui) {
if (!valid()) {
return false;
// it cancel's but the element is removed...

Maybe I should implement a "beforeStart" event? Suggestions?

Answer Source

You can use the cancel method

    connectWith: ".connectedSortable",
    items: '.sortable-item',
    handle: '.handle',
    placeholder: "ui-state-highlight",
    stop: function (event, ui) {
        if (!valid()) {
            $( this ).sortable( "cancel" );

Demo: Fiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download