Jquery通过选项卡连接列表-数据库交互
我有两个连接的列表,每个列表都位于 Jquery UI 选项卡中。我希望通过选项卡连接列表,以便将 li 元素从一个 ul 列表拖放到选项卡名称上应附加 li 元素添加到目标 ul 列表中。
我已经能够在一定程度上使用 JQuery 通过 连接列表来做到这一点tabs Jquery教程,但是我无法在drop事件上构造我需要的回调函数。
回调需要 (a) 识别可拖动项目的 id,(b) 基于它被放置到的可放置选项卡的目标列表,以及 (c) 通过 GET/POST 将两者发送到 PHP 脚本,这将执行数据库交互(将项目从一个数据库表移动到另一个数据库表)。
这是我到目前为止使用的 JQuery(第一部分处理 sortable1 和 sortable2 的内部排序):(
$(function() {
$("#sortable1").sortable({
items: 'li:not(.ui-state-disabled)',
opacity: 0.6,
cursor: 'move',
connectWith: '.connectedSortable',
update: function() {
var order1 = $(this).sortable("serialize");
$.get("updatedb.php?p=masterlist", order1, function(data){
});
},
});
$("#sortable2").sortable({
items: 'li:not(.ui-state-disabled)',
opacity: 0.6,
cursor: 'move',
connectWith: '.connectedSortable',
update: function() {
var order2 = $(this).sortable("serialize");
$.get("updatedb.php?p=offline", order2, function(data){
});
},
});
var $tabs = $( "#tabs" ).tabs();
$("#tabs").droppable({
accept: ".connectedSortable li",
drop: function(ev, ui) {
}
});
drop Even 回调为空,因为我的所有尝试都不起作用到目前为止。)
这是列表的 html 输出:
<div id="tabs">
<ul>
<li><a href="#tabs-1">Tab 1</a></li>
<li><a href="#tabs-2">Tab 2</a></li>
</ul>
<div id="tabs-1">
<ul id="sortable1" class="connectedSortable ui-helper-reset">
<li class="ui-state-default">Item 1</li>
<li class="ui-state-default">Item 2</li>
<li class="ui-state-default">Item 3</li>
<li class="ui-state-default">Item 4</li>
<li class="ui-state-default">Item 5</li>
</ul>
</div>
<div id="tabs-2">
<ul id="sortable2" class="connectedSortable ui-helper-reset">
<li class="ui-state-highlight">Item 1</li>
<li class="ui-state-highlight">Item 2</li>
<li class="ui-state-highlight">Item 3</li>
<li class="ui-state-highlight">Item 4</li>
<li class="ui-state-highlight">Item 5</li>
</ul>
</div>
</div>
对于此问题的任何帮助将不胜感激。谢谢。
编辑:
解决了,像这样:
var $tab_items = $( "ul:first li", $tabs ).droppable({
accept: ".connectedSortable li",
hoverClass: "ui-state-hover",
drop: function( event, ui ) {
var $id = ui.draggable.attr('id');
var $item = $( this );
var $list = $( $item.find( "a" ).attr( "href" ) )
.find( ".connectedSortable" );
ui.draggable.hide( "fast", function() {
$tabs.tabs( "select", $tab_items.index( $item ) );
$( this ).appendTo( $list ).show( "fast" );
});
var $listname = $list.attr('id');
$.get("tester.php", {list:$listname, id:$id}, function(data){
});
}
});
});
I have two connected lists, each within a Jquery UI tab. I would like the lists to be connected through the tab so that dragging and dropping a li element from one ul list onto the tab name should append the li element to the target ul list.
I have been able to do this to an extent with the JQuery with the connect lists through tabs Jquery tutorial, however I haven't been able to construct the callback function I need on the drop event.
The callback would need to (a) identify the id of the draggable item, (b) the target list based on which droppable tab it was droppped on, and (c) send both to a PHP script via GET/POST which would do the database interaction (moving the item from one database table to another).
Here's the JQuery I'm using so far (the first part handles internal sorting for sortable1 and sortable2):
$(function() {
$("#sortable1").sortable({
items: 'li:not(.ui-state-disabled)',
opacity: 0.6,
cursor: 'move',
connectWith: '.connectedSortable',
update: function() {
var order1 = $(this).sortable("serialize");
$.get("updatedb.php?p=masterlist", order1, function(data){
});
},
});
$("#sortable2").sortable({
items: 'li:not(.ui-state-disabled)',
opacity: 0.6,
cursor: 'move',
connectWith: '.connectedSortable',
update: function() {
var order2 = $(this).sortable("serialize");
$.get("updatedb.php?p=offline", order2, function(data){
});
},
});
var $tabs = $( "#tabs" ).tabs();
$("#tabs").droppable({
accept: ".connectedSortable li",
drop: function(ev, ui) {
}
});
(The drop even callback is empty as none of my attempts have worked so far.)
Here is the html output for the lists:
<div id="tabs">
<ul>
<li><a href="#tabs-1">Tab 1</a></li>
<li><a href="#tabs-2">Tab 2</a></li>
</ul>
<div id="tabs-1">
<ul id="sortable1" class="connectedSortable ui-helper-reset">
<li class="ui-state-default">Item 1</li>
<li class="ui-state-default">Item 2</li>
<li class="ui-state-default">Item 3</li>
<li class="ui-state-default">Item 4</li>
<li class="ui-state-default">Item 5</li>
</ul>
</div>
<div id="tabs-2">
<ul id="sortable2" class="connectedSortable ui-helper-reset">
<li class="ui-state-highlight">Item 1</li>
<li class="ui-state-highlight">Item 2</li>
<li class="ui-state-highlight">Item 3</li>
<li class="ui-state-highlight">Item 4</li>
<li class="ui-state-highlight">Item 5</li>
</ul>
</div>
</div>
Any help with this problem would be greatly appreciated. Thanks.
EDIT:
Solved, like this:
var $tab_items = $( "ul:first li", $tabs ).droppable({
accept: ".connectedSortable li",
hoverClass: "ui-state-hover",
drop: function( event, ui ) {
var $id = ui.draggable.attr('id');
var $item = $( this );
var $list = $( $item.find( "a" ).attr( "href" ) )
.find( ".connectedSortable" );
ui.draggable.hide( "fast", function() {
$tabs.tabs( "select", $tab_items.index( $item ) );
$( this ).appendTo( $list ).show( "fast" );
});
var $listname = $list.attr('id');
$.get("tester.php", {list:$listname, id:$id}, function(data){
});
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很好的解决方案。我在查找拖动元素的值时也遇到了问题。除了这个问题之外,我还必须让可排序函数接受动态生成的未知数量的列表。这对于访问此问题的人可能有用。
Great Solution. I too had problems finding the value of the dragged element. In addition to this problem, I had to have the sortable function accept a dynamically produced unknown number of lists. This may be useful for people visiting this problem.