jQuery UI 可选择 - 使多选默认

发布于 2024-10-21 23:30:39 字数 46 浏览 2 评论 0原文

我需要默认提供多项选择,这样用户就不必按住 ctrl。希望得到一些帮助,谢谢。

I need to make multiple selection available by default, so the user doesn't have to hold ctrl. Would appreciate some help with this, thanks.

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

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

发布评论

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

评论(5

捂风挽笑 2024-10-28 23:30:39
$("#selectable").selectable();
    $("#selectable").on("selectablestart", function (event, ui) {
        event.originalEvent.ctrlKey = true;
    });
$("#selectable").selectable();
    $("#selectable").on("selectablestart", function (event, ui) {
        event.originalEvent.ctrlKey = true;
    });
梦中楼上月下 2024-10-28 23:30:39

如果您想使用 selectable ,您可能需要将元键与 mousedown 事件结合使用。 这里已经有一场争论,我刚刚在谷歌搜索时发现。

If you want to use selectable , you might want to use bind meta key with mousedown event. There's already a debate going down here , i just found when i googled.

独守阴晴ぅ圆缺 2024-10-28 23:30:39

好吧,如果你想修改可选代码,请在顶部的选项对象中添加一个选项(我称之为 multi_no_key,默认为 false)。

然后,查找声明 var doSelect 的位置,它最初看起来像这样

var doSelect = !event.metaKey || !selectee.$element.hasClass('ui-selected');

并将其更改为此

var doSelect;
if(!options.multi_no_key)
    doSelect = !event.metaKey || !selectee.$element.hasClass('ui-selected');
else
    doSelect = !selectee.$element.hasClass('ui-selected');

在同一个 _mouseStart 方法中,查找以下代码

this.selectees.filter('.ui-selected').each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.startselected = true;
        if (!event.metaKey) {
            selectee.$element.removeClass('ui-selected');
            selectee.selected = false;
            selectee.$element.addClass('ui-unselecting');
            selectee.unselecting = true;
            // selectable UNSELECTING callback
            self._trigger("unselecting", event, {
                unselecting: selectee.element
            });
        }
    });

并更改条件以检查 multi_no_key 的选项

this.selectees.filter('.ui-selected').each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.startselected = true;
        if (!event.metaKey && options.multi_no_key == false) {
            selectee.$element.removeClass('ui-selected');
            selectee.selected = false;
            selectee.$element.addClass('ui-unselecting');
            selectee.unselecting = true;
            // selectable UNSELECTING callback
            self._trigger("unselecting", event, {
                unselecting: selectee.element
            });
        }
    });

我测试了这个,它对我来说效果很好。以下是可选js文件的全部内容(版本1.8.12)

/*
* jQuery UI Selectable 1.8.12
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Selectables
*
* Depends:
*   jquery.ui.core.js
*   jquery.ui.mouse.js
*   jquery.ui.widget.js
*/
(function( $, undefined ) {

$.widget("ui.selectable", $.ui.mouse, {
options: {
    appendTo: 'body',
    autoRefresh: true,
    distance: 0,
    filter: '*',
    tolerance: 'touch',
            multi_no_key: false
},
_create: function() {
    var self = this;

    this.element.addClass("ui-selectable");

    this.dragged = false;

    // cache selectee children based on filter
    var selectees;
    this.refresh = function() {
        selectees = $(self.options.filter, self.element[0]);
        selectees.each(function() {
            var $this = $(this);
            var pos = $this.offset();
            $.data(this, "selectable-item", {
                element: this,
                $element: $this,
                left: pos.left,
                top: pos.top,
                right: pos.left + $this.outerWidth(),
                bottom: pos.top + $this.outerHeight(),
                startselected: false,
                selected: $this.hasClass('ui-selected'),
                selecting: $this.hasClass('ui-selecting'),
                unselecting: $this.hasClass('ui-unselecting')
            });
        });
    };
    this.refresh();

    this.selectees = selectees.addClass("ui-selectee");

    this._mouseInit();

    this.helper = $("<div class='ui-selectable-helper'></div>");
},

destroy: function() {
    this.selectees
        .removeClass("ui-selectee")
        .removeData("selectable-item");
    this.element
        .removeClass("ui-selectable ui-selectable-disabled")
        .removeData("selectable")
        .unbind(".selectable");
    this._mouseDestroy();

    return this;
},

_mouseStart: function(event) {
    var self = this;

    this.opos = [event.pageX, event.pageY];

    if (this.options.disabled)
        return;

    var options = this.options;

    this.selectees = $(options.filter, this.element[0]);

    this._trigger("start", event);

    $(options.appendTo).append(this.helper);
    // position helper (lasso)
    this.helper.css({
        "left": event.clientX,
        "top": event.clientY,
        "width": 0,
        "height": 0
    });

    if (options.autoRefresh) {
        this.refresh();
    }

    this.selectees.filter('.ui-selected').each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.startselected = true;
        if (!event.metaKey && options.multi_no_key == false) {
            selectee.$element.removeClass('ui-selected');
            selectee.selected = false;
            selectee.$element.addClass('ui-unselecting');
            selectee.unselecting = true;
            // selectable UNSELECTING callback
            self._trigger("unselecting", event, {
                unselecting: selectee.element
            });
        }
    });

    $(event.target).parents().andSelf().each(function() {
        var selectee = $.data(this, "selectable-item");
        if (selectee) {                            
                            var doSelect;
                            if(!options.multi_no_key)
                                doSelect = !event.metaKey || !selectee.$element.hasClass('ui-selected');
                            else
                                doSelect = !selectee.$element.hasClass('ui-selected');

            selectee.$element
                .removeClass(doSelect ? "ui-unselecting" : "ui-selected")
                .addClass(doSelect ? "ui-selecting" : "ui-unselecting");
            selectee.unselecting = !doSelect;
            selectee.selecting = doSelect;
            selectee.selected = doSelect;
            // selectable (UN)SELECTING callback
            if (doSelect) {
                self._trigger("selecting", event, {
                    selecting: selectee.element
                });
            } else {
                self._trigger("unselecting", event, {
                    unselecting: selectee.element
                });
            }
            return false;
        }
    });

},

_mouseDrag: function(event) {
    var self = this;
    this.dragged = true;

    if (this.options.disabled)
        return;

    var options = this.options;

    var x1 = this.opos[0], y1 = this.opos[1], x2 = event.pageX, y2 = event.pageY;
    if (x1 > x2) { var tmp = x2; x2 = x1; x1 = tmp; }
    if (y1 > y2) { var tmp = y2; y2 = y1; y1 = tmp; }
    this.helper.css({left: x1, top: y1, width: x2-x1, height: y2-y1});

    this.selectees.each(function() {
        var selectee = $.data(this, "selectable-item");
        //prevent helper from being selected if appendTo: selectable
        if (!selectee || selectee.element == self.element[0])
            return;
        var hit = false;
        if (options.tolerance == 'touch') {
            hit = ( !(selectee.left > x2 || selectee.right < x1 || selectee.top > y2 || selectee.bottom < y1) );
        } else if (options.tolerance == 'fit') {
            hit = (selectee.left > x1 && selectee.right < x2 && selectee.top > y1 && selectee.bottom < y2);
        }

        if (hit) {
            // SELECT
            if (selectee.selected) {
                selectee.$element.removeClass('ui-selected');
                selectee.selected = false;
            }
            if (selectee.unselecting) {
                selectee.$element.removeClass('ui-unselecting');
                selectee.unselecting = false;
            }
            if (!selectee.selecting) {
                selectee.$element.addClass('ui-selecting');
                selectee.selecting = true;
                // selectable SELECTING callback
                self._trigger("selecting", event, {
                    selecting: selectee.element
                });
            }
        } else {
            // UNSELECT
            if (selectee.selecting) {
                if (event.metaKey && selectee.startselected) {
                    selectee.$element.removeClass('ui-selecting');
                    selectee.selecting = false;
                    selectee.$element.addClass('ui-selected');
                    selectee.selected = true;
                } else {
                    selectee.$element.removeClass('ui-selecting');
                    selectee.selecting = false;
                    if (selectee.startselected) {
                        selectee.$element.addClass('ui-unselecting');
                        selectee.unselecting = true;
                    }
                    // selectable UNSELECTING callback
                    self._trigger("unselecting", event, {
                        unselecting: selectee.element
                    });
                }
            }
            if (selectee.selected) {
                if (!event.metaKey && !selectee.startselected) {
                    selectee.$element.removeClass('ui-selected');
                    selectee.selected = false;

                    selectee.$element.addClass('ui-unselecting');
                    selectee.unselecting = true;
                    // selectable UNSELECTING callback
                    self._trigger("unselecting", event, {
                        unselecting: selectee.element
                    });
                }
            }
        }
    });

    return false;
},

_mouseStop: function(event) {
    var self = this;

    this.dragged = false;

    var options = this.options;

    $('.ui-unselecting', this.element[0]).each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.$element.removeClass('ui-unselecting');
        selectee.unselecting = false;
        selectee.startselected = false;
        self._trigger("unselected", event, {
            unselected: selectee.element
        });
    });
    $('.ui-selecting', this.element[0]).each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.$element.removeClass('ui-selecting').addClass('ui-selected');
        selectee.selecting = false;
        selectee.selected = true;
        selectee.startselected = true;
        self._trigger("selected", event, {
            selected: selectee.element
        });
    });
    this._trigger("stop", event);

    this.helper.remove();

    return false;
}
});
$.extend($.ui.selectable, {
version: "1.8.12"
});
})(jQuery);

Well, if you want to modify the selectable code, add an option to the options object at the top (I called mine multi_no_key, and defaulted to false).

Then, look for where var doSelect is declared, it originally looks like this

var doSelect = !event.metaKey || !selectee.$element.hasClass('ui-selected');

And change it to this

var doSelect;
if(!options.multi_no_key)
    doSelect = !event.metaKey || !selectee.$element.hasClass('ui-selected');
else
    doSelect = !selectee.$element.hasClass('ui-selected');

In that same _mouseStart method, look for the following code

this.selectees.filter('.ui-selected').each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.startselected = true;
        if (!event.metaKey) {
            selectee.$element.removeClass('ui-selected');
            selectee.selected = false;
            selectee.$element.addClass('ui-unselecting');
            selectee.unselecting = true;
            // selectable UNSELECTING callback
            self._trigger("unselecting", event, {
                unselecting: selectee.element
            });
        }
    });

And change the conditional to check the option for multi_no_key

this.selectees.filter('.ui-selected').each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.startselected = true;
        if (!event.metaKey && options.multi_no_key == false) {
            selectee.$element.removeClass('ui-selected');
            selectee.selected = false;
            selectee.$element.addClass('ui-unselecting');
            selectee.unselecting = true;
            // selectable UNSELECTING callback
            self._trigger("unselecting", event, {
                unselecting: selectee.element
            });
        }
    });

I tested this and it worked well for me. Below is the entire contents of the selectable js file (version 1.8.12)

/*
* jQuery UI Selectable 1.8.12
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Selectables
*
* Depends:
*   jquery.ui.core.js
*   jquery.ui.mouse.js
*   jquery.ui.widget.js
*/
(function( $, undefined ) {

$.widget("ui.selectable", $.ui.mouse, {
options: {
    appendTo: 'body',
    autoRefresh: true,
    distance: 0,
    filter: '*',
    tolerance: 'touch',
            multi_no_key: false
},
_create: function() {
    var self = this;

    this.element.addClass("ui-selectable");

    this.dragged = false;

    // cache selectee children based on filter
    var selectees;
    this.refresh = function() {
        selectees = $(self.options.filter, self.element[0]);
        selectees.each(function() {
            var $this = $(this);
            var pos = $this.offset();
            $.data(this, "selectable-item", {
                element: this,
                $element: $this,
                left: pos.left,
                top: pos.top,
                right: pos.left + $this.outerWidth(),
                bottom: pos.top + $this.outerHeight(),
                startselected: false,
                selected: $this.hasClass('ui-selected'),
                selecting: $this.hasClass('ui-selecting'),
                unselecting: $this.hasClass('ui-unselecting')
            });
        });
    };
    this.refresh();

    this.selectees = selectees.addClass("ui-selectee");

    this._mouseInit();

    this.helper = $("<div class='ui-selectable-helper'></div>");
},

destroy: function() {
    this.selectees
        .removeClass("ui-selectee")
        .removeData("selectable-item");
    this.element
        .removeClass("ui-selectable ui-selectable-disabled")
        .removeData("selectable")
        .unbind(".selectable");
    this._mouseDestroy();

    return this;
},

_mouseStart: function(event) {
    var self = this;

    this.opos = [event.pageX, event.pageY];

    if (this.options.disabled)
        return;

    var options = this.options;

    this.selectees = $(options.filter, this.element[0]);

    this._trigger("start", event);

    $(options.appendTo).append(this.helper);
    // position helper (lasso)
    this.helper.css({
        "left": event.clientX,
        "top": event.clientY,
        "width": 0,
        "height": 0
    });

    if (options.autoRefresh) {
        this.refresh();
    }

    this.selectees.filter('.ui-selected').each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.startselected = true;
        if (!event.metaKey && options.multi_no_key == false) {
            selectee.$element.removeClass('ui-selected');
            selectee.selected = false;
            selectee.$element.addClass('ui-unselecting');
            selectee.unselecting = true;
            // selectable UNSELECTING callback
            self._trigger("unselecting", event, {
                unselecting: selectee.element
            });
        }
    });

    $(event.target).parents().andSelf().each(function() {
        var selectee = $.data(this, "selectable-item");
        if (selectee) {                            
                            var doSelect;
                            if(!options.multi_no_key)
                                doSelect = !event.metaKey || !selectee.$element.hasClass('ui-selected');
                            else
                                doSelect = !selectee.$element.hasClass('ui-selected');

            selectee.$element
                .removeClass(doSelect ? "ui-unselecting" : "ui-selected")
                .addClass(doSelect ? "ui-selecting" : "ui-unselecting");
            selectee.unselecting = !doSelect;
            selectee.selecting = doSelect;
            selectee.selected = doSelect;
            // selectable (UN)SELECTING callback
            if (doSelect) {
                self._trigger("selecting", event, {
                    selecting: selectee.element
                });
            } else {
                self._trigger("unselecting", event, {
                    unselecting: selectee.element
                });
            }
            return false;
        }
    });

},

_mouseDrag: function(event) {
    var self = this;
    this.dragged = true;

    if (this.options.disabled)
        return;

    var options = this.options;

    var x1 = this.opos[0], y1 = this.opos[1], x2 = event.pageX, y2 = event.pageY;
    if (x1 > x2) { var tmp = x2; x2 = x1; x1 = tmp; }
    if (y1 > y2) { var tmp = y2; y2 = y1; y1 = tmp; }
    this.helper.css({left: x1, top: y1, width: x2-x1, height: y2-y1});

    this.selectees.each(function() {
        var selectee = $.data(this, "selectable-item");
        //prevent helper from being selected if appendTo: selectable
        if (!selectee || selectee.element == self.element[0])
            return;
        var hit = false;
        if (options.tolerance == 'touch') {
            hit = ( !(selectee.left > x2 || selectee.right < x1 || selectee.top > y2 || selectee.bottom < y1) );
        } else if (options.tolerance == 'fit') {
            hit = (selectee.left > x1 && selectee.right < x2 && selectee.top > y1 && selectee.bottom < y2);
        }

        if (hit) {
            // SELECT
            if (selectee.selected) {
                selectee.$element.removeClass('ui-selected');
                selectee.selected = false;
            }
            if (selectee.unselecting) {
                selectee.$element.removeClass('ui-unselecting');
                selectee.unselecting = false;
            }
            if (!selectee.selecting) {
                selectee.$element.addClass('ui-selecting');
                selectee.selecting = true;
                // selectable SELECTING callback
                self._trigger("selecting", event, {
                    selecting: selectee.element
                });
            }
        } else {
            // UNSELECT
            if (selectee.selecting) {
                if (event.metaKey && selectee.startselected) {
                    selectee.$element.removeClass('ui-selecting');
                    selectee.selecting = false;
                    selectee.$element.addClass('ui-selected');
                    selectee.selected = true;
                } else {
                    selectee.$element.removeClass('ui-selecting');
                    selectee.selecting = false;
                    if (selectee.startselected) {
                        selectee.$element.addClass('ui-unselecting');
                        selectee.unselecting = true;
                    }
                    // selectable UNSELECTING callback
                    self._trigger("unselecting", event, {
                        unselecting: selectee.element
                    });
                }
            }
            if (selectee.selected) {
                if (!event.metaKey && !selectee.startselected) {
                    selectee.$element.removeClass('ui-selected');
                    selectee.selected = false;

                    selectee.$element.addClass('ui-unselecting');
                    selectee.unselecting = true;
                    // selectable UNSELECTING callback
                    self._trigger("unselecting", event, {
                        unselecting: selectee.element
                    });
                }
            }
        }
    });

    return false;
},

_mouseStop: function(event) {
    var self = this;

    this.dragged = false;

    var options = this.options;

    $('.ui-unselecting', this.element[0]).each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.$element.removeClass('ui-unselecting');
        selectee.unselecting = false;
        selectee.startselected = false;
        self._trigger("unselected", event, {
            unselected: selectee.element
        });
    });
    $('.ui-selecting', this.element[0]).each(function() {
        var selectee = $.data(this, "selectable-item");
        selectee.$element.removeClass('ui-selecting').addClass('ui-selected');
        selectee.selecting = false;
        selectee.selected = true;
        selectee.startselected = true;
        self._trigger("selected", event, {
            selected: selectee.element
        });
    });
    this._trigger("stop", event);

    this.helper.remove();

    return false;
}
});
$.extend($.ui.selectable, {
version: "1.8.12"
});
})(jQuery);
向地狱狂奔 2024-10-28 23:30:39

我曾经需要类似的东西。

这是一个关于如何做到这一点的很好的例子。我想它可能会给你一些想法。

http://www.joelanman.com/static/examples/multiple_selection/

这里是教程链接以及

http:// /www.joelanman.com/2009/03/simple-multiple-selection-with-checkbox-lists-and-jquery/

i needed something like it once.

Here is a nice example about how to do it. I think it may give you some ideas.

http://www.joelanman.com/static/examples/multiple_selection/

Here is the tutorial link as well

http://www.joelanman.com/2009/03/simple-multiple-selection-with-checkbox-lists-and-jquery/

哆兒滾 2024-10-28 23:30:39

老实说,我只是在 jqui 脚本上对“c.metaKey”进行了查找/替换,并将其替换为“true /*edited*/”...肮脏,但有效。

honestly, I just did a find/replace for 'c.metaKey' and replaced it with 'true /* edited */' on the jqui script... Dirty, but effective.

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