【发布时间】:2012-03-11 14:19:57
【问题描述】:
我想通过按住 shift 在 jQuery UI 可选表中启用多选功能。
如果在鼠标点击时按住 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(...)
【问题讨论】:
标签: javascript jquery jquery-ui multi-select jquery-ui-selectable