Jquery:优化 MouseOver 上的 Droppable

发布于 2024-10-05 03:21:31 字数 1129 浏览 4 评论 0原文

我正在使用 Jquery 的 Draggable/Droppable 功能来允许从 TreeView 拖放到简单的 HTML 表,但我发现随着表中单元格数量的增加,Droppable 性能变得非常缓慢。

我环顾四周,人们建议的最常见的解决方案是限制活动可拖动和可放置的数量。现在,限制可拖动对象已经足够简单了(使用树视图节点上的鼠标悬停来启用拖动)。

然后我尝试对可放置的单元格执行相同的操作(即,仅当用户将鼠标悬停在单元格上时才使单元格可放置),但遇到了计时问题。

基本上发生的情况是,用户将节点拖动到单元格上并且无法删除它。但当我尝试第二次时,它起作用了!换句话说,鼠标悬停事件需要在其工作之前完成,但“完成”意味着停止您的第一次拖放,这显然远非理想。

这是我正在使用的代码:

<table id="taskTable">
<tbody>
<tr>
<td class="taskTableHourCell">6:00</td>
<td class='aCell'></td>  <!-- this is the cell to be dragged on, is actually created dynamically, see below -->
</tr>
</tbody>
</table>

<script type="text/javascript">
function addCell()
{
var newCell = $("<td class='aCell'></td>");
newCell.mouseover(function(){$(this).droppable({ drop: onDrop, hoverClass: "ui-state-active", addClasses: false });});
// Add the cell to the table, code not really relevant here
}
</script>

显然,对于动态添加到表中的每个新单元格(在页面加载或表调整大小时)都会调用 addCell() 。

有办法解决这个问题吗?如果 Javascript 有类似 'beginmouseover' 事件之类的东西,这会容易得多......

I'm using the Draggable/Droppable functionality of Jquery to allow drag-and-drop from a TreeView to a simple HTML table, but am finding that Droppable performance gets very sluggish as the number of cells increase in the table.

I've looked around and the most-common solution people suggest is limiting the number of active draggables and droppables. Now, limiting the draggables was simple enough (use the mouseover of the treeview node to enable dragging).

Then I tried to do the same thing for a droppable (ie, only make a cell Droppable when the user mouseovers it), but have hit a timing problem.

Basically what happens is, the user drags a node over the cell and can't drop it. But when I then try to do it a second time, it works! In other words, the mouseover event needs to complete before it works, but 'completing it' means stopping your first drag-and-drop which is obviously far from ideal.

This is the code that I'm using:

<table id="taskTable">
<tbody>
<tr>
<td class="taskTableHourCell">6:00</td>
<td class='aCell'></td>  <!-- this is the cell to be dragged on, is actually created dynamically, see below -->
</tr>
</tbody>
</table>

<script type="text/javascript">
function addCell()
{
var newCell = $("<td class='aCell'></td>");
newCell.mouseover(function(){$(this).droppable({ drop: onDrop, hoverClass: "ui-state-active", addClasses: false });});
// Add the cell to the table, code not really relevant here
}
</script>

and obviously addCell() is called for each new cell that is added to the table dynamically (on page load, or on table resize).

Is there a way around this? This would be much easier if Javascript has something like a 'beginmouseover' event...

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

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

发布评论

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

评论(1

烟柳画桥 2024-10-12 03:21:31

最后,我通过简单地使 TABLE 可放置(而不是单个单元格)然后计算放置处理程序中的放置位置来避免这种情况。

$("#taskTable").droppable({ drop: onDrop, addClasses: false, tolerance: 'pointer' });

function onDrop(event, ui) {
var theCell = document.elementFromPoint(event.pageX - $(window).scrollLeft(), event.pageY - $(window).scrollTop());
}

In the end, I managed to avoid it by simply making the TABLE droppable (instead of the individual cells) and then figuring out the dropposition in the drop handler.

$("#taskTable").droppable({ drop: onDrop, addClasses: false, tolerance: 'pointer' });

function onDrop(event, ui) {
var theCell = document.elementFromPoint(event.pageX - $(window).scrollLeft(), event.pageY - $(window).scrollTop());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文