kevzettler
kevzettler

Reputation: 5213

how to find selected elements with Jquery UI selectable

I am looking for info on the event and ui objects the jQuery selectable events: "selecting", and "start" take as parameters. I cannot find this in the documentation and looping through the properties is no help.

$('#content_td_account').selectable({
    filter: 'li:not(".non_draggable")',
    selecting: function(event, ui) { 
    }
});

Specifically I want to find what elements are being selected and check them to see if their parent elements are the same or not. I assumed this would be in the ui object some where.

Upvotes: 16

Views: 40202

Answers (5)

Javier Pino
Javier Pino

Reputation: 11

By using :last or :first the selection is not the first selected but the first in order of li

This is the solution I managed:

HTML

<ol class="selectable">                            
     <li class="ui-widget-content" 
        id="selectable_1"
        value="1"
     > First Selectable </li>
     <li class="ui-widget-content" 
        id="selectable_2"
        value="2"
     > Second Selectable </li>
</ol>  

In this code I gave the li an id and a value, thus making them available to the ui property of the selecting event

JAVASCRIPT

//A place to put the last one
var last_selected_value;
var last_selected_id;


$( ".selectable" ).selectable({
    selecting: function(event, ui) {

        //In the selection event we can know wich is the last one, by getting the id on
        //the ui.selecting.property

        last_selected_value = ui.selecting.value;
        last_selected_id = ui.selecting.id;
    },
    stop: function(event, ui) {

        //Here the event ends, so that we can remove the selected
        // class to all but the one we want

        $(event.target).children('.ui-selected').not("#" + last_selected_id)
            .removeClass('ui-selected');

        //We can also put it on a input hidden
        $( "#roles_hidden" ).val(last_selected_value);
    }
});

Upvotes: 1

broom
broom

Reputation: 11

the ui argument in that event is the reference to the elemenent, if it was the selected event ui.selected event will be the elemennt, if it was unselect unselected.ui.....etc

Upvotes: -1

Lukspa
Lukspa

Reputation: 121

You have to use selected and unselected event which are fired for every selected item in group selection.

var selected = new Array();
$("#selectable").selectable({
    selected: function(event, ui){            
        selected.push(ui.selected.id);
    },
    unselected: function(event, ui){
        //ui.unselected.id
    }
});

Upvotes: 12

dez
dez

Reputation: 2265

this will solve your problem:

    <script type="text/javascript">
    $(function() {
        $("#selectable").selectable({
            stop: function(){
                var result = $("#select-result").empty();
                $(".ui-selected", this).each(function(){
                    var index = $("#selectable li").index(this);
                    result.append(" #" + (index + 1));
                });
            }
        });
    });
    </script>


<div class="demo">

<p id="feedback">
You've selected: <span id="select-result">none</span>.
</p>

<ol id="selectable">
    <li class="ui-widget-content">Item 1</li>
    <li class="ui-widget-content">Item 2</li>
    <li class="ui-widget-content">Item 3</li>
    <li class="ui-widget-content">Item 4</li>
    <li class="ui-widget-content">Item 5</li>
    <li class="ui-widget-content">Item 6</li>
</ol>

check out http://jqueryui.com/demos/selectable/#serialize for more info

Upvotes: 0

Andy Gaskell
Andy Gaskell

Reputation: 31761

When an element is selected it gets the ui-selected class added.

So you could get all selected elements with $(".ui-selected")

This might not work exactly but I think the idea would be something like this:

$('#content_td_account').selectable({
  filter: 'li:not(".non_draggable")',
  selecting: function(event, ui) { 
    var p = $(this).parent();
    $(".ui-selected").each(obj, function() {
      if(obj.parent() == p) {
        // get rad
      }
    });
  }
});

Upvotes: 12

Related Questions