jQuery UI Selectable - unselect selected item on click
Asked Answered
V

4

15

Does anyone know if there's a way to configure a jquery ui selectable element to unselect the selected element when you click it? Sort of like a toggle. If it's already selected, unselect it, otherwise do the default behavior.

Thanks.

Vair answered 15/10, 2010 at 21:58 Comment(0)
E
42

i'm very late in responding to your question, but let me just answer it anyway so that to keep it as a reference for others.

$( ".selector" ).bind( "mousedown", function ( e ) {
    e.metaKey = true;
} ).selectable();

this will allow the toggle behavior that you're looking for.

Eloquence answered 25/9, 2012 at 22:28 Comment(2)
i see it is working as expected but honestly, i dont understand how! can you please explain why this code is working (i mean how it is unselecting on second click?)Amputee
This just adds the metaKey (Ctrl) to the "mousedown" event, which forces the same behaviour as if you would have pressed Ctrl yourself.Terbia
V
11

Well here's what I just ended up doing. I used a class name to toggle selecting and unselecting. I'd love to hear if there is another option:

$("#selectable").selectable({
    selected: function (event, ui) {
        if ($(ui.selected).hasClass('selectedfilter')) {
            $(ui.selected).removeClass('selectedfilter');
            // do unselected stuff
        } else {            
            $(ui.selected).addClass('selectedfilter');
            // do selected stuff
        }
    },
    unselected: function (event, ui) {
        $(ui.unselected).removeClass('selectedfilter');
    }
});
Vair answered 15/10, 2010 at 22:40 Comment(3)
Ahh ok now I get what you ment! sorry!Illuminator
No problem @Trufa. I'm sure I didn't explain it very well. Thanks for trying to help.Vair
Note that in order to switch a class name, you can simply use 'toggleClass("selectedFilter")'. No need to check if an element has a class. ToggleClass does that for you.Landa
Q
2

If you want that existing selections be preserved and yet have the toggle operation, you simply need to ignore the unselected event for the solution given. Also you need to remove the ui-selected class.

$("#selectable").selectable({
selected: function (event, ui) {
    if ($(ui.selected).hasClass('selectedfilter')) {
        $(ui.selected).removeClass('selectedfilter').removeClass('ui-selected');
        // do unselected stuff
    } else {            
        $(ui.selected).addClass('selectedfilter').addClass('ui-selected');
        // do selected stuff
    }
}
});
Quieten answered 23/9, 2011 at 15:12 Comment(0)
I
0

Is this what you mean?

This event is triggered at the end of the select operation, on each element removed from the selection.

Code examples

Supply a callback function to handle the unselected event as an init option.

$( ".selector" ).selectable({
   unselected: function(event, ui) { ... }
});
Bind to the unselected event by type: selectableunselected.
$( ".selector" ).bind( "selectableunselected", function(event, ui) {
  ...
});

Source:

http://jqueryui.com/demos/selectable/#event-unselected

Illuminator answered 15/10, 2010 at 22:4 Comment(1)
Well what I need is to trigger this unselected event when a selected element is clicked. Right now, this event only fires when I click another selectable element.Vair

© 2022 - 2024 — McMap. All rights reserved.