jQuery sortable - 句柄是否必须是现有的DOM元素

时间:2018-02-12 15:57:47

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

当使用sortable并且你想给它一个句柄选项时,你选择的选择器在调用sortable时是否必须是dom上的现有元素?

如果是这样的话,是否还有一个类用于稍后将动态添加的元素?

示例代码:

$("#sortableMembers").sortable
({
    connectWith: "#sortableMembers",
    cursor: "move",
    delay: "150",
    revert: 200,

    start: function (e, ui)
    {
        //Workaround for the height being too small... make the height of the placeholder the same size
        //as the height of the container being dragged
        ui.placeholder.height(ui.item.height());
    }
});

然后当我添加.member元素时,我调用

$('#sortableMembers').sortable('option', 'handle', '.member');

我做了.draggable但它只是让我在屏幕上拖动它没有排序的元素。以上都没有做任何事情。

1 个答案:

答案 0 :(得分:0)

  

您选择的选择器在调用sortable时是否必须是DOM上的现有元素?

是的。另请注意,它必须是您定义为可拖动元素的后代。

  

如果是这样的话,是否还有一个类用于稍后将动态添加的元素?

您可以在使用其句柄附加新项目之后更新可拖动选项,如下所示:

class PriceQuantityEntry 
{
    static var all:[PriceQuantityEntry] = []
    static var prices:[Double:PriceQuantityEntry] = [:]

    var price : Double
    var size  : Double

    init(price:Double, size:Double)
    {
       self.price = price
       self.size  = size
       PriceQuantityEntry.all.append(self)
       // PriceQuantityEntry.all.resort() // on demand or when new prices added
       PriceQuantityEntry.prices[price] = self
    }

    class func update(price:Double, with size:Double)
    {
       if let instance = PriceQuantityEntry.prices[price]
       { instance.size = size }
       else
       { 
         let _ = PriceQuantityEntry(price:price, size:size) 
         PriceQuantityEntry.resort()
       }
    }

    class func resort()
    {
       PriceQuantityEntry.all.sort{$0.price < $1.price}
    }        
}    

// if adding multiple initial entries before updates ...
let _ = PriceQuantityEntry(price:1, size:3)
let _ = PriceQuantityEntry(price:1.25, size:2)
let _ = PriceQuantityEntry(price:0.95, size:1)
PriceQuantityEntry.resort()

// for updates ...
PriceQuantityEntry.update(price:1, with: 2)

// going throug list ...
var count:Double = 0
var total:Double = 0
var quantity:Double = 5
for entry in PriceQuantityEntry.all
{
  total += min(entry.size,quantity-count) * entry.price
  count = min(quantity,count + entry.size)
  if count == quantity {break}
}