【问题标题】:jQueryUI Sortable - Make Two Sortables Move SimultaneouslyjQueryUI Sortable - 使两个可排序同时移动
【发布时间】:2012-05-17 17:20:38
【问题描述】:

我正在使用来自 Google 的 CDN 的 jQuery 1.7 和 jQuery UI 1.8(所以我有最新版本)。

我的页面上有两个元素,一个标题导航和一个内容区域,其中包含对应于每个标题列表项的容器。目前,我将 Sortable 附加到两者,允许我重新排列导航元素和内容容器(单独)的视觉顺序。我试图实现的行为是,当我拖动导航元素时,内容容器会随之移动,当我将导航元素放在列表中的新位置时,内容容器会粘在内容中的新位置区域也是如此。我正在处理的网站是一个内网页面,所以我无法提供链接,但我在下面附上了一张图表:

Navigation:
NavOne...NavTwo...NavThree
Content:
ContentOne...ContentTwo...ContentThree

在 NavOne 和 NavTwo 之间拖动 NavThree 后,整个排列应该是这样的:

Navigation:
NavOne...NavThree...NavTwo
Content:
ContentOne...ContentThree...ContentTwo

有没有一种简单的方法可以链接两个 Sortables,使它们以这种方式运行?它并不真正必须流畅或“活”(尽管如果可以的话,我真的很喜欢)。在第一个列表中的重新排序完成(删除)之前,我可以不刷新第二个可排序列表。

我已经在 jQuery UI 论坛上问过这个问题,并等待了一周的回复:[LINK]

这是我在将其移至实际项目之前对其进行测试的骨架示例:

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<style>
/* This is my CSS */
/* Basic reset -- outline for debugging */
* {padding: 0px; margin: 0px; outline: 1px solid rgba(0,0,0,0.1);}
html {width: 100%; height: 100%;}
body {width: 100%; height: 100%;}
/* Main styles */
.containerDiv {
  overflow-x: scroll;
  overflow-y: hidden;
  width: 800px;
  height: 600px;
  white-space: nowrap;
  float: left;
}
.itemDiv {
  width: 200px;
  height: 500px;
  display: inline-block;
}
.navBar ul, .navBar li {
  display: inline;
}
</style>
</head>
<body>
<!-- This is my HTML -->
<div class="navBar">
  <ul>
    <li id="nav1">Navigation 1</li>
    <li id="nav2">Navigation 2</li>
    <li id="nav3">Navigation 3</li>
  </ul>
</div>
<div class="containerDiv">
  <div class="itemDiv" id="item1">Item 1</div>
  <div class="itemDiv" id="item2">Item 2</div>
  <div class="itemDiv" id="item3">Item 3</div>
</div>
</body>
<script>
/* This is my JavaScript */
// Make containerDiv children sortable on X-axis
$(".containerDiv").sortable({
  axis: "x"
});
// Make nav children sortable on x-axis
$(".navBar").sortable({
  axis: "x",
  items: "li"
});
// Bind sorting of each (.navBar li) to its corresponding (.containerDiv .itemDiv):
$(".navBar li").bind("mouseup", function(event,ui){
// If I use "sortupdate" I get little to no response. If I use "mouseup",
// I can at least get an alert to return the value of thisId.
// Note: I am sad that console.log is blocked in Firefox,
// because its DOM inspector is better than Chrome's
  // the (.navBar li) have ids of "nav1, nav2" and so on,
  // and the (.containerDiv .itemDiv) have corresponding ids of "item1, item2"
  // which is my way of attempting to make it easier to link them.
  // This line is my attempt to target the (.containerDiv .itemDiv) which directly
  // corresponds to the (.navBar li) which is currently being sorted:
  var tempId = "#" + $(this).attr('id').replace("nav","item");
  // When this (.navBar li) is updated after a sort drag,
  // trigger the "sortupdate" event on the corresponding (.containerDiv .itemDiv):
  $(tempId).trigger("sortupdate");
});
</script>
</html>

在那一周里,我尝试了许多不同的可能解决方案,但没有一个能达到我想要的效果。我觉得这应该相对简单,但是在一周的工作中(断断续续),我没有得到任何有用的地方。

在寻找一些见解时,我已经阅读了以下相关主题:

我认为,如果一切都失败了,我应该至少可以序列化可排序,将其 ajax 到服务器,然后根据序列化数据更新第二个列表,但我想保持这个本地浏览器以减少对服务器的调用(直到用户选择将新安排保存到他们的帐户)。

那么,StackOverflow ......这是我正在努力完成的合理/可实现的任务,还是我只是在这里用头撞砖?我应该寻找不同的方法吗?如果可能,我想避免修改 Sortable 插件,但如果必须,我会这样做。

我以前从未使用过 jsFiddle,但我意识到它可能会有所帮助,所以这里是:http://jsfiddle.net/34u7n/1/

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-sortable


    【解决方案1】:

    AFAIK 没有可用于 jQuery UI Sortable 的内置方法或模块,因此我编写了自己的 jQuery 代码来模拟此类功能。

    仅当您要基于一个主列表同步多个可排序列表并且所有列表具有相同数量的项目时,此解决方案才有效。也许它也可以让它以两种方式同步/移动,但我没有尝试过。

    <ul class="MasterList">
        <li>one</li>
        <li>two</li>
        <li>three</li>
    </ul>
    
    <ul class="SecondList">
        <li>one</li>
        <li>two</li>
        <li>three</li>
    </ul>
    
    <ul class="ThirdList">
        <li>one</li>
        <li>two</li>
        <li>three</li>
    </ul>
    
    
    $( '.MasterList' ).sortable
    ({
        start: function( event, ui )
        {
            // Store start order of the sorting element
            ui.item.OrderStart = ui.item.index();
        },
        stop: function( event, ui )
        {
            // Total number of sorting element
            var OrderTotal = $( '#ObjectiveListSortable li' ).length - 1;
            // Store drop order of the sorting element
            ui.item.OrderStop = ui.item.index();
    
            // Only do the element sync if there is any change of order. If you simply drag and drop the element back to same position then this prevent such cases.
            if ( ui.item.OrderStart != ui.item.OrderStop )
            {
                // Sorting element dropped to top
                if ( ui.item.OrderStop == 0 )
                {
                    $( '.SecondList' ).eq( 0 ).before( $( '.SecondList' ).eq( ui.item.OrderStart ) );
                    $( '.ThirdList' ).eq( 0 ).before( $( '.ThirdList' ).eq( ui.item.OrderStart ) );
                }
                // Sorting element dragged from top or dropped to bottom
                else if ( ui.item.OrderStart == 0 || ui.item.OrderStop == OrderTotal )
                {
                    $( '.SecondList' ).eq( ui.item.OrderStop ).after( $( '.SecondList' ).eq( ui.item.OrderStart ) );
                    $( '.ThirdList' ).eq( ui.item.OrderStop ).after( $( '.ThirdList' ).eq( ui.item.OrderStart ) );
                }
                else
                {
                    $( '.SecondList' ).eq( ui.item.OrderStop - 1 ).after( $( '.SecondList' ).eq( ui.item.OrderStart ) );
                    $( '.ThirdList' ).eq( ui.item.OrderStop - 1 ).after( $( '.ThirdList' ).eq( ui.item.OrderStart ) );
                }
            }
        }
    });
    

    同步完成后,您还可以在二级列表上触发事件$( '.SecondList' ).trigger( 'refresh' );,以便可排序小部件识别以编程方式完成的项目顺序的变化。

    【讨论】:

      【解决方案2】:

      排序更新不起作用的原因是您试图将sortupdate 事件附加到列表中的每个项目。 sortupdate 不会在列表中的每个项目上触发,它会在列表本身上触发,您可以将事件作为一个整体附加到列表并获取被移动的项目,如下所示。

      $(".navBar").sortable({
        axis: "x",
        items: "li",
        update: function(event, ui) { onSort(event, ui); }
      });
      
      //this should now at least be firing when you move an item 
      function onSort(event, ui)
      {
          var tempId = $(ui.item).attr('id').replace("nav","item");
          alert(tempId);
      }
      

      【讨论】:

      • 我试了一下,它确实产生了警报。当我尝试使用它在.itemDivs 上触发sortupdate 时,它似乎没有效果。我什至附加了一个匿名函数以在update 事件在.itemDivs 上触发时调用alert,这在我对.itemDivs 进行排序时有效,但在通过@987654331 从onSort 函数调用时不会触发@ 或 $(tempId).trigger("update")。我错过了什么吗? jsfiddle.net/adriantp/v88QS/1
      • 我不明白您为什么期望在另一个列表中的项目上调用 sortupdate 以将另一个列表中的项目移动到您希望它所在的位置。我原以为您需要检查最近移动项目的新位置,并将相应项目的位置设置为该新位置。不知道那是不是我只是不太了解 jQuery,但我只是不知道它应该如何通过调用 sortupdate 来知道将项目移动到哪里。
      • 我也不知道我为什么期望它这样做。经过反思,它没有意义;事件作为动作的结果触发,而不是相反。我想我的选择是序列化导航栏并使用序列化数据(通过另一个函数)重新排序内容项,或者扩展可排序插件以允许我将新订单传递给它并导致可排序的更新/刷新.
      • 这看起来就像我的用例!在此期间,您是否更接近可行的解决方案?
      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 2014-02-14
      相关资源
      最近更新 更多