7

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

4

5 回答 5

5
$("#selectable").selectable();
    $("#selectable").on("selectablestart", function (event, ui) {
        event.originalEvent.ctrlKey = true;
    });
于 2014-02-18T12:50:48.170 回答
3

如果您想使用 selectable ,您可能需要使用绑定元键和 mousedown 事件。这里已经有一场辩论了,我只是在谷歌搜索时发现的。

于 2011-03-13T10:49:24.377 回答
2

我曾经需要类似的东西。

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

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

这里也是教程链接

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

于 2011-03-13T09:09:16.157 回答
2

好吧,如果你想修改可选择的代码,在顶部的选项对象中添加一个选项(我叫我的 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);
于 2011-05-20T04:11:32.920 回答
1

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

于 2012-01-09T04:37:46.273 回答