在 jQuery UI 可选中启用 Shift-Multiselect
我想通过按住 shift 在 jQuery UI 可选择表中启用多选功能。
如果在 mouseclick 上按住 shift
- 获取最上面的选定元素
- 获取单击的元素
- 选择之间的所有元素
,我可能应该这样做,但我找不到如何以干净的方式执行此操作...
目前,我在可选配置中得到了这个:
start: function(e)
{
var oTarget = jQuery(e.target);
if(!oTarget.is('tr')) oTarget = oTarget.parents('tr');
}
所以 oTarget
是单击的元素(而 e.currentTarget
是整个表格),但现在怎么办?我怎样才能找到哪些元素已经被选择,从而可以告诉我单击的元素是在所选元素之上还是之下,并选择之间的所有元素?
我现在已经解决了这个问题,添加到可选元素中:
jQuery(table).mousedown(function(e)
{
//Enable multiselect with shift key
if(e.shiftKey)
{
var oTarget = jQuery(e.target);
if(!oTarget.is('.ui-selectee')) oTarget = oTarget.parents('.ui-selectee');
var iNew = jQuery(e.currentTarget).find('.ui-selectee').index(oTarget);
var iCurrent = jQuery(e.currentTarget).find('.ui-selectee').index(jQuery(e.currentTarget).find('.ui-selected'));
if (iCurrent < iNew) {
iHold = iNew;
iNew = iCurrent;
iCurrent = iHold;
}
if(iNew != '-1')
{
jQuery(e.currentTarget).find('.ui-selected').removeClass('ui-selected');
for (i=iNew;i<=iCurrent;i++) {
jQuery(e.currentTarget).find('.ui-selectee').eq(i).addClass('ui-selected');
}
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
return false;
}
}
}).selectable(...)
I want to enable multiselect capabilities in a jQuery UI Selectable table by holding shift.
I probably should do something like this if shift is held down on mouseclick
- Get topmost selected element
- Get clicked element
- Select all elements in between
but i can't find how to do this in a clean way...
At the moment i got this inside the selectable configuration:
start: function(e)
{
var oTarget = jQuery(e.target);
if(!oTarget.is('tr')) oTarget = oTarget.parents('tr');
}
So oTarget
is the clicked element (and e.currentTarget
is the whole table) but now what? How can i find which elements are already selected in a way that can tell me if the clicked element is over or below the selected ones and select everything in between?
I've solved it now like this, added to the selectable element:
jQuery(table).mousedown(function(e)
{
//Enable multiselect with shift key
if(e.shiftKey)
{
var oTarget = jQuery(e.target);
if(!oTarget.is('.ui-selectee')) oTarget = oTarget.parents('.ui-selectee');
var iNew = jQuery(e.currentTarget).find('.ui-selectee').index(oTarget);
var iCurrent = jQuery(e.currentTarget).find('.ui-selectee').index(jQuery(e.currentTarget).find('.ui-selected'));
if (iCurrent < iNew) {
iHold = iNew;
iNew = iCurrent;
iCurrent = iHold;
}
if(iNew != '-1')
{
jQuery(e.currentTarget).find('.ui-selected').removeClass('ui-selected');
for (i=iNew;i<=iCurrent;i++) {
jQuery(e.currentTarget).find('.ui-selectee').eq(i).addClass('ui-selected');
}
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
return false;
}
}
}).selectable(...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在没有插件的情况下做到这一点,如下所示:
这是现场演示:http://jsfiddle。网/mac2000/DJFaL/1/嵌入/结果/
You can do it without plugins like this:
Here is live demo: http://jsfiddle.net/mac2000/DJFaL/1/embedded/result/
我为该功能编写了简单的插件。它不依赖于 jQuery ui Selectable 插件,据我所知,它可以很好地工作。
您可以在这里找到插件代码和简单示例: http://jsfiddle.net/bMgpc/170/
下面写简单的描述。
基本用法:
如果按住“Ctrl”或“Command 键”,则可以一一选择/取消选择元素。
ul - 保存要选择的内部元素的父级。
有许多可用选项:
这是一个开发版本插件,因此请小心使用
I wrote simple plugin for that functionality. It's not dependent on jQuery ui Selectable plugin and as far as i know works fine with it.
You can find plugin code and simple example here: http://jsfiddle.net/bMgpc/170/
Going to write simple description below.
Basic usage:
If you hold "Ctrl" or "Command Key" then you can select/unselect elements one by one.
ul - parent that holds inner elements to be selected.
There are number of options available:
It's a dev version plugin, so use with care
环顾四周后,我无法找到解决这个问题的方法,同时仍然使用 jQuery UI 的 Selectable 函数,所以我写了一个。本质上,它利用 Selectable 的选定/未选定回调来管理 DOM 状态,同时仍然按照标准 Selectable API 执行回调。它支持以下用例:
选中对于表格:
一些注释。 (1) 目前仅支持兄弟元素。 (2) 它将传递您将在表示例中看到的配置选项以及 Selectable 方法。 (3) 我喜欢 underscore.js,所以使用它,尽管它不是必需的。如果您不想使用这个很棒的库,请随意更换其简单的检查和扩展。不,我与 underscore.js 没有任何关系。 :)
表小提琴示例
列表小提琴示例
希望这对其他人有帮助!干杯。
After looking around I was unable to find a solution to this problem while still using jQuery UI's Selectable function, so I wrote one up. Essentially it taps into the selected / unselected callbacks of Selectable to manage DOM state while still honoring the callbacks as per the standard Selectable API. It supports the following use case:
Usage for a table:
A few notes. (1) It currently only supports sibling elements. (2) It will pass through configuration options as you will see in the table example as well as the Selectable methods. (3) I heart underscore.js so it is used even though for this it is not essential. Feel free to swap out its simple checks and extends if you don't want to use this awesome library. And no, I have no affiliation with underscore.js. :)
table fiddle example
list fiddle example
Hope this helps someone else! Cheers.