代码之家  ›  专栏  ›  技术社区  ›  Svish

如何使用jqueryui以编程方式选择selectables?

  •  25
  • Svish  · 技术社区  · 15 年前

    我有一系列可选择的项目。我想添加一个按钮的地方,激活其中的预设选择。有办法吗?

    更多信息: 我谈论的事件是 their api 继续 their demo page

    • 挑选出来的
    • 停止
    • 取消选择

    6 回复  |  直到 13 年前
        1
  •  28
  •   Homer    12 年前

    下面是Alex R的代码的一个变体,它使用多个元素

    http://jsfiddle.net/XYJEN/1/

    function SelectSelectableElements (selectableContainer, elementsToSelect)
    {
        // add unselecting class to all elements in the styleboard canvas except the ones to select
        $(".ui-selected", selectableContainer).not(elementsToSelect).removeClass("ui-selected").addClass("ui-unselecting");
    
        // add ui-selecting class to the elements to select
        $(elementsToSelect).not(".ui-selected").addClass("ui-selecting");
    
        // trigger the mouse stop event (this will select all .ui-selecting elements, and deselect all .ui-unselecting elements)
        selectableContainer.data("selectable")._mouseStop(null);
    }
    

    更新:

    jQueryUI 1.10,根据kmk的评论: http://jsfiddle.net/XYJEN/163/

        2
  •  14
  •   Alex R    10 年前

    http://jqueryui.com/demos/selectable/

    <style>
        #feedback { font-size: 1.4em; }
        #selectable .ui-selecting { background: #FECA40; }
        #selectable .ui-selected { background: #F39814; color: white; }
        #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
        #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
        </style>
        <script>
        $(function() {
            $( "#selectable" ).selectable();
        });
        </script>
    
    
    
    <div class="demo">
    
    <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>
        <li class="ui-widget-content">Item 7</li>
    </ol>
    
    </div><!-- End demo -->
    

    您可以使用如下函数:

        function selectSelectableElement (selectableContainer, elementToSelect)
        {
            // add unselecting class to all elements in the styleboard canvas except current one
            jQuery("li", selectableContainer).each(function() {
            if (this != elementToSelect[0])
                jQuery(this).removeClass("ui-selected").addClass("ui-unselecting");
            });
    
            // add ui-selecting class to the element to select
            elementToSelect.addClass("ui-selecting");
    
            selectableContainer.selectable('refresh');
            // trigger the mouse stop event (this will select all .ui-selecting elements, and deselect all .ui-unselecting elements)
            selectableContainer.data("selectable")._mouseStop(null);
        }
    

    // select the fourth item
    selectSelectableElement (jQuery("#selectable"), jQuery("#selectable").children(":eq(3)"));
    

    可以对其进行改进,以允许选择元素集合,但这是一个开始。

        3
  •  4
  •   gblazex    15 年前

    给你

    ,calc: function() { this._mouseStop(); },
    custom: function(guys) {
      var self = this;
      self.selectees.removeClass("ui-selected");
      guys.each(function(){
        $(this).addClass("ui-selecting");
        self._trigger("selecting", {}, {
           selecting: this
        });
      });
      this.calc(); // do the selection + trigger selected
    } 
    

    在后面加上这个 _mouseStop 在里面 selectable.js 然后你就可以 :

    $("#selectable").selectable("custom", $("#selectable :first-child"));
    

    ... 或者你喜欢的任何东西。

    乐趣 ! :)

        4
  •  2
  •   Michael Lumbroso    15 年前

    编辑:抱歉误解,我正在编辑我的答案。

    因此,是的,对象的选择可能对应于所选的类ui,因此您可以做的是:

    $(#button).click(function(){
      $("#element1").addClass("ui-selected");
    
      .......
    
    });
    
        5
  •  0
  •   Bruno    15 年前

    selected 使用手动事件 .trigger('selected') ?

        6
  •  0
  •   Antonio Louro    15 年前

    使用Ionut代码,如何:

     $("#selectable li:first").trigger('start').trigger('selecting').trigger('selected'); 
    

    ?