jQuery 可拖动 +可排序 - 如何更改可排序项目?
我有一个使用以下选项初始化的可排序视频列表:
items: '.videoBox:not(.isFirst, .isLast)',
第一个和最后一个视频不可排序,也不能以任何方式拖动 - 这太棒了!
使用 connectToSortable
选项将其连接到另一个可拖动的视频列表。我希望能够将一些视频拖到 isLast
和 isFirst
视频(其中有很多)之间,因此对于那些我需要再次对这些项目进行排序的视频,所以我使用:
start: function(event, ui) {
if (// my test //)
{
$('#timeline').sortable('option', 'items', '.videoBox');
}
},
这允许我在以前不可排序的项目之间拖放 - 太棒了!
现在,我想在拖动完成后将可排序恢复到初始设置,因此我将其放入可拖动初始化中:
stop: function() {
if (// my test //)
{
$('#timeline').sortable('option', 'items', '.videoBox:not(.isFirst, .isLast)');
$('#timeline').sortable('refresh');
$('#timeline .videoBox.isLast, #timeline .videoBox.isFirst').draggable('destroy');
}
},
如果可拖动未放入#timeline,则效果很好。但是,如果将其放入 #timeline
中,则似乎不会调用此回调(?)。我已将完全相同的代码添加到可排序事件中:stop
、update
和 deactivate
,虽然我可以看到这些回调正在被触发,isFirst
和 isLast
视频仍然可以拖动。
然而它们不可排序 - 所以我认为问题在于 .draggable('destroy')
位...有什么想法/建议吗?
干杯, 汤姆
I have a sortable list of videos initialised with the option:
items: '.videoBox:not(.isFirst, .isLast)',
The first and last videos are not sortable, neither are they draggable in any way - this is great!
This is connected to another draggable list of videos using the connectToSortable
option. Some of the videos I want to be able to drag in between the isLast
and isFirst
videos (of which there are many) so for those I need these items sortable again, so I use:
start: function(event, ui) {
if (// my test //)
{
$('#timeline').sortable('option', 'items', '.videoBox');
}
},
This allows me to drag and drop amongst the items that were previously not sortable - great!
Now I want to revert the sortable to the initial set up once the drag has finished, so I put this in the draggable init:
stop: function() {
if (// my test //)
{
$('#timeline').sortable('option', 'items', '.videoBox:not(.isFirst, .isLast)');
$('#timeline').sortable('refresh');
$('#timeline .videoBox.isLast, #timeline .videoBox.isFirst').draggable('destroy');
}
},
This works great IF the draggable is not dropped into #timeline. However if it is dropped into #timeline
then this callback does not seem to be called(?). I have added the exact same code into the sortable events: stop
, update
and deactivate
, and while I can see that these callbacks are getting fired, the isFirst
and isLast
videos are still draggable.
However they are not sortable - so I think the problem lies with the .draggable('destroy')
bit... Any ideas/suggestions?
Cheers,
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法修复甚至无法确定上述问题的原因。然而,我找到了一个看起来相当稳健的解决方法。
在
sortable
init 的stop
回调中:第一行设置 items 选项,就像我之前所做的那样。然后,我将可排序初始化选项保存到变量中,销毁可排序行为并使用正确的选项重新初始化它。这就是我现在想要的 - 我销毁了 isFirst 和 isLast 项上的可拖动项)。我已将
stop
回调与上面的draggable
回调完全相同,这是当拖动未结束于可排序时的情况所必需的。看起来有点像大锤方法,但正在努力寻找更好的方法。有兴趣知道是否有人有任何其他建议。
汤姆
I havent been able to fix or even identify the cause of the problem above. I have however found a workaround that seems fairly robust.
In the
stop
callback of thesortable
init:The first line sets the items option as I have been doing before. Then I save the
sortable
init options to a variable, destroy thesortable
behaviour and reinitialise it with the correct options. This does what I want now - i destroys the draggable on theisFirst
andisLast
items). I have left thestop
callback exactly as above in thedraggable
callback which is required for the case when the drag does not end up over the sortable.Seems like a bit of a sledgehammer approach but struggling to find a nicer method. Be interested to know if anyone has any other suggestions though.
Tom