Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JqueryUI Selectable - unselecting without Ctrl

I am trying to make a selectable list with parent/child/grandchild indentations. Please see below:

http://jsfiddle.net/Lmsop4k7/

$('#theParentList').selectable({
    filter: 'li div',
    selected: function (event, ui) {
        var selectedText = $(ui.selected).text();
        $("#selectedNode").text(selectedText);

        if ($(ui.selected).hasClass('selectedfilter')) {
            $(ui.selected).removeClass('selectedfilter');
        }               
    }
});

But, I am having a lot of problems coming up with the "unselect" functionality (i.e. without having press down Ctrl). I also don't want to "bind" Ctrl automatically to mouse down (which is described in some other solutions), b/c I only want one item selected at one time. Also, I just want to understand how to do the control flow to unselect through the events (e.g. "selected:").

What am I doing wrong here? As you can see, the selection gets picked up correctly since the textbox gets updated correctly with the correct text. However, when I click an already clicked item to "unselect" (without holding down Ctrl), it doesn't unselect. I figure even in this situation, a "selected" event gets triggered - but clearly there is something wrong with my "selected:" code. Very frustrating..

Thanks everyone.

like image 503
Phoeniyx Avatar asked Oct 19 '22 19:10

Phoeniyx


1 Answers

Try

// utilize `dblclick` event to remove selected class
$(".ui-selected").one("dblclick", function(e) {
    $(e.target).removeClass("ui-selected")
});

http://jsfiddle.net/Lmsop4k7/3/

like image 115
guest271314 Avatar answered Oct 22 '22 09:10

guest271314