admin管理员组文章数量:1415653
I am impelemnting a drag'n'drop where you can drag users to a role. I know how I get the user ID and the target role ID but I do not know how to get the role ID where the user is dragged FROM!
<div id="role_1" class="role">
<h5>Administrator</h5>
<ul class="users">
<li id="user_1">Foo</li>
<li id="user_2">Bar</li>
</ul>
</div>
<div id="role_2" class="role">
<h5>Member</h5>
<ul class="users">
<li id="user_1337">Baz</li>
</ul>
</div>
<script type="text/javascript">
$(function() {
// Get roles and users lists
var $templates = $(".role"),
$users = $(".users");
// let the user items be draggable
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move"
});
// let the roles be droppable, accepting the user items
$templates.droppable({
accept: ".users > li",
activeClass: "ui-state-highlight",
drop: function(event, ui) {
var $uid = ui.draggable.attr("id"),
$targetRid = $(this).attr("id"),
$sourceRid = ???;
// snip
}
});
});
</script>
Thanks for your help in advance.
I am impelemnting a drag'n'drop where you can drag users to a role. I know how I get the user ID and the target role ID but I do not know how to get the role ID where the user is dragged FROM!
<div id="role_1" class="role">
<h5>Administrator</h5>
<ul class="users">
<li id="user_1">Foo</li>
<li id="user_2">Bar</li>
</ul>
</div>
<div id="role_2" class="role">
<h5>Member</h5>
<ul class="users">
<li id="user_1337">Baz</li>
</ul>
</div>
<script type="text/javascript">
$(function() {
// Get roles and users lists
var $templates = $(".role"),
$users = $(".users");
// let the user items be draggable
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move"
});
// let the roles be droppable, accepting the user items
$templates.droppable({
accept: ".users > li",
activeClass: "ui-state-highlight",
drop: function(event, ui) {
var $uid = ui.draggable.attr("id"),
$targetRid = $(this).attr("id"),
$sourceRid = ???;
// snip
}
});
});
</script>
Thanks for your help in advance.
Share Improve this question asked Dec 7, 2012 at 9:21 Hikaru-ShindoHikaru-Shindo 1,90112 silver badges22 bronze badges2 Answers
Reset to default 3Hook the start
event, and get the closest .role
:
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move",
start: function() {
var role = $(this).closest(".role").attr("id");
// Here, role is either the id or undefined if no role could be found
}
});
If you need that information when it's dropped, you could store it on the element using data
in the start
event and then retrieve it when dropped.
I think you need to remember this id when you start the drag event:
var srcId;
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move",
start: function( event, ui ) {
srcId = $(this).closest(".role").attr('id');
}
});
本文标签: javascriptJQuery UI Get ID of source element in drag39n39dopStack Overflow
版权声明:本文标题:javascript - JQuery UI: Get ID of source element in drag'n'dop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745230405a2648798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论