【问题标题】:jQuery sortable placeholder position (index)jQuery 可排序占位符位置(索引)
【发布时间】:2012-08-03 11:30:39
【问题描述】:

我想在可排序占位符中显示可排序元素位置(或索引)。它在拖动第一个元素时起作用,而不是与其他元素一起拖动(位置 0 而不是 1)。

请查看:http://jsfiddle.net/upsidown/Hw2rJ/

我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    我一直在寻找类似挑战的解决方案,并找到了这个 http://forum.jquery.com/topic/sortable-show-placeholders-position-and-give-it-a-number 并稍加调整(和更正)就可以了

    $('.elements-sortable').sortable({
        placeholder: 'sortable-placeholder',
        opacity: 0.6,
        helper: 'clone',
        sort: function(event,ui){
            $(ui.placeholder).html(Number($('.elements-sortable > div:visible').index(ui.placeholder)+1));
        }
    });
    

    【讨论】:

      【解决方案2】:

      可能是因为它在您拖动的元素之前附加了新元素

      让我们试试这段代码

      var position;
      $(".elements-sortable").sortable({
      
      placeholder: "sortable-placeholder",
      opacity: "0.6",
      
      start: function(event, ui) {
      
          position = position = Number($(".elements-sortable > div:not(.ui-sortable-helper)").index(ui.placeholder)+1);
          $(".sortable-placeholder").html('Drop me at position ' + position);
      
      },
      
      change: function(event, ui) {
          position = position = Number($(".elements-sortable > div:not(.ui-sortable-helper)").index(ui.placeholder)+1);
          $(".sortable-placeholder").html('Drop me at position ' + position);
      }
      });
      

      我已经在 jsFiddle 上测试过了

      【讨论】:

      • 是的,我肯定可以这样做,但是没有一种“标准”方法可以在所有元素上获得相同的索引位置吗?
      • 我已经修复了一些代码,但我认为有时它有一个错误,当将 div 移动到第一个位置时我无法解释。
      【解决方案3】:

      jsFiddle上试试这个

      $(".connectedSortable").sortable({
          connectWith: ".connectedSortable",
          stop: function (e,ui ) {
              $("body").append("<p> current position : " + $(ui.item).prevAll().length + "</p>");
          }
      });
      

      【讨论】:

      • 不。这不是这里需要的。
      猜你喜欢
      • 1970-01-01
      • 2016-12-19
      • 2011-09-02
      • 1970-01-01
      • 2012-12-02
      • 2012-02-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      相关资源
      最近更新 更多