how to find selected elements with Jquery UI selectable

前端 未结 5 1223
说谎
说谎 2020-12-31 06:09

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

相关标签:
5条回答
  • 2020-12-31 06:42

    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

    0 讨论(0)
  • 2020-12-31 06:44

    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
        }
    });
    
    0 讨论(0)
  • 2020-12-31 06:59

    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);
        }
    });
    
    0 讨论(0)
  • 2020-12-31 07:01

    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
          }
        });
      }
    });
    
    0 讨论(0)
  • 2020-12-31 07:07

    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

    0 讨论(0)
提交回复
热议问题