我有以下问题,我有一个大树,其子节点可以根据需要折叠和展开(节点内的数据用AJAX获取).但是,我使用jquery.event.drop / drag来创建拖放目标.
然而,当我折叠/展开掉落目标改变位置,我需要重新计算.这就是我想要这样做的方式:
function create_drop_targets() {
$('li a')
.bind('dropstart', function(event) {
})
.bind('drop', function(event) {
})
.bind('dropend', function(event) {
});
}
折叠/展开时调用create_drop_targets().
但是,这不起作用.我在jquery.event.drop中找到了以下内容:
var drop = $.event.special.drop = {
setup: function(){
drop.$elements = drop.$elements.add( this );
drop.data[ drop.data.length ] = drop.locate( this );
},
locate: function( elem ){ // return { L:left, R:right, T:top, B:bottom, H:height, W:width }
var $el = $(elem), pos = $el.offset(), h = $el.outerHeight(), w = $el.outerWidth();
return { elem: elem, L: pos.left, R: pos.left+w, T: pos.top, B: pos.top+h, W: w, H: h };
}
现在我需要知道如何再次调用setup()方法,以便使用droppables的新位置重新填充$元素.
解决方法:
刚刚遇到同样的问题.我在jQuery的源代码中闲逛并找到了这个(在ui.droppable.js中):
drag: function(draggable, event) {
//If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
if(draggable.options.refreshPositions) $.ui.ddmanager.prepareOffsets(draggable, event);
...
所以,你只需要使用
$(".cocktails").draggable({
refreshPositions: true,
});
似乎没有太多记录……但它解决了我的问题.当然,使一切变得慢一点,我会建议一些依赖于使用的调整(在更改发生之前启用它,并在用户移动鼠标并且发生更改后禁用它).
标签:jquery,javascript,tree
来源: https://codeday.me/bug/20190627/1302193.html