如何使用 jQuery UI 以编程方式选择可选择项?

发布于 2024-09-07 02:15:20 字数 452 浏览 7 评论 0原文

我有一系列可供选择的项目。我想在某处添加一个按钮来激活其中的预设选择。我有办法做到这一点吗?

我想要的是告诉它“选择这些人”,然后让所有事件正常触发,这样我就不必手动调用所有这些选择事件。

更多信息:我谈论的事件是他们的api 和他们的演示页面

  • 选择
  • 选择
  • 开始
  • 停止
  • 取消选择
  • 取消选择

而且,我认为可能有选择事物时也会设置/清除的数据。所以不仅仅是添加那些css类。

I have a range of items that are selectable. I would like to add a button somewhere that activates a preset selection amongst those. Is there a way I can do that?

What I would like is to tell it to "Select these guys" and then have all the events and all fired as normal, so I don't have to call all of those selection events manually.

More info: The events that I talk about are the ones listed in their api and on their demo page:

  • selected
  • selecting
  • start
  • stop
  • unselected
  • unselecting

And also, I think there might be data that is set/cleared as well when selecting things. So it's not just to add those css classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

渡你暖光 2024-09-14 02:15:20

以下是 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/

Here is a variation of Alex R's code working with multiple elements

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);
}

Update:

jQueryUI 1.10, per comments from kmk: http://jsfiddle.net/XYJEN/163/

攒一口袋星星 2024-09-14 02:15:20

假设 jQuery UI 网站上有可选示例 (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)"));

这可以改进以允许选择元素集合,但它是让你继续前进的起点。

Assuming the selectable sample on the jQuery UI website (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 -->

you can have a function like:

    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);
    }

and use it like:

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

This can be improved to allow selecting a collection of elements, but it's a starting point to get you going.

酷到爆炸 2024-09-14 02:15:20

开始

,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
} 

selectable.js中的_mouseStop之后添加这个,然后你可以

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

...或无论你喜欢什么。

玩得开心! :)

There you go:

,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
} 

Add this after _mouseStop in selectable.js and then you can say:

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

... or whatever you like.

Have fun! :)

以为你会在 2024-09-14 02:15:20

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

所以,是的,对象的选择可能对应于 ui-selected 类,所以你可以做的是:

$(#button).click(function(){
  $("#element1").addClass("ui-selected");

  .......

});

Edit : Sorry for the misunderstanding, I'm editing my answer.

So, yes it is possible the selection of the object corresponds to the class ui-selected, so what you can do is :

$(#button).click(function(){
  $("#element1").addClass("ui-selected");

  .......

});
演多会厌 2024-09-14 02:15:20

Is it not possible to trigger the selected event manually with .trigger('selected')?

來不及說愛妳 2024-09-14 02:15:20

使用 Ionut 代码,怎么样:

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

Using Ionut code, how about:

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

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文